DATABASE

SQL Server 2008 : T-SQL Tips and Tricks (part 1) - Date Calculations & Sorting Results with the GROUPING Function

5/17/2011 3:55:19 PM
The following sections provide some general tips and tricks to help you get the most from your T-SQL code.

Date Calculations

Occasionally, you may find that you need to start with a date value and use it to calculate some other date. For example, your SQL code might need to determine what date is the first day of the month or last day of the month. As you may know, working with the datetime data type in SQL Server can be a bit of a challenge. You probably already know how to use the datepart() function to extract specific components of a date (for example, year, month, day). You can then use those components along with a number of functions to calculate a date that you might need. This section provides some examples of algorithms you can use to generate some commonly needed date values.

The DATEDIFF function calculates the difference between two dates, where the difference is based on an interval, such as hours, days, weeks, months, years, and so on. The DATEADD function calculates a date by adding an interval of time to a date. In this case, the intervals of time are the same as those used by the DATEDIFFDATEADD and DATEDIFF functions to calculate specific dates requires thinking outside the box a bit to convert the date value into a date you need. You need to start thinking in terms of date intervals—for example, how many date intervals it is from the current date to the date you want to calculate, or how many date intervals it is from today to some other date, such as "2000-01-01", and so on. You use the DATEADD and DATEDIFF functions to calculate your desired date by determining the appropriate date intervals from the current date and then adding or subtracting intervals to arrive at the desired calculated date. Understanding how to use the various date intervals helps you more easily understand how to calculate the desired dates. function. Using the

Calculating the First Day of Month

Let’s look at a method for determining the first day of the month for a given date. To do this, you start out with the initial date. (In this example, you can use getdate()"1/1/1900". to work with the current system date and time.) The next step is to figure out the number of months between the given date and the date

Note

The value “1/1/1900” is the default for a date if an empty string ('') is used to represent a date.


You can use the DATEDIFF function to determine the number of months from "1/1/1900":

select DATEDIFF(mm,'',getdate())
go

-----------
1321

Now, using the number of months, you can add that result to "1/1/1900" to obtain the first day of the month for the given date:

select DATEADD(mm, DATEDIFF(mm,'',getdate()), '')

By adding the number of months between the given date and "1/1/1900" to "1/1/1900", you are able to arrive at the first day of the current month. In addition, the time portion of the calculated date is set to "00:00:00.000".

This technique for calculating a date interval between the current date and the year, "1900-01-01", and then adding the calculated number of intervals to "1900-01-01" can be used to calculate many different dates. The next four examples use the same technique to generate different dates based on the current date.

Calculating the First Day of the Year

You can use the year interval (yy) to display the first day of the year:

select DATEADD(yy, DATEDIFF(yy,'',getdate()), '')

Calculating the First Day of the Quarter

To calculate the first day of the current quarter, you use the quarter (qq) interval:

select DATEADD(qq, DATEDIFF(qq,'',getdate()), '')

Calculating Midnight for the Current Day

If you need to truncate the time portion for a datetime value so it reflects the current date at midnight, you can use the date interval (dd) to get the midnight time stamp for the desired date:

select DATEADD(dd, DATEDIFF(dd,'',getdate()), '')

Calculating Monday of the Current Week

You can use the week interval (wk) to calculate what date is Monday of the current week:

select DATEADD(wk, DATEDIFF(wk,'',getdate()), '')

Calculating Other Dates

As you have seen, by using simple DATEADD and DATEDIFF calculations, you can come up with many different dates that might be valuable. All the examples so far have only calculated the number of date intervals between the current date and "1/1/1900" and then added the appropriate number of intervals to "1900-01-01" to arrive at the calculated date. If you have to calculate other date values, you can use this calculation as the basis and then add or subtract additional intervals to come up with other useful dates.

For example, to calculate the last day of the previous month for a given date, you can use the following calculation to determine the first day of the current month and subtract a day from it:

select DATEADD(dd, -1, DATEADD(mm, DATEDIFF(mm,'',getdate()), ''))

You can perform a similar calculation to determine the last day of the previous year, based on the formula to calculate the first date of the current year for the given date:

select DATEADD(dd, -1, DATEADD(yy, DATEDIFF(yy,'',getdate()), ''))

What if you need to determine the last day of the current month for a given date? One way to do this is to calculate the first date of the next month and subtract one day from that. To calculate the first day of the next month, you can use the formula to calculate the first day of the current month and add one to the number of intervals returned by DATEDIFF when comparing the given date to "1/1/1900" to get the first day of the next month:

select DATEADD(mm, DATEDIFF(mm,'',getdate()) + 1, '')

Now that you have the first date of the next month, you simply subtract one day from it to get the last day of the current month:

select DATEADD(dd, -1, DATEADD(mm, DATEDIFF(mm,'',getdate()) + 1, ''))

Similarly, you can modify the formula to calculate the first day of the year to return the last day of the previous year:

select DATEADD(dd, -1, DATEADD(yy, DATEDIFF(yy,'',getdate()) + 1, ''))

Now, let’s try a little more advanced calculation: the first Monday of the current month. To find this, you start with the calculation for the Monday of the current week and modify it slightly. Rather than use getdate() as the date value, you use the calculation to get the first day of the month and add five days to it. Adding five days to the first day of the month ensures that you are in the first full week of the month (SQL Server treats Sunday as the first day of the week, so if the first day of the month was on a Monday, adding 5 days keeps you in the same week. If the first day is Tuesday or later, adding 5 days puts you into the next week). You can use the following calculation to get the first day of the month and add five days to it:

select DATEADD(dd, 5, DATEADD(mm, DATEDIFF(mm,'',getdate()), ''))

Now, you use this expression in place of the getdate() function in the calculation to get the date for Monday of the current week:

select DATEADD(wk, DATEDIFF(wk,'',
DATEADD(dd, 5, DATEADD(mm, DATEDIFF(mm,'',getdate()), ''))), '')



Tip

If you find yourself using any of these date calculations frequently, it might be a good idea to create one or more user-defined functions to encapsulate these calculations. It would save your having to reenter the sometimes complex formulas, which can be easily mistyped, leading to incorrect calculations.


Converting Dates for Comparison

Because the datetime data type contains both time and date components, searching for data rows matching a specific date only, excluding the time component, can sometimes be a bit tricky—especially when you consider that SQL Server stores time values only down to 3/1,000 second. For example, if you want to find all rows where the date is for a certain day, you have to perform a range search for all times within that day. Because a date without a time specified defaults to a time of midnight (00:00:00.000) for that date, the following query doesn’t return all matching rows if any of the data values contain a time other than midnight:

select title_id, pubdate from dbo.titles where pubdate = '2006-01-14'

To be sure to include all rows for a particular date, regardless of the time component stored, you could run a query similar to the following:

select title_id, pubdate from dbo.titles
where pubdate between '2006-01-14 00:00:00.0' and '2006-01-14 23:59:59.997'
go

title_id pubdate
-------- -----------------------
FI3599 2006-01-14 00:00:00.000



Now you might be wondering, why use a time of "2006-01-14 23:59:59.997" as the last time of the day? You do so because SQL Server stores datetime values only down to 3/1,000 second. If you enter a time of "2006-01-14 23:59:59.999", SQL Server rounds it up to "2006-01-15 00:00:00.000", and it actually matches any rows with that datetime value, as in this example:

select title_id, pubdate from dbo.titles
where pubdate between '2006-01-14 00:00:00.0' and '2006-01-14 23:59:59.999'

title_id pubdate
-------- -----------------------
FI3599 2006-01-14 00:00:00.000
FI5162 2006-01-15 00:00:00.000



This is one reason you have to be careful when performing date searches. Now you might be wondering why not just use the DATEDIFF function as in the following example:

select title_id, pubdate from dbo.titles
where datediff(day, pubdate, '2006-01-14') = 0
go

title_id pubdate
-------- -----------------------
FI3599 2006-01-14 00:00:00.000

Although this query returns the correct result, the use of the function on the pubdate column may prevent SQL Server from using any indexes that exist on the pubdate column to optimize the query, and it is likely to end up performing a table scan.  To help ensure that your queries are optimized effectively, you need to try to avoid using any functions or expressions on the column in the search argument, and you need to search against constant expressions.

Another way to write the preceding query would be to use the date calculations discussed previously in this section. For example, you could use the calculation to determine midnight of the desired date and use that as the inclusive lower bound, and you could use the calculation of midnight of the next day as the noninclusive upper bound and write a query similar to the following:

declare @date datetime
set @date = '2006-01-14'
select title_id, pubdate from dbo.titles
where pubdate >= DATEADD(dd, DATEDIFF(dd,'',@date), '')
and pubdate < DATEADD(dd, DATEDIFF(dd,'',@date) + 1, '')
go

title_id pubdate
-------- -----------------------
FI3599 2006-01-14 00:00:00.000

SQL Server 2008 introduces the date and time data types, as well as the datetime2 data type. The long-awaited date and time data types store just a date value or time value, respectively, making date-only or time-only comparisons much simpler. For example, the previous solution for finding all books published on a specific day can be simplified a bit using the date data type because there is no need to consider a time component:

declare @date date
set @date = '2006-01-14'
select title_id, pubdate from dbo.titles
where pubdate >= @date
and pubdate < DATEADD(dd, 1, @date)
go

If the pubdate column were defined using the date data type instead of datetime (reasonable because the time of publication of a book is irrelevant), the comparison becomes even simpler:

alter table titles drop constraint DF__titles__pubdate__103673A0
drop statistics titles.pubdate
alter table titles alter column pubdate date null
alter table titles
add constraint DF__titles__pubdate__103673A0
default getdate() for pubdate

go
declare @date date
set @date = '2006-01-14'
select title_id, pubdate from dbo.titles
where pubdate = @date
go

title_id pubdate
-------- -----------------------
FI3599 2006-01-14 00:00:00.000

The datetime2 data type stores the time value down to microseconds and avoids the 3/1,000 second rounding issue that was present with the datetime data type. For example, if you redefine the pubdate column using the datetime2 data type, you avoid the rounding issue and get a single row as expected by the following query:

alter table titles drop constraint DF__titles__pubdate__103673A0
alter table titles alter column pubdate datetime2 null
alter table titles
add constraint DF__titles__pubdate__103673A0
default sysdatetime() for pubdate
go
select title_id, pubdate from dbo.titles
where pubdate between '2006-01-14 00:00:00.0' and '2006-01-14 23:59:59.999999'
go

title_id pubdate
-------- -----------------------
FI3599 2006-01-14 00:00:00.000


Sorting Results with the GROUPING Function

When working with the CUBE or ROLLUP operator, SQL Server generates NULL values for the columns that are being rolled up to generate the aggregate values. When you are viewing the results, however, it can be difficult to determine whether the NULL value shown for a nonaggregate column is the result of a rollup or because the column itself contains a NULL value. Fortunately, SQL Server provides the GROUPING function, which you can use to distinguish between real NULLNULL values that represent a rollup of all values for a column in the result set. values and

The GROUPING function returns 1 when the value is grouped and 0 when the column contains a NULL value.

In Listing 1, the GROUPING function is used to replace NULL values for the rolled-up columns with ALL.

Listing 1. Using the GROUPING Function
SELECT CASE when GROUPING(type) = 1 then 'ALL'
else isnull(type, 'Other')
END AS type,
cast(CASE when (grouping(advance) = 1) then 'ALL'
else isnull(convert(varchar(10), advance), 'Unknown')
END as varchar(10)) as advance,
count(*) AS number
FROM DBO.titles
where type like '%cook%' or type like 'p%'
GROUP BY type, advance
WITH rollup
go

type advance number
------------ ---------- -----------
mod_cook 0.00 1
mod_cook 15000.00 1
mod_cook ALL 2
popular_comp Unknown 1
popular_comp 7000.00 1
popular_comp 8000.00 1
popular_comp ALL 3
psychology 2000.00 1
psychology 2275.00 1
psychology 4000.00 1
psychology 6000.00 1
psychology 7000.00 1
psychology ALL 5
trad_cook 4000.00 1
trad_cook 7000.00 1
trad_cook 8000.00 1
trad_cook ALL 3
ALL ALL 13



You can also use the GROUPING function to order the result sets to move all the rollups toward the bottom, as shown in Listing 2.

Listing 2. Using the GROUPING Function to Order the Result Sets
SELECT CASE when GROUPING(type) = 1 then 'ALL'
else isnull(type, 'Other')
END AS type,
cast(CASE when (grouping(advance) = 1) then 'ALL'
else isnull(convert(varchar(10), advance), 'Unknown')
END as varchar(10)) as advance,
count(*) AS number
FROM DBO.titles
where type like '%cook%' or type like 'p%'
GROUP BY type, advance
WITH rollup
ORDER by GROUPING(type), GROUPING(advance)
go

type advance number
------------ ---------- -----------
popular_comp Unknown 1
popular_comp 7000.00 1
popular_comp 8000.00 1
psychology 2000.00 1
psychology 2275.00 1
psychology 4000.00 1
psychology 6000.00 1
psychology 7000.00 1
trad_cook 4000.00 1
trad_cook 7000.00 1
trad_cook 8000.00 1
mod_cook 0.00 1
mod_cook 15000.00 1
mod_cook ALL 2
trad_cook ALL 3
psychology ALL 5
popular_comp ALL 3
ALL ALL 13



Other  
  •  SQL Server 2008 : General T-SQL Performance Recommendations
  •  SQL Server 2008 : General T-SQL Coding Recommendations (part 2) - Avoid SQL Injection Attacks When Using Dynamic SQL & Comment Your T-SQL Code
  •  SQL Server 2008 : General T-SQL Coding Recommendations (part 1) - Provide Explicit Column Lists & Qualify Object Names with a Schema Name
  •  SQL Server 2008 : Advanced Stored Procedure Programming and Optimization - Using Extended Stored Procedures
  •  SQL Server 2008 : Advanced Stored Procedure Programming and Optimization - Installing and Using .NET CLR Stored Procedures
  •  SQL Server 2008 : Advanced Stored Procedure Programming and Optimization - Using Dynamic SQL in Stored Procedures
  •  SQL Server 2008 : Advanced Stored Procedure Programming and Optimization - Stored Procedure Performance
  •  SQL Server 2008 : Using Remote Stored Procedures
  •  SQL Server 2008 : Using Temporary Tables in Stored Procedures
  •  SQL Server 2008 : Advanced Stored Procedure Programming and Optimization - Nested Stored Procedures
  •  
    Video
    Top 10
    SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
    The latest Audi TT : New angles for TT
    Era of million-dollar luxury cars
    Game Review : Hearthstone - Blackrock Mountain
    Game Review : Battlefield Hardline
    Google Chromecast
    Keyboards for Apple iPad Air 2 (part 3) - Logitech Ultrathin Keyboard Cover for iPad Air 2
    Keyboards for Apple iPad Air 2 (part 2) - Zagg Slim Book for iPad Air 2
    Keyboards for Apple iPad Air 2 (part 1) - Belkin Qode Ultimate Pro Keyboard Case for iPad Air 2
    Michael Kors Designs Stylish Tech Products for Women
    REVIEW
    - First look: Apple Watch

    - 3 Tips for Maintaining Your Cell Phone Battery (part 1)

    - 3 Tips for Maintaining Your Cell Phone Battery (part 2)
    Popular Tags
    Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Exchange Server Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe Photoshop CorelDRAW X5 CorelDraw 10 windows Phone 7 windows Phone 8 Iphone