0

The conversion of a date data type to a datetime data type resulted in an

out-of-range value.

The statement has been terminated.

I just need to convert a date field to a date time format.

WorkSmarter
  • 3,738
  • 3
  • 29
  • 34
Dragon
  • 85
  • 9

2 Answers2

1

Example of what the code would look like to convert from date to datetime2

DECLARE @d1 date;
SET @d1 = GETDATE()

-- When converting date to datetime the minutes portion becomes zero.
SELECT CAST (@d1 AS datetime2) AS [date as datetime2]

For more information about cast and convert, see the Microsoft reference.

WorkSmarter
  • 3,738
  • 3
  • 29
  • 34
1

You probably get out of range exception because Date supports a date range between
January 1, 1 and December 31, 9999 , while DateTime supports a date range only between
January 1, 1753 to December 31, 9999.

Want to know Why 1753? Read this. (Recommended reading for anyone that likes trivia items)

Try converting to Datetime2 instead of Datetime, this should be OK since Datetime2 supports a date range similar to date.

the conversion can be done simply by using CAST:

SELECT CAST(YourDateTypeColumn As Datetime2)
FROM YourTable
Community
  • 1
  • 1
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121