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 calcu
Trang 1convert 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 DATEADDandDATEDIFFfunctions 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
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()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 ”1/1/1900”
NOTE
The value “1/1/1900” is the default for a date if an empty string (’’) is used to
repre-sent a date
You can use the DATEDIFFfunction 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()), ‘’)
Trang 2Calculating 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 datetimevalue 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 DATEADDandDATEDIFFcalculations, you can come up
with many different dates that might be valuable All the examples so far have only
calcu-lated 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 byDATEDIFFwhen
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, ‘’))
Trang 3Similarly, 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 thegetdate()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()), ‘’))), ‘’)
The examples presented in this chapter should give you some insight into using the
DATEADDandDATEDIFFfunctions for calculating dates using date intervals You can use
them as a basis for calculating other dates that your applications might need
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
easi-ly mistyped, leading to incorrect calculations For information on creating user-defined
functions, see Chapter 29, “Creating and Managing User-Defined Functions.”
Converting Dates for Comparison
Because the datetimedata 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:
Trang 4select 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 DATEDIFFfunction 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 pubdatecolumn
to optimize the query, and it is likely to end up performing a table scan (For more
infor-mation on query optimization and optimizable search arguments, see Chapter 35.) 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
Trang 5Another 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 dateandtimedata types, as well as the datetime2data
type The long-awaited dateandtimedata 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 datedata 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 thepubdatecolumn were defined using thedatedata type instead ofdatetime
(reason-able 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
Trang 6title_id pubdate
-
-FI3599 2006-01-14 00:00:00.000
Thedatetime2data type stores the time value down to microseconds and avoids the
3/1,000 second rounding issue that was present with the datetimedata type For example,
if you redefine the pubdatecolumn using the datetime2data type, you avoid the
round-ing issue and get a sround-ingle row as expected by the followround-ing 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 CUBEorROLLUPoperator, SQL Server generates NULLvalues 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 NULLvalue 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 GROUPINGfunction, which you can use to
distinguish between real NULLvalues and NULLvalues that represent a rollup of all values
for a column in the result set
TheGROUPINGfunction returns 1when the value is grouped and 0when the column
contains a NULLvalue
In Listing 43.13, the GROUPINGfunction is used to replace NULLvalues for the rolled-up
columns with ALL
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’)
Trang 7END 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 GROUPINGfunction to order the result sets to move all the rollups
toward the bottom, as shown in Listing 43.14
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
Trang 8ORDER 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
Using CONTEXT_INFO
Although SQL Server enables you to define local variables within a T-SQL batch or stored
procedure, local variables do not retain values between batches or stored procedures
Unfortunately, SQL Server 2008 does not enable you to create user-defined global
vari-ables However, you can simulate global variables by using the CONTEXT_INFOsetting,
which allows you to store information in the context_infocolumn in the
sys.sysprocessescatalog view A row in sys.sysprocessesexists for every connection to
SQL Server, so the data remains there until you disconnect from SQL Server
Thecontext_infocolumn is a binary (128)column You can store any data value in it
with the SET CONTEXT_INFOcommand, but you have to deal with hexadecimal data when
retrieving it If you are handy at manipulating hexadecimal data, you can store multiple
values in the context_infocolumn The following example stores the average price from
thetitlestable in the context_infocolumn:
declare @avg_price money
select @avg_price = avg(price) from dbo.titles
set context_info @avg_price
You can retrieve the value stored in context_infoby using a SELECTstatement You need
to convert the binarydata back to moneywhen you retrieve it Because avg(price)is the
Trang 9only value stored in context_info, you can retrieve it by performing a substring on the
first 8 bytes of the context_infocolumn (The moneydata type is 8 bytes in size.) Because
SQL Server assigns a unique server process ID (SPID) to each connection, you use the
@@SPIDfunction to retrieve the information for the current connection:
select convert(money, substring(context_info, 1, 8)) as AVG_PRICE
from master sysprocesses
where spid = @@spid
go
AVG_PRICE
-0.3751
If you don’t use a substring to specify only the first 8 bytes of thecontext_infocolumn,
SQL Server assumes that themoneydata is stored in the last 8 bytes and returns a result of0:
select convert(money, context_info) as AVG_PRICE
from master sysprocesses
where spid = @@spid
go
AVG_PRICE
-0.00
Becausemoneycan be implicitly converted to binary, you don’t need to convert it when
settingcontext_info For some other data types, such as charordatetime, you need to
explicitly convert the data to binarybecause implicit conversions from those data types to
binaryis not supported In the following example, you append a datetimevalue to the
average price value already stored in context_info You explicitly convert the datetime
value to binaryand append it to the 8 bytes you have already stored in context_info:
declare @max_date datetime,
@context_info binary(128)
select @max_date = max(pubdate) from dbo.titles
select @context_info = substring(context_info, 1, 8)
+ convert(binary(8), @max_date) from master sysprocesses
where spid = @@spid
set context_info @context_info
You now have two values stored in context_info Using the appropriate substring, you
can retrieve either the average price or the maximum pubdate from context_info:
declare @avg_price money,
@max_pubdate datetime
Trang 10select @avg_price = substring(context_info, 1, 8),
@max_pubdate = substring(context_info, 9, 8)
from master sysprocesses
where spid = @@spid
select @avg_price as ‘Avg Price’, @max_pubdate as ‘Max PubDate’
go
Avg Price Max PubDate
-
-0.3751 2009-05-31 00:00:00.000 Note that the binarydata converts implicitly to moneyanddatetime Working with Outer Joins An outer join is used to return all the rows from the specified outer table (specified with LEFT OUTER,RIGHT OUTER, or FULL OUTER), even if the other table has no match Rows returned from the outer table that have no corresponding match in the inner table display the value NULLfor any columns retrieved from the inner table For example, you might want to display the names of all authors along with the average royalty paid, if available: select au_lname, au_fname, avg(royaltyper) as avg_royalty from dbo.authors a left outer join dbo.titleauthor ta on a.au_id = ta.au_id group by au_lname, au_fname order by 3 go au_lname au_fname avg_royalty - -
-Greene Morningstar NULL Greenfield Tom NULL McBadden Heather NULL Smith Meander NULL Stringer Dirk NULL Gringlesby Burt 30
O’Leary Michael 35
Ringer Anne 37
Yokomoto Akiko 40
MacFeather Stearns 42
Hunter Sheryl 50
Dull Ann 50
Bennet Abraham 60
Green Marjorie 70
DeFrance Michel 75
Karsen Livia 75
Ringer Albert 75