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 Format

Author  Topic 

sford
Starting Member

4 Posts

Posted - 2007-03-28 : 12:14:50
I am pretty sure this question has been ask about a thousand times, but I couldn't figure out how to search the forum, and I didn't see what I needed in the FAQ...

In SQL Server... How do I convert a DateTime to a varchar that looks like this...

2007-Mar

That is I want a 4 character year followed by a dash followed by a 3 character month... NO DAY, NO TIME...

convert(varchar, date, ???)

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-03-28 : 12:19:30
[code]select convert(varchar(4), datepart(year, getdate())) + '-' + left(datename(month, getdate()), 3)[/code]


KH

Go to Top of Page

spejbl
Starting Member

28 Posts

Posted - 2007-03-28 : 13:08:13
or
DECLARE @d DATETIME
SET @d=GETDATE()
SELECT CONVERT(VARCHAR(4),@d,102)+'-'+CONVERT(VARCHAR(3),@d,0)


kb
http://kbupdate.info/ | http://suppline.com/
Go to Top of Page

sford
Starting Member

4 Posts

Posted - 2007-03-28 : 14:51:39
Thanks KH & KB...
Both solutions work well!
smf
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-28 : 14:56:39
This is shorter
SELECT STUFF(CONVERT(VARCHAR, CURRENT_TIMESTAMP, 107), 4, 5, '-')


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-28 : 14:59:33
quote:
Originally posted by spejbl

or
DECLARE @d DATETIME
SET @d=GETDATE()
SELECT CONVERT(VARCHAR(4),@d,102)+'-'+CONVERT(VARCHAR(3),@d,0)



SELECT CONVERT(VARCHAR(5), @d, 120) + CONVERT(VARCHAR(3), @d, 107)


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-03-28 : 15:11:35
*sigh* ....

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page
   

- Advertisement -