-3

I have to inspect single rows of data table in SQL Server Management Studio. The table has a lot of columns. It would be more easier to read if the result in grid was converted:

Is

Number | Description | Date1 | Date2 | ...
123    | bla bla     | xx xx | yy yy | ...

Wanted:

Number      | 123
Description | bla bla
Date1       | xx xx
Date2       | yy yy
...
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Volker
  • 447
  • 1
  • 5
  • 19
  • Possible duplicate of [Understanding PIVOT function in T-SQL](https://stackoverflow.com/questions/10428993/understanding-pivot-function-in-t-sql) – Greg Viers Feb 09 '18 at 21:08
  • 1
    You can use [unpivot](https://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx) functionality for this. – JNevill Feb 09 '18 at 21:26
  • Unpivot brings desired result - however is quite big query – Volker Feb 14 '18 at 20:54

2 Answers2

1
CREATE FUNCTION [dbo].[fnParseStringTSQL] (@string NVARCHAR(MAX))
RETURNS @parsedString TABLE (ID INT IDENTITY(1,1),string NVARCHAR(MAX))
AS 
BEGIN
DECLARE @position int
SET @position = 1
SET @string = @string + ','
WHILE charindex(',',@string,@position) <> 0
  BEGIN
     INSERT into @parsedString
     SELECT  substring(@string, @position, charindex(',',@string,@position) - 
 @position)
     SET @position = charindex(',',@string,@position) + 1
  END
 RETURN
END
GO

DECLARE @string1 NVARCHAR(MAX)='Number,Description,Date1,Date2'
DECLARE @string2 NVARCHAR(MAX)='123,bla bla,xx xx,yy yy'
SELECT K.string,K1.string FROM (SELECT ID,string FROM [dbo].
[fnParseStringTSQL](@string1))K
INNER JOIN (SELECT ID,string FROM [dbo].[fnParseStringTSQL](@string2))K1
ON K.ID=K1.ID
Erfan Mohammadi
  • 424
  • 3
  • 7
0
SELECT  Number , Desci 

FROM (
SELECT CAST (Number as varchar) as Number
      ,CAST(Desc_EN as varchar) as Desc_EN
      ,CAST (Desc_DE as varchar) as Desc_DE
      ,CAST(Specification_EN as varchar) as Specification_EN
      ,CAST(Specification_DE as varchar) as Specification_DE
      ,CAST(Template as varchar) as Template
FROM dbo.Master
WHERE Number = '10257285'
) as a


unpivot
( Desci for Descs in 
   (Desc_EN ,Desc_DE, Specification_EN, Specification_DE, Template)
)  as b;
Volker
  • 447
  • 1
  • 5
  • 19