Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 I need this output in 1 column not 4!!

Author  Topic 

Rasta Pickles
Posting Yak Master

174 Posts

Posted - 2012-05-02 : 13:15:27
I'm still learning and I know my code is probably sloppy but any advice appreciated.


DECLARE @DateTest AS TABLE (Datex DATE)
DECLARE @DURATION INT
SET @DURATION = 0
WHILE (@DURATION > -1999)
BEGIN
INSERT INTO @DateTest
SELECT DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) + @DURATION, 0) AS Datex
SET @DURATION = @DURATION - 1
END

SELECT DAY (Datex) AS DAYNO
,MONTH (Datex) AS MONTHNO
,YEAR (Datex) AS YEARNO
,CASE WHEN (MONTH (Datex) IN (1,2,3)) THEN CAST (YEAR (Datex) AS VARCHAR(6)) + '01' END AS QUARTERNO
,CASE WHEN (MONTH (Datex) IN (4,5,6)) THEN CAST (YEAR (Datex) AS VARCHAR(6)) + '02' END AS QUARTERNO
,CASE WHEN (MONTH (Datex) IN (7,8,9)) THEN CAST (YEAR (Datex) AS VARCHAR(6)) + '03' END AS QUARTERNO
,CASE WHEN (MONTH (Datex) IN (10,11,12)) THEN CAST (YEAR (Datex) AS VARCHAR(6)) + '04' END AS QUARTERNO

FROM @DateTest;

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-05-02 : 13:28:41
What output do you need in 1 column vs. 4 columns? The quarter number? That's easy:
SELECT DAY (Datex) AS DAYNO
,MONTH (Datex) AS MONTHNO
,YEAR (Datex) AS YEARNO
,STR(DATEPART(year,Datex),4,0) + REPLACE(STR(DATEPART(q,Datex),4,0), ' ', '0') AS QUARTERNO
FROM @DateTest;

Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2012-05-02 : 13:48:48
OR this

SELECT datex,convert(char(6),YEAR(datex)*100+DATEPART(q,datex)) as QUARTERNO
from @DateTest


Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

Rasta Pickles
Posting Yak Master

174 Posts

Posted - 2012-05-02 : 13:59:25
Thanks both, I am humbled and feel a little stupid for asking what was obviously an easy question.

Still, we all have to start somewhere :-)
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2012-05-02 : 14:33:38
Don't feel stupid. We're all here to help each other.
Go to Top of Page
   

- Advertisement -