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 2000 Forums
 Transact-SQL (2000)
 date format

Author  Topic 

jung1975
Aged Yak Warrior

503 Posts

Posted - 2005-11-10 : 15:24:00
How can I convert a datetime field( 100000 records) to char?


For example, I would like to convert date time to char(8)
Birthdate
1975-01-16 00:00:00.000 --> 19750116

I've tried

select cast(replace(birthdate,'-', '') as char(8) ) ,but it didn't work.


jhocutt
Constraint Violating Yak Guru

385 Posts

Posted - 2005-11-10 : 15:28:04
declare @a as datetime
set @a = '1975-01-16 00:00:00.000'
--> 19750116

select @a, Cast(Year(@a)as varchar)+right('00'+Cast(Month(@a) as varchar),2)+right('00'+Cast(Day(@a) as varchar),2)



"God does not play dice" -- Albert Einstein
"Not only does God play dice, but he sometimes throws them where they cannot be seen."
-- Stephen Hawking
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2005-11-10 : 15:29:24
SELECT CONVERT(char(8), Birthdate, 112)

Check out CONVERT in BOL for details. Style 112 gives you yyyymmdd format.

Tara Kizer
aka tduggan
Go to Top of Page

jhocutt
Constraint Violating Yak Guru

385 Posts

Posted - 2005-11-10 : 15:35:28
Looks like I always do things the hard way. :)

"God does not play dice" -- Albert Einstein
"Not only does God play dice, but he sometimes throws them where they cannot be seen."
-- Stephen Hawking
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-11-11 : 00:18:19
Where do you want to show these converted Dates?
If you are using Front End Application or Reports, you can also use Format function there to format the date

Madhivanan

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

- Advertisement -