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
 Other Forums
 SQL Server 6.5 \ SQL Server 7.0
 SQL Statement help

Author  Topic 

Chadzuk
Starting Member

6 Posts

Posted - 2007-04-27 : 18:16:12
Hello all, I was wondering if someone might be able to assist me with how to code something. I need to concatenate a date into a YYYYMMDD format however from the database I am pulling it from the year, month, and day are all in there own fields. This isn't difficult to deal with however the catch is if the month is 1 character I need to add a 0 to the front of it, same with the day. The CASE statement to do this has me confused. For example.

Year = 2007
Month = 2
Day = 8

I need to take these out of the seperate fields and make it look like "20070208" in a new field. If the month or day is 2 characters I don't need to add the zero to the front. I also have to do a CAST on each field when combining them or it will add them together instead of concatenating them. This is the first step to a complex statement but I think if I can get this I can figure the other part out. Any help anyone can give would be wonderful.

Thank you in advance.

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2007-04-27 : 18:25:41
You mean
 select '2007' + RIGHT(('00' + '2'),2) + right(('00' + '8'),2) 



Dinakar Nethi
SQL Server MVP
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-04-28 : 01:38:27
Also see http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=22339

SELECT dateadd(month, 12 * YearColumn - 22801 + MonthColumn, DayColumn - 1)


Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -