Create a Database in SQL Server (T-SQL)

Many developers and database administrators create databases using GUI tools such as SQL Server Management Studio (SSMS), Azure Data Studio, etc.

But it can be much quicker to create databases using SQL. This is especially true if you have various environments that you need to recreate the same database on. You can run the same script against each environment, and the database will be created within seconds each time.

In SQL Server, we do this with T-SQL. T-SQL stands for Transact-SQL, and it is SQL Server’s extension to SQL.

Below is an example of using T-SQL to create a database in SQL Server.

Continue reading

Create a Relationship in SQL

In SQL, you create a relationship by creating a foreign key constraint.

More specifically, you have a parent table and a child table. The parent contains the primary key, and the child table contains a foreign key that references the primary key of the parent table.

When you use SQL to create a relationship, you can create the relationship at the time you create the table, or you can create it later (by altering the table). This article covers both scenarios.

Continue reading

Change the Language for the Current Session in SQL Server

You may know that when you connect to SQL Server, the language for that session is usually determined by your login. When a login is created, it is assigned a default language.

The session language determines the datetime formats and system messages.

While you can certainly change the default language for a login, you can also override the default language within a session if you need to. You can toggle back and forth between languages if required. Or you could even open two separate connections and apply a different language to each of them.

This article explains how to change the language within a session.

Continue reading

Change the Date Format For the Current Session in SQL Server

Whenever you connect to SQL Server, a bunch of default settings are applied to your session. These include the language and the date format settings.

The date format is normally determined by your default language. For example, if your default language is us_english, then the default date format will probably be mdy, and the first day of the week will be day 7 (Sunday).

If you change your language, you the date format will implicitly be updated accordingly.

However, you still have the option of changing the date format without changing the language. To do this, you can use SET DATEFORMAT.

Continue reading

Convert a Date to Another Time Zone in SQL Server

The AT TIME ZONE clause was introduced in SQL Server 2016 to convert a date into a datetimeoffset value in a target time zone.

This function is similar to some other T-SQL functions, such as SWITCHOFFSET() and TODATETIMEOFFSET(), however, the AT TIME ZONE clause allows/(requires) you to specify the time zone offset by name, instead of an actual offset value.

This article explores how AT TIME ZONE works, and explains its benefits when compared to the other functions mentioned.

Continue reading