T-Sql 101

T-SQL 101: 72 Extracting date and time components in SQL Server T-SQL using DATEPART

In previous posts, I mentioned that you can easily extract the year, month, and day from dates and times, but you might want other components. That’s what the DATEPART() function does.

You can see the output from the query above here:

To use this function , we put the interval that we’re after and then the value we want to find it out from. So if you look at this one, I said I wanted the day from 28th of February 2019. The day is 28.

2020-06-01

T-SQL 101: 71 Subtracting dates and times in SQL Server T-SQL with DATEDIFF and DATEDIFF_BIG

When you’re writing T-SQL code, it’s also common to need to work out how far apart two dates are, or it could even be dates and times.

The DATEDIFF() function does that. In this case I’m asking how many days is it from 28th of February 2019 to 31st of July 2019?  The answer is:

So it says the difference is 153 days. Now we could have used any of those other intervals for that. I mentioned them before in this post.

2020-05-25

T-SQL 101: 70 Adding and subtracting intervals in SQL Server T-SQL using DATEADD

When you’re writing T-SQL, you might also need to add intervals to dates, perhaps add days or subtract days and so on. The DATEADD() function is the one that we can add or subtract intervals to the date and time.

Now in the example shown here, what I’ve said is in 20190228 or  28th of February 2019. I want to add on 12 days. To subtract 12 days, I would have just put -12 instead of 12. You can see the output here:

2020-05-18

T-SQL 101: 69 Extracting date components in SQL Server T-SQL with DAY, MONTH, and YEAR

When you’re writing T-SQL code, it’s really common to need to extract components of a date, and the most common need is for year, month, and day. So T-SQL has separate functions just for that.

You can see the output from this query here:

The YEAR(), MONTH(), and DAY() functions take a date (or datetime, datetime2, smalldatetime) as input and return an integer for the year, month, or day.

2020-05-11

T-SQL 101: 68 Current date and time in SQL Server T-SQL with SYSDATETIME and SYSDATETIMEOFFSET

It’s really common to need to know the current date and time in T-SQL code. Ever since the beginning of the SQL Server product, we’ve had a function called GETDATE(). It returns the current date and time of the server as a datetime value.

In SQL Server 2008, we got new higher precision data types, and so new current date and time functions were added as well.

SYSDATETIME() is a function that returns the current date and time as a datetime2 value. And SYSDATETIMEOFFSET() returns the timezone offset for the server as well.

2020-05-04

T-SQL 101: 67 Literal date and time values in SQL Server T-SQL

One of the challenges when you go to write dates is that there’s no standard format in SQL Server for how to write a date. Some other languages have a specific format for writing dates. In T-SQL, we have to write it as a string.

Because there’s no special way to write dates, it’s very important to come up with a format that will work properly all the time. The example I’ve got here for OrderDate is 20190128. That is a safe format that always works. SQL Server will interpret it as YYYYMMDD every time, no matter what regional or language settings you have configured.

2020-04-27

T-SQL 101: 66 Date and time data types in SQL Server T-SQL

While there are some other data types that I’d like to see added (for example a month data type), SQL Server has quite a rich set of date and time data types. Some of these were older data types and some new ones were introduced in SQL Server 2008.

An important one that was added in 2008 is the date data type. It’s just a date. It has no time associated with it and it covers year from 1 to 9999. It says that it’s based on the Gregorian calendar (i.e. our current calendar) but that’s a bit ambitious given it didn’t exist back in the year 1. But for the range of dates that we would work with, this will always work fine.

2020-04-20

T-SQL 101: 65 Formatting strings in SQL Server T-SQL using FORMAT

In upcoming blog posts, I’ll show you how to change from one data type to another. I’ll show you how to use CONVERT to output date/time values to string formats.

If you look at the web page for CAST and CONVERT, you’ll notice there are many styles for conversion. For example, the US format is called 101 if it has a 4 digit century. But it’s called 1 if you only have a 2 digit century. Another very common one is the British/French one and that’s 103 for a 4 digit century or just 3 for a 2 digit century.

2020-04-13

T-SQL 101: 64 Changing the offset of a datetimeoffset value in SQL Server T-SQL using SWITCHOFFSET

The datetimeoffset data type was added in SQL Server 2012 and allowed us to not only store date and time values, but to also store a time zone offset (from -14 hours to +14 hours). When you’re using this data type though, you might need to change a value from one time zone offset to another.  That’s the purpose of the SWITCHOFFSET function.

Look at the following query:

SYSDATETIMEOFFSET is being used to return the current date, time, and time zone offset for the server, but we’re also asking for the equivalent with a +7 time zone offset. (That was the current time in Seattle when the query was run). You can see the result here:

2020-04-06

T-SQL 101: 63 Adding offsets to dates and times in SQL Server T-SQL using TODATETIMEOFFSET

The datetimeoffset data type was added in SQL Server 2012 and allowed us to not only store date and time values, but to also store a time zone offset (from -14 hours to +14 hours). When you’re using this data type though, you often have the datetime value and the offset separately, and need to combine them together to make a datetimeoffset value.

The TODATETIMEOFFSET function takes a datetime2 value (higher precision datetime) and a time zone offset, and returns a datetimeoffset data type.

2020-03-30