How to Find the Date Format Being Used in the Current Session in SQL Server (T-SQL)

When using SQL Server, your current connection includes a number of options that determine things like the language, date formats, etc. These could be set at whatever the default is, but they can also be overridden during the session by using a SET statement.

The date format affects the interpretation of character strings as they are converted to date values for storage in the database. When the language is set using SET LANGUAGE, the date format setting is implicitly set accordingly. This can be explicitly overridden with the SET DATEFORMAT statement.

In any case, you can find the current date format by using the DBCC USEROPTIONS command. This command returns the SET options that have been set for the current connection.

Continue reading

How to Find a User’s Default Language in SQL Server (T-SQL)

In SQL Server, you can find out the default language for a given user by querying the sys.server_principals system catalog view.

This view contains a row for every server-level principal. It contains information such as the principal’s name, type, create/modify date, default database, default language, etc. A principal is an entity that can request SQL Server resources.

Continue reading

How to Find the Date Formats Used for a Specific Language in SQL Server (T-SQL)

When working with dates in SQL Server, it’s easy to get tripped up with different date formats. For example, someone from the US might consider 01/07/2018 to mean the 7th of January, but someone from the UK might consider it to mean the 1st of July.

In many cases you might not even know which date format is used for any particular language/culture. Fortunately, SQL Server stores this information in its resource database, and you can retrieve it by using either of the two methods below.

Continue reading

List of All Languages and Associated Date Formats in SQL Server 2017

The following is a list of all languages installed with the SQL Server 2017 database engine. This includes the language, along with its associated date formats.

This list can be returned by executing the sp_helplanguage stored procedure, or by querying the sys.syslanguages view directly. The list below contains the column names and their corresponding values for each language.

Continue reading

3 Ways to Get the Language of the Current Session in SQL Server (T-SQL)

When a new login is created in SQL Server, it is assigned a default language. This language is used for system messages and date/time formats. This language will be used as the default language whenever that login connects to SQL Server (but it can also be changed to a different language within the session).

If you ever wish to find out the language that’s assigned to the current session, you can run one of the options on this page.

Continue reading

PARSE() vs CAST() vs CONVERT() in SQL Server: What’s the Difference?

Perhaps you’ve encountered the T-SQL PARSE(), CAST(), and CONVERT() functions when working with SQL Server and wondered what the difference is. All three functions seem to do the same thing, but there are subtle differences between them.

In this article I aim to outline the main differences between these functions.

Continue reading

PARSE() vs TRY_PARSE() in SQL Server: What’s the Difference?

In SQL Server, the PARSE() and TRY_PARSE() functions are used for translating a value into another data type. They essentially do the same thing, with one exception; how they deal with errors.

If PARSE() fails when attempting to parsing to a different data type, it will return an error. If TRY_PARSE() fails, it will return NULL.

Continue reading

6 Ways to Convert a String to a Date/Time Value in SQL Server

If you need to convert a string into a date/time value in SQL Server, you have a number of options. In this post I outline six T-SQL functions that allow you to do this.

The six functions are:

  • CAST()
  • CONVERT()
  • PARSE()
  • TRY_CAST()
  • TRY_CONVERT()
  • TRY_PARSE()

Below are example of how you can use these functions to convert a string to a date/time data type.

Continue reading

How to Convert a String to a Date/Time in SQL Server using PARSE()

If you work with SQL Server, chances are you’ve used at least one of the CONVERT() or CAST() functions to convert from one data type to another. If you’ve ever encountered an error while trying to convert a string to a date/time data type, the PARSE() function could be what you need.

For example, if you have a string like say, Fri, 20 Jul 2018, the CONVERT() or CAST() functions will throw an error. But the PARSE() function will handle it without a problem.

The PARSE() function returns the result of an expression, translated to the requested data type in SQL Server. So you can use it to “translate” your string value into a date/time data type (such as date, datetime, datetime2, etc).

Continue reading