Author |
Topic |
Indsqlbeginner
Starting Member
21 Posts |
Posted - 2014-06-30 : 02:28:49
|
Dear Allplease help me on the belowCurrent Query Input :Quarter Month YearQ1Y16 4,5,6 2015Q2Y15 7,8,9 2014Q2Y16 7,8,9 2015Q3Y15 10,11,12 2014Q4Y15 1,2,3 2015 Desired OutputQuarter Month YearQ1Y16 4 2015Q1Y16 5 2015Q1Y16 6 2015Q2Y15 7 2014Q2Y15 8 2014Q2Y15 9 2014Q2Y16 7 2015Q2Y16 8 2015Q2Y16 9 2015Q3Y15 10 2014Q3Y15 11 2014Q3Y15 12 2014Q4Y15 1 2015Q4Y15 2 2015Q4Y15 3 2015 Thanks in Advance |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2014-06-30 : 05:11:51
|
[code]DECLARE @Sample TABLE ( [Quarter] CHAR(5) NOT NULL, [Month] VARCHAR(8) NOT NULL, [Year] SMALLINT NOT NULL );INSERT @Sample ( [Quarter], [Month], [Year] )VALUES ('Q1Y16', '4,5,6' , 2015), ('Q2Y15', '7,8,9' , 2014), ('Q2Y16', '7,8,9' , 2015), ('Q3Y15', '10,11,12' , 2014), ('Q4Y15', '1,2,3' , 2015);-- SwePesoWITH cteSource([Quarter], [Month], [Year])AS ( SELECT [Quarter], REPLACE([Month], ',', '.') AS [Month], [Year] FROM @Sample)SELECT s.[Quarter], f.[Month], s.[Year]FROM cteSource AS sCROSS APPLY ( VALUES (CAST(PARSENAME(s.[Month], 3) AS TINYINT)), (CAST(PARSENAME(s.[Month], 2) AS TINYINT)), (CAST(PARSENAME(s.[Month], 1) AS TINYINT)) ) AS f([Month]);[/code] Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
|
|
|