Setting Start and End dates - sql

I have a simple query that needs to pull in all employees that were hired in the past 10 days, up until end of day yesterday at 11:59, excluding the current date, using Start & End date variables.
I know there's MANY ways of setting this date range.
What is the most efficient SQL server expression that can give you the start and end dates for the past 10 days, excluding the current date?
DECLARE #StartDate = ???
DECLARE #EndDate = ???
SELECT #StartDate, #EndDate
Desired outcome:
2020-04-02 00:00:00.000, 2020-04-11 23:59:59.000

The :59 schemes are a bad idea. Comparisons should be less than the (start of) next day in order to include the full previous day.
SELECT #StartDate = DATEADD(dd,-10,CAST(getdate() AS DATE)), #EndDate = CAST(getdate() AS DATE)
However if you still want it:
SELECT #StartDate = DATEADD(dd,-10,CAST(getdate() AS DATE)), #EndDate = DATEADD(ss,-1,CAST(CAST(getdate() AS DATE) AS DATETIME))

Hope this Code Works fine for your Case:
SELECT
#Start=CAST(CAST(GETDATE()-10 AS DATE) AS DATETIME),
#End=CONCAT(CAST(GETDATE()-1 AS DATE),' 23:59:59.000') ---

Related

SQL Greater than specific time on the previous day

A shift in our plant is defined as starting at 4am and continues until 2 am on the following day.
At 3 am on a given day I want to get all the records for the previous shift.
The query below gets me the previous day upto now but also includes 12am to 2 am on the "previous previous" shift. How do i get the query to get data ONLY after 4am ?
select
*
from yourTable
WHERE TimeStamp >= dateadd(day,datediff(day,1,GETDATE()),0)
DECLARE #yesterday DATE = GETDATE()-1
DECLARE #time TIME = '04:00:00'
DECLARE #shiftstart DATETIME = CAST(#yesterday AS DATETIME) + CAST(#time AS DATETIME)
select
*
from yourTable
WHERE TimeStamp >= #shiftstart
The logic is to subtract 2 hours from the shift time and use the date portion. A simplistic implementation is:
where cast(dateadd(hour, -2, TimeStamp) as date) = cast(dateadd(day, -1, GetDate()) as date)
Sometimes, it is more efficient to do all the arithmetic on the "constant" (i.e. getdate()):
where TimeStamp >= dateadd(hour, 2, cast(GetDate() as Date))
Note: the appropriate function for this logic is dateadd() not datediff().

SQL query to find employee aniversary

I need to find anniversary date and anniversary year of employees and send email in every 14 days.But I have a problem with last week of December when using the following query if start date and end date are in different years.
Select * from Resource
where (DATEPART(dayofyear,JoinDate)
BETWEEN DATEPART(dayofyear,GETDATE())
AND DATEPART(dayofyear,DateAdd(DAY,14,GETDATE())))
Instead of comparing to a dayofyear (which resets to zero at jan 1st and is the reason your query breaks within 14 days of the end of the year) you could update the employee's joindate to be the current year for the purpose of the query and just compare to actual dates
Select * from Resource
-- Add the number of years difference between joinDate and the current year
where DATEADD(year,DATEDIFF(Year,joinDate,GetDate()),JoinDate)
-- compare to range "today"
BETWEEN GetDate()
-- to 14 days from today
AND DATEADD(Day,14,GetDate())
-- duplicate for following year
OR DATEADD(year,DATEDIFF(Year,joinDate,GetDate())+1,JoinDate) -- 2016-1-1
BETWEEN GetDate()
AND DATEADD(Day,14,GetDate())
Test query:
declare #joindate DATETIME='2012-1-1'
declare #today DATETIME = '2015-12-26'
SELECT #joinDate
where DATEADD(year,DATEDIFF(Year,#joinDate,#today),#JoinDate) -- 2015-1-1
BETWEEN #today -- 2015-12-26
AND DATEADD(Day,14,#today) -- 2016-01-09
OR DATEADD(year,DATEDIFF(Year,#joinDate,#today)+1,#JoinDate) -- 2016-1-1
BETWEEN #today -- 2015-12-26
AND DATEADD(Day,14,#today) -- 2016-01-09
(H/T #Damien_The_Unbeliever for a simple fix)
The above correctly selects the joinDate which is in the first week of Jan (note I've had to fudge #today as Ive not managed to invent time travel).
The above solution should also solve the issue with leap years that was hiding in your original solution.
Update
You expressed in comments the requirement to select AnniversaryDate and Years of service, you need to apply some CASE logic to determine whether to add 1 (year or date) to your select
select *,
CASE
WHEN DATEADD(YEAR,DATEDIFF(Year,JoinDate,GETDATE()),JoinDate) < GetDate()
THEN DATEDIFF(Year,JoinDate,GETDATE())+1
ELSE DATEDIFF(Year,JoinDate,GETDATE())
END as [Years],
CASE WHEN DATEADD(YEAR,DATEDIFF(Year,JoinDate,GETDATE()),JoinDate) < GetDate()
THEN DATEADD(YEAR,DATEDIFF(Year,JoinDate,GETDATE())+1,JoinDate)
ELSE DATEADD(YEAR,DATEDIFF(Year,JoinDate,GETDATE()),JoinDate)
end as [AnniversaryDate]
.... // etc
You could do this:
Select * from Resource
where DATEPART(dayofyear,JoinDate)
BETWEEN DATEPART(dayofyear,GETDATE())
AND DATEPART(dayofyear,DateAdd(DAY,14,GETDATE()))
OR
DATEPART(dayofyear,JoinDate)
BETWEEN (DATEPART(dayofyear,GETDATE()) + 365)
AND (DATEPART(dayofyear,DateAdd(DAY,14,GETDATE())) + 365)
Try this:
DECLARE #Today DATE = GETDATE() --'12/25/2013'
DECLARE #Duration INT = 14
;WITH Recur AS
(
SELECT #Today AS RecurDate
UNION ALL
SELECT DATEADD(DAY, 1, RecurDate)
FROM Recur
WHERE DATEDIFF(DAY, #Today, RecurDate)+1 < #Duration
)
SELECT
r.*
FROM
Resource r
JOIN Recur
ON CONVERT(VARCHAR(5), JoinDate, 101) = CONVERT(VARCHAR(5), RecurDate, 101)
WHERE JoinDate < #Today
You can use the SQL DATEADD() function with week number parameter
Here is how you can use it:
DECLARE #date date = getdate()
Select * from Resource
where
JoinDate BETWEEN #date AND DATEADD(ww,2,#date)

TSQL select columns from rollup data columns

I have a rollup data table which stores data for daily counts, monthly counts and yearly counts.
"rollup_type" designates if its a daily(1)/monthly(2)/yearly data(3). For a yearly record, both monthl/daily is null. For monthly record, daily is null.
I am trying to do a simple SELECT based on input start date and end date with no success. Here is what I tried which isn't working correctly.
declare #start_date datetime = '2013-02-01'
declare #enddate datetime = '2014-01-12'
select * from olr_rollup_data rd
where ( rd.Year > DATEPART(YYYY, #start_date) or ( rd.Year = DATEPART(YYYY, #start_date) and ( (rd.Month > DATEPART(MM, #start_date) and rd.day is null) or (rd.MONTH = DATEPART(MM, #start_date) and rd.day >= DATEPART(DD, #start_date) ))))
and ( rd.Year < DATEPART(YYYY, #enddate) or ( rd.Year = DATEPART(YYYY, #enddate) and ( (rd.Month < DATEPART(MM, #enddate) and rd.day is null) or ( rd.MONTH = DATEPART(MM, #enddate) and rd.Day <= DATEPART(DD, #enddate) ))))
Basically, a generic select statement which will use combination of daily, monthly and yearly data from input dates. It should select days plus full month when input dates cover full month in between and so on.
I would appreciate if you help figure out correct select statement. Thank you.
Dates are always a pain to work with, that is why it is nice to use the built in date time functions when available. Sadly that won't work here, we have to figure out some tricks.
One way to work with dates as "numbers" is to multiply the year by 10000, the month by 100 and add these numbers to the day of the month. This will give you an integer where the digits look like this
YYYYMMDD
While disjointed (there are a lot of integers you will never see) any integer with this representation has the same cardinality as the date it represents (those that are larger as a date are also larger as an integer.)
We can use this to solve your problem as follows:
DECLARE #startDate INTEGER = 20130201
DECLARE #endDate INTEGER = 20140112
SELECT *
FROM
(
SELECT ((year*10000)+(ISNULL(month,0)*100)+ISNULL(day,0)) as dateInt, *
FROM olr_rollup_date
) sub
WHERE dateInt >= #startDate AND dateInt <= #endDate
This will include roll up row for the month that are "surrounded". Since we default the null values to 0 a month null for May of 2014 would be 20140500. This is greater than any date in April and less than any date June but smaller than any date in May.
Years will work in a similar way.

How to get Previous business day in a week with that of current Business Day using sql server

i have an ssis Package which runs on business days (mon-Fri). if i receive file on tuesday , background(DB), it takes previous business day date and does some transactions. If i run the job on friday, it has to fetch mondays date and process the transactions.
i have used the below query to get previous business date
Select Convert(varchar(50), Position_ID) as Position_ID,
TransAmount_Base,
Insert_Date as InsertDate
from tblsample
Where AsOfdate = Dateadd(dd, -1, Convert(datetime, Convert(varchar(10), '03/28/2012', 101), 120))
Order By Position_ID
if i execute this query i'll get the results of yesterdays Transactios. if i ran the same query on monday, it has to fetch the Fridays transactions instead of Sundays.
SELECT DATEADD(DAY, CASE DATENAME(WEEKDAY, GETDATE())
WHEN 'Sunday' THEN -2
WHEN 'Monday' THEN -3
ELSE -1 END, DATEDIFF(DAY, 0, GETDATE()))
I prefer to use DATENAME for things like this over DATEPART as it removes the need for Setting DATEFIRST And ensures that variations on time/date settings on local machines do not affect the results. Finally DATEDIFF(DAY, 0, GETDATE()) will remove the time part of GETDATE() removing the need to convert to varchar (much slower).
EDIT (almost 2 years on)
This answer was very early in my SO career and it annoys me everytime it gets upvoted because I no longer agree with the sentiment of using DATENAME.
A much more rubust solution would be:
SELECT DATEADD(DAY, CASE (DATEPART(WEEKDAY, GETDATE()) + ##DATEFIRST) % 7
WHEN 1 THEN -2
WHEN 2 THEN -3
ELSE -1
END, DATEDIFF(DAY, 0, GETDATE()));
This will work for all language and DATEFIRST settings.
This function returns last working day and takes into account holidays and weekends. You will need to create a simple holiday table.
-- =============================================
-- Author: Dale Kilian
-- Create date: 2019-04-29
-- Description: recursive function returns last work day for weekends and
-- holidays
-- =============================================
ALTER FUNCTION dbo.fnGetWorkWeekday
(
#theDate DATE
)
RETURNS DATE
AS
BEGIN
DECLARE #importDate DATE = #theDate
DECLARE #returnDate DATE
--Holidays
IF EXISTS(SELECT 1 FROM dbo.Holidays WHERE isDeleted = 0 AND #theDate = Holiday_Date)
BEGIN
SET #importDate = DATEADD(DAY,-1,#theDate);
SET #importDate = (SELECT dbo.fnGetWorkWeekday(#importDate))
END
--Satruday
IF(DATEPART(WEEKDAY,#theDate) = 7)
BEGIN
SET #importDate = DATEADD(DAY,-1,#theDate);
SET #importDate = (SELECT dbo.fnGetWorkWeekday(#importDate))
END
--Sunday
IF(DATEPART(WEEKDAY,#theDate) = 1)
BEGIN
SET #importDate = DATEADD(DAY,-2,#theDate);
SET #importDate = (SELECT dbo.fnGetWorkWeekday(#importDate))
END
RETURN #importDate;
END
GO
Then how about:
declare #dt datetime='1 dec 2012'
select case when 8-##DATEFIRST=DATEPART(dw,#dt)
then DATEADD(d,-2,#dt)
when (9-##DATEFIRST)%7=DATEPART(dw,#dt)%7
then DATEADD(d,-3,#dt)
else DATEADD(d,-1,#dt)
end
The simplest solution to find the previous business day is to use a calendar table with a column called IsBusinessDay or something similar. The your query is something like this:
select max(BaseDate)
from dbo.Calendar c
where c.IsBusinessDay = 0x1 and c.BaseDate < #InputDate
The problem with using functions is that when (not if) you have to create exceptions for any reason (national holidays etc.) the code quickly becomes unmaintainable; with the table, you just UPDATE a single value. A table also makes it much easier to answer questions like "how many business days are there between dates X and Y", which are quite common in reporting tasks.
You can easily make this a function call, adding a second param to replace GetDate() with whatever date you wanted.
It will work for any day of the week, at any date range, if you change GetDate().
It will not change the date if the day of week is the input date (GetDate())
Declare #DayOfWeek As Integer = 2 -- Monday
Select DateAdd(Day, ((DatePart(dw,GetDate()) + (7 - #DayOfWeek)) * -1) % 7, Convert(Date,GetDate()))
More elegant:
select DATEADD(DAY,
CASE when datepart (dw,Getdate()) < 3 then datepart (dw,Getdate()) * -1 + -1 ELSE -1 END,
cast(GETDATE() as date))
select
dateadd(dd,
case DATEPART(dw, getdate())
when 1
then -2
when 2
then -3
else -1
end, GETDATE())
thanks for the tips above, I had a slight variant on the query in that my user needed all values for the previous business date. For example, today is a Monday so he needs everything between last Friday at midnight through to Saturday at Midnight. I did this using a combo of the above, and "between", just if anyone is interested. I'm not a massive techie.
-- Declare a variable for the start and end dates.
declare #StartDate as datetime
declare #EndDate as datetime
SELECT #StartDate = DATEADD(DAY, CASE DATENAME(WEEKDAY, GETDATE())
WHEN 'Sunday' THEN -2
WHEN 'Monday' THEN -3
ELSE -1 END, DATEDIFF(DAY, 0, GETDATE()))
select #EndDate = #StartDate + 1
select #StartDate , #EndDate
-- Later on in the query use "between"
and mydate between #StartDate and #EndDate

SQL 1st of the month to the end of the month

I have an #StartDate and #EndDate.
I need the #StartDate to be the day the query is ran(which will always be the first of the month) and the #EndDate to be exaclty at the end of the month no matter if the month is 30 or 31 days, etc.
A worked example:
DECLARE #StartDate DATETIME, #EndDate DATETIME
SET #StartDate = '2010-01-01 00:00:00.000'
SET #EndDate = DATEADD(m, 1, #StartDate)
SELECT #StartDate, #EndDate - 1
Basically you want to take the start date, add one month (that's what the DATEADD is doing), and then deduct one day.
The output from that query is:
StartOfMonth EndOfMonth
----------------------- -----------------------
2010-01-01 00:00:00.000 2010-01-31 00:00:00.000