Mazhar
Mazhar

Reputation: 3837

TSQL Change String to DATETIME datatype - SQLSERVER 2008 R2

Is there a way to convert this NVARCHAR "datetime" ("28/08/2014 08:45:50") to a proper DATETIME datatype (2014-08-28 08:45:50)?

or perhaps even the other away around

2014-08-28 08:45:50 to "28/08/2014 08:45:50"

I've tried to CAST or CONVERT to DATETIME but I get this error

The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.

Thanks in advance

Upvotes: 0

Views: 40

Answers (1)

Donal
Donal

Reputation: 32713

This is because your date format is mdy. You can changed this by setting the DATEFORMAT to dmy. For example, try this:

SET DATEFORMAT dmy
SELECT CAST('28/08/2014 08:45:50' AS DATETIME)

Upvotes: 2

Related Questions