4

How do I convert a string to a date type in SQL Server 2008 R2?

My string is formatted dd/mm/yyyy

I tried this

SELECT CAST('01/08/2014' AS DATE)

But that does the cast in mm/dd/yyyy format.

1
  • 2
    Take a look at the documentation for convert(). Commented Jul 22, 2014 at 1:21

5 Answers 5

10

You need the convert function, where you can specify a format code:

select convert(datetime, '01/08/2014', 103)

The 103 means dd/mm/yyyy. See the docs.

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

Comments

5

Dateformat.

SET DATEFORMAT DMY ;
SELECT cast('01/08/2014' as date) ;

Convert.

SELECT convert(date, '01/08/2014', 103 ) ;

And for completeness, SQL Server 2012 and later has the following.

SELECT parse('01/08/2014' as date using 'en-NZ' ) ;

1 Comment

I just realised that the asker and both answerers of this question are kiwis. Ka pai!
0

TO_DATE function in oracle is used to convert any character to date format.

for example :

SELECT to_date ('2019/03/01', 'yyyy/mm/dd')
FROM   dual;

CONVERT function in SQL SERVER is used to converts an expression from one datatype to another datatype.

for example:

SELECT CONVERT(datetime, '01/08/2014', 103) date_convert;

I hope this will help you.

1 Comment

The OP is about SQL Server. Pointing out the Oracle function for that task does not answer their question
0

You have to use CAST before use FORMAT_DATE function. Please, see bellow.

SELECT FORMAT_DATE(CAST(yourcolumn AS date), 'MM/dd/yyyy') AS DateOfBirth

Hope it helps!

Comments

-2

You can convert string to date easily by SELECT CAST(YourDate AS DATE)

Comments

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.