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
 DATE?

Author  Topic 

Amber_Deslaurier
Starting Member

40 Posts

Posted - 2010-07-01 : 20:27:45
Hi,

I have a field that has digits...

20100517
19990612
19851231

etc.. the current format is YEAR MONTH DATE

but I need it to be:

2010-05-17
1999-06-12
1985-12-31

how do i convert the output? Thanks,

Amber

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-07-02 : 00:06:43
What's the data type? Int or varchar?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-07-02 : 00:52:20
this will works for both in or varchar

select convert(varchar(10), convert(datetime, convert(varchar(10), thefield)), 121)


If it is varchar, you can remove the extra most inner convert()

Or you can also use stuff() to insert the dash in between the number / strings


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-07-02 : 02:54:17
You should always use proper DATETIME datatype and leave formation at front end application

Madhivanan

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

Amber_Deslaurier
Starting Member

40 Posts

Posted - 2010-07-02 : 17:51:57
Thank you! Yes I did find out that you were right... I had to leave the conversion local... tried it on the server but had bytes errors or something..
Go to Top of Page
   

- Advertisement -