DATETIME2FROMPARTS() Examples in SQL Server (T-SQL)

In SQL Server, the DATETIME2FROMPARTS() function works similar to the DATETIMEFROMPARTS() function, except that it accepts 8 arguments and returns a datetime2 value. The DATETIMEFROMPARTS() function on the other hand, accepts just 7 arguments and it returns a datetime value.

Basically, you provide all the date/time parts and this function will return a datetime2 value based on the parts you provide.

Continue reading

DATETIMEFROMPARTS() Examples in SQL Server (T-SQL)

In SQL Server, the DATETIMEFROMPARTS() function works just like the DATEFROMPARTS() function, except that it also returns the time portion of the date.

The DATETIMEFROMPARTS() function accepts seven arguments, each representing a different part of the date/time. The function then returns a datetime value from the given parts.

Continue reading

DATEFROMPARTS() Examples in SQL Server (T-SQL)

You can use the T-SQL DATEFROMPARTS() function in SQL Server to return a date value that maps to the date parts that you provide as integer expressions.

The way it works is, you provide three values (one for each of the year, month, and day values), and SQL Server will return a date value based on the values you provide.

Examples below.

Continue reading

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

How to Set the Default Language for All New Logins in SQL Server (T-SQL)

Each time you create a new login in SQL Server, you have the choice of assigning a default language to that login. If you don’t do this, the login will use the default language as specified in the default language server configuration option.

This article demonstrates how to set the default language server configuration option in SQL Server, using T-SQL.

Continue reading

How to Change the Current Date Format in SQL Server (T-SQL)

When you connect to SQL Server, usually the date format is determined by your language. The default language for a session is the language for that session’s login, unless overridden on a per-session basis by using the Open Database Connectivity (ODBC) or OLE DB APIs.

The date format setting affects the interpretation of character strings as they are converted to date values for storage in the database. It does not affect the display of date data type values that are stored in the database or the storage format.

Despite the fact that the your session’s language determines the date format, you can override the date format if required. For example, if your language is us_english, the date format will be mdy (so that 07/01/2018 represents the 1st of July and not the 7th of January). You can change this so that the date format is dmy (or any other format) while the language remains us_english.

You can use T-SQL to explicitly set the date format of the current session by using the SET DATEFORMAT statement.

Continue reading