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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Change date to int

Author  Topic 

coolerbob
Aged Yak Warrior

841 Posts

Posted - 2008-05-21 : 11:02:42
I want to convert "2007-01-27 14:13:01.223" to 20070127 (an integer representing the day). I could concatenate the calls to YEAR, MONTH DAY but then I would have to cater for the possibility that MONTH or DAY may return me one char instead of two; then I'll have to pad it manually.
Is there a shorter way to write such a query?

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-05-21 : 11:07:31
SELECT CONVERT(CHAR(8), GETDATE(), 112)


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

coolerbob
Aged Yak Warrior

841 Posts

Posted - 2008-05-21 : 11:09:24
Thanks! I knew I'd seen it somewhere before!
Just for interest sake, it looks a little bit more tricky if I wanted it the other way round 27012007. But not much:

select replace(convert(char(10), @a, 105),'-','')
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-05-22 : 07:42:10
Also if you use front end application, consider formation there

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -