Skip to content
Advertisement

sql server – How can I put the values in a result side by side

CREATE TABLE #E (
ID INT ,
NAME VARCHAR(20),
RESULT VARCHAR(20)
)

INSERT INTO #E VALUES (1,'DOMAIN','marka')
INSERT INTO #E VALUES (1,'EXTENSION','.com')
INSERT INTO #E VALUES (1,'TYPE','WEB')

SELECT * FROM #E

Declare @DomainName varchar(20) = ''
--How can I put the values(RESULT) in a result side by side
SELECT RESULT FROM #E  WHERE ID=1 AND NAME IN ('DOMAIN','EXTENSION')

How can I put the values(RESULT) in a result side by side How can I do it? –> Set @DomainName = ‘marka.com’

@DomainName = ‘marka.com’ –How do I add

enter image description here

Advertisement

Answer

DECLARE @DomainName varchar(20) = ''

SELECT @DomainName = (DOMAIN + EXTENSION) FROM
(
    SELECT * 
    FROM #E  
    WHERE ID=1 
    AND NAME IN ('DOMAIN','EXTENSION')
) as main
PIVOT
(
    MAX(RESULT)
    FOR NAME IN ([DOMAIN],[EXTENSION])
) piv

SELECT @DomainName
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement