{"id":189,"date":"2012-05-02T10:08:48","date_gmt":"2012-05-02T10:08:48","guid":{"rendered":"https:\/\/sqlserverlearner.azurewebsites.net\/2012\/05\/02\/the-conversion-of-a-varchar-data-type-to-a-datetime-data-type-resulted-in-an-out-of-range-value\/"},"modified":"2012-05-02T10:08:48","modified_gmt":"2012-05-02T10:08:48","slug":"the-conversion-of-a-varchar-data-type-to-a-datetime-data-type-resulted-in-an-out-of-range-value","status":"publish","type":"post","link":"https:\/\/sqlserverlearner.com\/2012\/05\/02\/the-conversion-of-a-varchar-data-type-to-a-datetime-data-type-resulted-in-an-out-of-range-value\/","title":{"rendered":"The conversion of a varchar data type to a datetime data type resulted in an out-of-range value."},"content":{"rendered":"

When you try to convert varchar datatype to datetime you would get this error when the value in varchar datatype does not represent the correct range of date.<\/p>\n

Example Queries:
\n[sql]
\nSELECT CAST( ‘2012-04-31’ AS DATETIME)
\n[\/sql]<\/p>\n

Result:
\n
\nMsg 242, Level 16, State 3, Line 1
\nThe conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
\n<\/font><\/p>\n

Other Sample queries:
\n[sql]
\nSELECT CAST( ‘2012-04-30 24:00:00.000’ AS DATETIME)
\n[\/sql]<\/p>\n

[sql]
\nSELECT CAST( ‘2012-04-30 00:60:00.000’ AS DATETIME)
\n[\/sql]<\/p>\n

[sql]
\nSELECT CAST( ‘2012-04-30 00:00:60.000’ AS DATETIME)
\n[\/sql]<\/p>\n

[sql]
\nSELECT CAST( ‘2012-04-30 24:00:00.000’ AS DATETIME)
\n[\/sql]<\/p>\n

Fix:
\n– Check if the values in the varchar data type are in the range of datetime datatype. Use this link to check the same: Range of datetime data type<\/a>
\n– Check if the values for month, day, hours, minutes, seconds, milliseconds are valid.<\/p>\n

Applicable to: SQL Server 2005, SQL Server 2008,SQL Server 2008 r2,SQL Server 2012, ASP, C#<\/p>\n","protected":false},"excerpt":{"rendered":"

When you try to convert varchar datatype to datetime you would get this error when the value in varchar datatype does not represent the correct range of date. Example Queries: [sql] SELECT CAST( ‘2012-04-31’ AS DATETIME) [\/sql] Result: Msg 242, Level 16, State 3, Line 1 The conversion of a varchar data type to a…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[128,1789],"tags":[1890,1891,1892,1893,1894,1895,1896],"class_list":["post-189","post","type-post","status-publish","format-standard","hentry","category-sql-errors","category-t-sql-code-examples","tag-datetime-data-type-resulted-in-an-out-of-range-value","tag-msg-242","tag-msg-242-level-16-state-3-line-1","tag-out-of-range-value-for-column-datetime","tag-range-of-datetime-data-type-resulted-in-an-out-of-range-value","tag-the-conversion-of-a-varchar-data-type-to-a-datetime-data-type-resulted-in-an-out-of-range-value-c","tag-the-conversion-of-a-varchar-data-type-to-a-datetime-data-type-resulted-in-an-out-of-range-value-asp"],"_links":{"self":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts\/189","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/comments?post=189"}],"version-history":[{"count":0,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts\/189\/revisions"}],"wp:attachment":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/media?parent=189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/categories?post=189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/tags?post=189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}