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

About the char data type to a datetime data type conversion error

There are some times where database applications give the following error when trying to convert strings to the datetime format in SQL Server: The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

 

How to resolve the above conversion error

This error is related to the default language and consequently the dateformat used by the corresponding SQL Server login.

There are two approaches for avoiding/resolving this issue.

 

Approach 1: Modify your T-SQL Scripts & Application Code

The first approach is to include the necessary logic in your database application in order not to depend on the default language setting, but use the ISO date format which is: yyyy-mm-dd hh:nn:ss.

 

Approach 2: The Default Language Setting

The second approach is to change the default language setting for the specific SQL Server login to “us_english”.

By executing the following script in SQL Server you get a list with all the logins and among others, the default language names used by these logins. The relevant column name in the results of this script is the “DefLangName”:

--Query that changes the default language to "us_english" for a given SQL Server login:
use [master];
GO
EXEC sp_helplogins
GO

 

If the default language name for the specific login is different than “us_english”, and your application’s code does not explicitly handle these date-conversion issues, there is the possibility of getting the conversion error when executing your application. You can easily change this setting to “us_english” by executing the below script:

--Query that changes the default language to "us_english" for a given SQL Server login:
use [master];
GO
ALTER LOGIN "LOGIN_NAME" WITH DEFAULT_LANGUAGE = us_english;
GO

* Note 1: You have to change the “LOGIN_NAME” string to the desired login name for which you would like to change its default language setting.

* Note 2: You can also change this setting through SQL Server Management Studio.

 

Concluding Remarks

As a concluding remark, the safest way to handle date format data in database applications, is to setup the default language setting for the relevant SQL Server login to “us_english” and use parameterized queries in your source code. To this end, the date formatting will be automatically handled thus avoiding possible conversion errors.

Hope this helps!

 

Learn more tips like this! Get the Online Course!

Check our online course titled “Essential SQL Server Administration Tips” (special limited-time discount included in link).

Learn essential hands-on SQL Server Administration tips on SQL Server maintenance, security, performance, integration, error handling and more. Many live demonstrations and downloadable resources included!

Essential SQL Server Administration Tips - Online Course with Live Demonstrations and Hands-on Guides
Essential SQL Server Administration Tips (Lifetime Access).

Enroll Now with Discount!

 

Featured Online Courses:

 

Related Error Messages and ways to Resolve them:

 

Check out our latest software releases!

Subscribe to our newsletter and stay up to date!

Easily generate snippets with Snippets Generator!

Secure your databases using DBA Security Advisor!

Convert static T-SQL to dynamic and vice versa with Dynamic SQL Generator.

 

Rate this article: 1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)

Loading...

Reference: SQLNetHub.com (https://www.sqlnethub.com)

© SQLNetHub

 

1 thought on “The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value”

  1. Just as a sidenote to this, i'm running SQL Server 2005 SP2 (with the language of the sql account set to British English). I had issues around using the ISO format YYYY-MM-DD. I got the "conversion of char data type" exception with that format when the day was > 12. When changing the language of the sql account to Engligh (us_english) the same query went through fine. However, when i changed my code to format the date into YYYYMMDD the query worked with both languages, so i don't know if this is a SQL bug or what, but i'm using the latter format now.

Comments are closed.