Petah
Petah

Reputation: 46050

Convert string to date in specific format

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.

Upvotes: 4

Views: 43313

Answers (5)

Stracke
Stracke

Reputation: 1

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!

Upvotes: 0

Hassan Abdelrahman
Hassan Abdelrahman

Reputation: 57

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.

Upvotes: 0

nayan
nayan

Reputation: 63

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

Upvotes: -2

Greenstone Walker
Greenstone Walker

Reputation: 1150

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' ) ;

Upvotes: 5

Blorgbeard
Blorgbeard

Reputation: 103467

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.

Upvotes: 10

Related Questions