Published on

August 25, 2016

Understanding SQL Server Datetime Conversion

As a SQL Server professional, I often come across interesting challenges and puzzles that require me to dig deep into the inner workings of the database. Recently, a customer sent me a subset of their database code with some data, asking me to investigate why their queries were running slow and suggest improvements. Little did I know that this assignment would lead me to discover a fascinating aspect of datetime conversion in SQL Server.

One particular section of the code caught my attention – it involved converting datetime values. The stored procedure was attempting to convert a datetime value in a rather unusual format: ’10 listopad 2015′. When I ran the same code on my machine, I encountered an error: “Conversion failed when converting date and/or time from character string.”

Curious to understand why this code worked on the customer’s machine but not on mine, I reached out to them for clarification. They promptly responded with a screenshot that revealed the answer to this puzzle.

Upon closer inspection, I realized that the datetime format used in the code was specific to the Croatian language. This explained why it worked on the customer’s machine, as their SQL Server instance was configured to recognize Croatian datetime formats. However, on my machine, which was set to a different language, the conversion failed.

So, what can be done to resolve this issue? Well, SQL Server provides various options and settings that can be adjusted to handle different datetime formats. One option is to change the language setting for the SQL Server instance to Croatian, which would allow the conversion to succeed. However, this may not be a practical solution in all scenarios.

Alternatively, we can modify the code to explicitly specify the datetime format using the CONVERT function. In this case, we can use the format code ‘113’ to indicate the Croatian datetime format. By rewriting the code as follows: SELECT CONVERT (DATETIME, ’10 listopad 2015′, 113); we can ensure that the conversion is performed correctly, regardless of the language settings.

Understanding the intricacies of datetime conversion in SQL Server is crucial for ensuring accurate and efficient data processing. By being aware of the various options and settings available, we can overcome challenges like the one presented in this puzzle.

I hope you found this blog post insightful. Please feel free to leave a comment with your thoughts or any additional questions you may have. In a future blog post, I will reveal the correct answer to this puzzle and give credit to those who provided valid answers.

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.