ODBC Scalar Functions for Date and Time in SQL Server (T-SQL Examples)

You can use ODBC scalar functions when running T-SQL statements in SQL Server. There are various types of ODBC scalar functions, including string functions, numeric functions, system functions, and of course, date/time functions (also referred to as date, time, and interval functions).

Below are examples of the date, time and interval functions you can use in SQL Server.

Continue reading

SYSUTCDATETIME() vs GETUTCDATE() in SQL Server: What’s the Difference?

SYSUTCDATETIME() and GETUTCDATE() are two T-SQL date/time functions available for use in SQL Server.  These functions return the date and time of the computer on which the instance of SQL Server is running. Both functions return the date and time as UTC time (Coordinated Universal Time).

So, both functions do the same thing. Almost.

Here’s how these two functions differ:

  • GETUTCDATE() returns its value as a datetime value.
  • SYSUTCDATETIME() returns its value as a datetime2 value.

This means that SYSUTCDATETIME()  provides more seconds precision. The datetime2 data type also has a larger range than datetime.

Continue reading

SYSDATETIME() vs GETDATE() in SQL Server: What’s the Difference?

SQL Server has a bunch of functions that return the current date/time. Two of these are the GETDATE() and SYSDATETIME() functions.

At first glance, these two functions appear to do the same thing – get the current date and time from the operating system of the computer on which the instance of SQL Server is running.

However, there’s a subtle difference between the two.

Continue reading

3 Ways to Get the Day Name from a Date in SQL Server (T-SQL)

In SQL Server, just as you can use T-SQL to get the month name from a date, you can also use the same methods to get the day name. By day name, I mean Monday or Tuesday for example, and not the date number or number of the day of the week (which you can also get if you need it).

Here are three ways to return the day name from a date in SQL Server using T-SQL.

Continue reading

3 Ways to Get the Month Name from a Date in SQL Server (T-SQL)

When using SQL Server, you have a few different options when you need to return the month name from a date using T-SQL. By month name, I’m not talking about the month number (such as 07). I’m talking about the full name of the month (such as July).

For example, when given a date of 2018-07-01, you want July to be returned.

This article presents three ways to return the month name from a date in SQL Server using T-SQL.

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