tags:

views:

34

answers:

3

Hi All,

How can I convert GETDATE() into a string like so: '2010-10-15'

-rod.

+2  A: 

Here a complext way to do it:

Select Convert(char(4),DATEPART(yy,GetDate())) + '-' + convert(char(2),DATEPART(mm,GetDate())) + '-' + Convert(char(2),DATEPART(dd,GetDate()))

An easier way is:

Select Convert(VARCHAR(10), GetDate(), 120)

You might want to take a look at the T-SQL Convert function. It allows you to format dates in many pre-defined ways:

http://msdn.microsoft.com/en-us/library/ms187928.aspx
Randy Minder
+4  A: 
SELECT CONVERT(VARCHAR(10), GETDATE(), 120)

By setting the varchar length, you can effectively truncate unwanted portions of the DateTime

CAST and CONVERT (Transact-SQL)

Brad
A: 

Here is another way to do it, SELECT REPLACE(CONVERTrt(varchar(10),GETDATE(),111)'/','-')

TRAD
it contains excessive 'rt' in CONVERTrt and missing virgule before '/'
vgv8