You are on page 1of 2

SQL Tips and Tricks

PreviousBusinessDay dateadd(dd, case datediff(dd,'17530101',dte)%7 when 0 then -3 when 6 then -2 else -1 end,dte) First Day of Month select DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)
The inner most function call "getdate()" returns the current date and time. DATEDIFF(mm,0,getdate()) calculates the number of months between the current date and the date "1900-01-01 00:00:00.000". Remember date and time variables are stored as the number of milliseconds since "1900-01-01 00:00:00.000"; this is why you can specify the first datetime expression of the DATEDIFF function as "0." DATEADD, adds the number of months between the current date and '1900-01-01". By adding the number of months between our pre-determined date '1900-01-01' and the current date, I am able to arrive at the first day of the current month. In addition, the time portion of the calculated date will be "00:00:00.000."

Monday of the Current Week


Here I use the week interval (wk) to calculate what date is Monday of the current week. This example assumes Sunday is the first day of the week.

select DATEADD(wk, DATEDIFF(wk,0,getdate()), 0) Set Monday as the first day of the week. set DATEFIRST 1 --2 for Tuesday, 3 for Wed... select DATEADD(dd, 1 - DATEPART(dw, getdate()), getdate()) First Day of the Year select DATEADD(yy, DATEDIFF(yy,0,getdate()), 0) First Day of the Quarter select DATEADD(qq, DATEDIFF(qq,0,getdate()), 0) Truncate the time portion from getdate() select DATEADD(dd, DATEDIFF(dd,0,getdate()), 0) Last Day of Prior Month
Works by subtracting 3 milliseconds from the first day of the month example. Now remember the time portion in SQL Server is only accurate to 3 milliseconds. This is why I needed to subtract 3 milliseconds to arrive at my desired date and time.

select dateadd(ms,-3,DATEADD(mm, DATEDIFF(mm,0,getdate() ), 0))


The time portion of the calculated date contains a time that reflects the last millisecond of the day ("23:59:59.997") that SQL Server can store.

Last Day of Prior Year


Like the prior example to get the last date of the prior year you need to subtract 3 milliseconds from the first day of year.

select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))

Last Day of Current Month


Now to get the last day of the current month I need to modify slightly the query that returns the last day of the prior month. The modification needs to add one to the number of intervals return by DATEDIFF when comparing the current date with "1900-01-01." By adding 1 month, I am calculating the first day of next month and then subtraction 3 milliseconds, which allows me to arrive at the last day of the current month. Here is the TSQL to calculate the last day of the current month.

select dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate() )+1, 0)) Last Day of Current Year


You should be getting the hang of this by now. Here is the code to calculate the last day of the current year.

select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() )+1, 0)) First Monday of the Month select DATEADD(wk, DATEDIFF(wk,0,dateadd(dd,6-datepart(day,getdate()),getdate())), 0)
In this example, I took the code for "Monday of the Current Week," and modified it slightly. The modification was to change the "getdate()" portion of the code to calculate the 6th day of the current month. Using the 6th day of the month instead of the current date in the formula allows this calculation to return the first Monday of the current month.

Last business day of previous month select DATEADD(D, CASE DATENAME(DW, DATEADD(m, DATEDIFF(m, 0, DATEADD(dd, DATEDIFF(dd,0,dateadd(ms,-3,DATEADD(mm, DATEDIFF(mm,0,getdate() ), 0))), 0))+1, 0)) WHEN 'Sunday' THEN -2 WHEN 'Monday' THEN -3 ELSE -1 END, DATEADD(m, DATEDIFF(m, 0, DATEADD(dd, DATEDIFF(dd,0,dateadd(ms,-3, DATEADD(mm, DATEDIFF(mm,0,getdate() ), 0))), 0))+1, 0))
Used Last Day of Prior Month, Truncate the time portion from getdate() and added code to find the previous Friday unless the last day is a Friday.

Pull Domain from email address


SELECT RIGHT(Email, LEN(Email) - CHARINDEX('@', email)) Domain , COUNT(Email) EmailCount FROM dbo.email WHERE LEN(Email) > 0 GROUP BY RIGHT(Email, LEN(Email) - CHARINDEX('@', email)) ORDER BY EmailCount DESC Or

SELECT RIGHT(Email, CHARINDEX(@, REVERSE(email))-1) as Domain , COUNT(Email) as EmailCount FROM dbo.email WHERE LEN(Email) > 0 GROUP BY RIGHT(Email, CHARINDEX(@, REVERSE(email))-1) ORDER BY EmailCount DESC Or Use the substring function to get the domain part of the email substring(email, charindex(@, email) + 1, len(email))

You might also like