6

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

-rod.

4 Answers 4

6
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)

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

2

Try below code which will convert your date for specific format

SELECT convert(varchar,Getdate(),23) -- YYYY-MM-DD
SELECT convert(varchar,Getdate(),105) -- DD-MM-YYYY
SELECT convert(varchar,Getdate(),110) --MM-DD-YYYY

1 Comment

23 is not a documented option for convert, the others aren't what the OP asked for and the accepted answer from 2010 already has link for the documentation.
0

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

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.