DATEADD MS -1 does nothing - sql

This is my basic "test"
select DATEADD(ms,-2,DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0))
, DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0)
,case when DATEADD(ms,-2,DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0)) != DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0) then 'No Match' else 'Match' end
union all
select DATEADD(ms,-1,DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0))
,DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0)
,case when DATEADD(ms,-1,DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0)) != DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0) then 'No Match' else 'Match' end
I am trying to understand why MS -2 , subtracts 3 and MS -1 subtracts none.

MSSQL's datetime data type has a finest granularity of .00333333s (repeating), or roughly 3 milliseconds. Changes less than that will result in either no change, or rounded to 3.
From https://msdn.microsoft.com/en-us/library/cc280460.aspx
datetime2(3) has a precision of one millisecond, and datetime has a
precision of 1/300 of a second.

Related

Count ISO_WEEK between two dates (potentially spanning across years)

This may be a fairly simple question but I can't find a simple solution anywhere. Is there a way to count ISO_WEEK between two dates (which also may span over multiple years) without subtracting the ISO_WEEK values themselves?
If I subtract the number of ISO weeks between the below I get -47
2024-12-01 = W48
2024-12-31 = W01
The expected result should be 5
If I subtract the number of ISO weeks between the below I get -47
2022-01-01 = W52
2022-01-31 = W05
The expected result should be 6
If I subtract the number of ISO weeks between the below I get 0
2022-01-01 = W52
2022-12-31 = W52
The expected result should be 53
If I subtract the number of ISO weeks between the below I get -3
2022-12-31 = W01
2023-01-26 = W04
The expected result should be 5
Here is two portions of SQL I am using to test:
The intention here is to calculate number of ISO weeks in the previous month for billing purposes. A condition has been added to remove 1 week from previous month should the week have already been billed in the previous iteration using conditional logic surrounding the first Sunday of that month.
DECLARE #D DATE = '2025-01-01'
SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, #D)-1, 0) AS FOM
, DATEPART(ISO_WEEK,DATEADD(MONTH, DATEDIFF(MONTH, 0, #D)-1, 0)) AS FOM_ISOWEEK#
, DATEPART(ISO_WEEK,DATEADD(MONTH, DATEDIFF(MONTH, -1, #D)-1, -1))-DATEPART(ISO_WEEK,DATEADD(MONTH, DATEDIFF(MONTH, 0, #D)-1, 0))+1 AS ISOWEEK_COUNT
, DATEPART(ISO_WEEK,DATEADD(MONTH, DATEDIFF(MONTH, -1, #D)-1, -1)) AS EOM_ISOWEEK#
, DATEADD(MONTH, DATEDIFF(MONTH, -1, #D)-1, -1) AS EOM
, DATEADD(WEEK, DATEDIFF(WEEK, 0,DATEADD(DAY, 0 - DATEPART(DAY, DATEADD(MONTH, DATEDIFF(MONTH, 0, #D)-1, 0)), DATEADD(MONTH, DATEDIFF(MONTH, 0, #D)-1, 0))), 6) AS FSUNDAY
, CASE WHEN DATEDIFF(DAY, (DATEADD(MONTH, DATEDIFF(MONTH, 0, #D)-1, 0)), (DATEADD(WEEK, DATEDIFF(WEEK, 0,DATEADD(DAY, 0 - DATEPART(DAY, DATEADD(MONTH, DATEDIFF(MONTH, 0, #D)-1, 0)), DATEADD(MONTH, DATEDIFF(MONTH, 0, #D)-1, 0))), 6)))+1 < 7
THEN DATEPART(ISO_WEEK,DATEADD(MONTH, DATEDIFF(MONTH, -1, #D)-1, -1))-DATEPART(ISO_WEEK,DATEADD(MONTH, DATEDIFF(MONTH, 0, #D)-1, 0))
ELSE DATEPART(ISO_WEEK,DATEADD(MONTH, DATEDIFF(MONTH, -1, #D)-1, -1))-DATEPART(ISO_WEEK,DATEADD(MONTH, DATEDIFF(MONTH, 0, #D)-1, 0))+1 END AS [SUM]
DECLARE #DD DATE = '2022-02-01'
SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, #DD)-1, 0) AS FOM
, DATEPART(ISO_WEEK,DATEADD(MONTH, DATEDIFF(MONTH, 0, #DD)-1, 0)) AS FOM_ISOWEEK#
, DATEPART(ISO_WEEK,DATEADD(MONTH, DATEDIFF(MONTH, -1, #DD)-1, -1))-DATEPART(ISO_WEEK,DATEADD(MONTH, DATEDIFF(MONTH, 0, #DD)-1, 0))+1 AS ISOWEEK_COUNT
, DATEPART(ISO_WEEK,DATEADD(MONTH, DATEDIFF(MONTH, -1, #DD)-1, -1)) AS EOM_ISOWEEK#
, DATEADD(MONTH, DATEDIFF(MONTH, -1, #DD)-1, -1) AS EOM
, DATEADD(WEEK, DATEDIFF(WEEK, 0,DATEADD(DAY, 0 - DATEPART(DAY, DATEADD(MONTH, DATEDIFF(MONTH, 0, #DD)-1, 0)), DATEADD(MONTH, DATEDIFF(MONTH, 0, #DD)-1, 0))), 6) AS FSUNDAY
, CASE WHEN DATEDIFF(DAY, (DATEADD(MONTH, DATEDIFF(MONTH, 0, #DD)-1, 0)), (DATEADD(WEEK, DATEDIFF(WEEK, 0,DATEADD(DAY, 0 - DATEPART(DAY, DATEADD(MONTH, DATEDIFF(MONTH, 0, #DD)-1, 0)), DATEADD(MONTH, DATEDIFF(MONTH, 0, #DD)-1, 0))), 6)))+1 < 7
THEN DATEPART(ISO_WEEK,DATEADD(MONTH, DATEDIFF(MONTH, -1, #DD)-1, -1))-DATEPART(ISO_WEEK,DATEADD(MONTH, DATEDIFF(MONTH, 0, #DD)-1, 0))
ELSE DATEPART(ISO_WEEK,DATEADD(MONTH, DATEDIFF(MONTH, -1, #DD)-1, -1))-DATEPART(ISO_WEEK,DATEADD(MONTH, DATEDIFF(MONTH, 0, #DD)-1, 0))+1 END AS [SUM]
Everything works except when the subtraction spans over multiple years. I have provided two examples of this occurence above.
UPDATE
I have simplified somewhat and found a reasonable workaround here. Updated logic below:
DECLARE #D DATE = '2025-01-01'
DECLARE #FOM DATE = DATEADD(MONTH, DATEDIFF(MONTH, 0, #D)-1, 0)
, #EOM DATE = DATEADD(MONTH, DATEDIFF(MONTH, -1, #D)-1, -1)
SELECT #FOM AS FOM
, DATEPART(ISO_WEEK,#FOM) AS FOM_ISOWEEK#
, DATEDIFF(dd,0,#EOM)/7 - DATEDIFF(dd,0,#FOM)/7 +1 AS ISOWEEK_COUNT
, DATEPART(ISO_WEEK,#EOM) AS EOM_ISOWEEK#
, #EOM AS EOM
, DATEADD(WEEK, DATEDIFF(WEEK, 0,DATEADD(DAY, 0 - DATEPART(DAY, DATEADD(MONTH, DATEDIFF(MONTH, 0, #D)-1, 0)), DATEADD(MONTH, DATEDIFF(MONTH, 0, #D)-1, 0))), 6) AS FSUNDAY
, CASE WHEN DATEDIFF(DAY, (DATEADD(MONTH, DATEDIFF(MONTH, 0, #D)-1, 0)), (DATEADD(WEEK, DATEDIFF(WEEK, 0,DATEADD(DAY, 0 - DATEPART(DAY, DATEADD(MONTH, DATEDIFF(MONTH, 0, #D)-1, 0)), DATEADD(MONTH, DATEDIFF(MONTH, 0, #D)-1, 0))), 6)))+1 < 7
THEN DATEDIFF(dd,0,#EOM)/7 - DATEDIFF(dd,0,#FOM)/7
ELSE DATEDIFF(dd,0,#EOM)/7 - DATEDIFF(dd,0,#FOM)/7 +1 END AS [SUM]
This update produces the desired output.
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/604052a8-b024-41a5-8bac-1c657d9f6025/calculate-number-of-weeks-between-dates?forum=transactsql
This helped. I have edited my question with an UPDATE to show it in use.

Get every month of the current year in SQL Server

I'm using the following code to get the date of every day in the current week, in SQL Server:
SELECT
DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0) Monday,
DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0) + 1 Tuesday,
DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0) + 2 Wednsday,
DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0) + 3 Thursday,
DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0) + 4 Friday,
DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0) + 5 Saturday,
DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0) + 6 Sunday;
I'm using this query to fill a bar chart in a asp.net application.
I need the same thing but with the months of the current year.
Can you help me with this, please?
Edit: More like a query to retrieve data from every month of this year.
This will return the first and last day of every month for the current year.
select FirstDay = dateadd(month, thisMonth - 1, dateadd(year, datediff(year, 0, getdate()), 0))
, LastDay = dateadd(day, -1, dateadd(month, thisMonth, dateadd(year, datediff(year, 0, getdate()), 0)))
from
(
values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12)
) x(thisMonth)

Two Seperate WHERE clauses in a DELETE Function

I want to perform a DELETION on certain Transactions that were made between the first day of the current month and the last day of the month. The deletion process shall ONLY be made once the FileUploadDate reaches the last day of the month. In other words when the FileUploadDate is EQUAL to the last day of the month. The main issue here is when the first condition of the WHERE clause is TRUE, the second condition is "Filtered" with values from the first condition, which makes it irrelevant. I tried using other methods such as CASE-WHEN-THEN, but I'm having problems integrating DELETE inside a CASE clause (if that is even possible). Is there a way to perform two separate conditions without one affecting the other? Thanks.
DELETE
FROM transacions
WHERE EXISTS
(
SELECT
*
FROM transacions
WHERE fileuploaddate = CONVERT( date, dateadd(d, -2, dateadd(m, datediff(m, 0, getdate()) + 1, 0)),103)
AND fileuploaddate BETWEEN CONVERT(date, dateadd(month, datediff(month, 0, getdate()), 0))
AND CONVERT(date, dateadd(d, -1, dateadd(m, datediff(m, 0, getdate()) + 1, 0)),103)
)
you could use OR instead AND
WHERE FileUploadDate = CONVERT(DATE, DATEADD(d, -2, DATEADD(m, DATEDIFF(m, 0, GETDATE()) + 1, 0)),103)
OR FileUploadDate BETWEEN CONVERT(DATE, DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0)) AND CONVERT(DATE, DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, GETDATE()) + 1, 0)),103)
Try this:
DELETE
FROM Transactions
WHERE EOMONTH (FileUploadDate) = DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0) -- delete on the last day of the month
AND FileUploadDate >= DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0) -- delete files from the begining of the month
AND FileUploadDate <= DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0) -- delete files to the end of the current montn

Select the first weekday of the next Month

I am trying to modify someone else's code I found here to find the first weekday of next month (Link):
SELECT CASE
WHEN DATENAME(WEEKDAY, dateadd(mm, DATEDIFF(MM, 0, getdate()), 0)) = 'Saturday'
THEN dateadd(mm, DATEDIFF(MM, 0, getdate()), 0) + 2
WHEN DATENAME(WEEKDAY, dateadd(mm, DATEDIFF(MM, 0, getdate()), 0)) = 'Sunday'
THEN dateadd(mm, DATEDIFF(MM, 0, getdate()), 0) + 1
ELSE dateadd(mm, DATEDIFF(MM, 0, getdate()), 0)
END
Here is what I have come up with (which doesn't work):
select CASE
WHEN DATENAME(WEEKDAY, eomonth(dateadd(mm, DATEDIFF(MM, 0, getdate()),1), 0 )) = 'Saturday'
THEN eomonth(dateadd(mm, DATEDIFF(MM, 0, getdate()), 0),1) + 2
WHEN DATENAME(WEEKDAY, eomonth(dateadd(mm, DATEDIFF(MM, 0, getdate()),1), 0)) = 'Sunday'
THEN eomonth(dateadd(mm, DATEDIFF(MM, 0, getdate()), 0),1) + 1
ELSE eomonth(dateadd(mm, DATEDIFF(MM, 0, getdate()), 1),1)
end
Thanks for the help!
Your query implementation looks like for SQL server. You may use DATEFROMPARTS or EOMONTH function to get first day of next month, then use DATENAME in CASE expression to decide the first weekday like below (SQL Server only).
-- get first day of next month using DATEFROMPARTS
Declare #NextMonthFirstDate datetime = DATEFROMPARTS(YEAR(GETDATE()),MONTH(GETDATE())+1,1)
-- then use CASE expression
Select CASE WHEN DATENAME(WEEKDAY, #NextMonthFirstDate) = 'Sunday'
THEN #NextMonthFirstDate + 1
WHEN DATENAME(WEEKDAY, #NextMonthFirstDate) = 'Saturday'
THEN #NextMonthFirstDate + 2
ELSE #NextMonthFirstDate END as FirstWeekDayOfnextMonth

GETDATE last month

I am trying to list last a website's statistics.
I listed Last 30 days with;
CONVERT(VARCHAR(10), S.DATEENTERED, 101)
BETWEEN
CONVERT(VARCHAR(10), GETDATE()-30, 101)
AND
CONVERT(VARCHAR(10), GETDATE(), 101)
and this month with;
RIGHT(CONVERT(VARCHAR(10), S.DATEENTERED, 103), 7) =
RIGHT(CONVERT(VARCHAR(10), GETDATE(), 103), 7)
but I have no idea what query to use for last month. I tried with;
RIGHT(CONVERT(VARCHAR(10), S.DATEENTERED, 103), 7) =
RIGHT(CONVERT(VARCHAR(10), GETDATE()-1, 103), 7)
Did not work.
Dates are always a joy to work with in any programming language, SQL not excluded.
To answer your question to find all records that occurred last month
select S.DATEENTERED
,*
from sometable S
where S.DATEENTERED
between dateadd(mm, datediff(mm, 0, dateadd(MM, -1, getdate())), 0)
and dateadd(ms, -3, dateadd(mm, datediff(mm, 0, dateadd(MM, -1, getdate())) + 1, 0))
order by 1
To expand the best means for getting records within a certain time-frame is by utilizing the datediff function, dateadd function, and the between condition in the where clause.
select 'howdy'
,getdate()
where getdate()
between dateadd(mm, 0, 0)
and dateadd(ms, -3, dateadd(mm, datediff(mm, 0, dateadd(mm,-1,getutcdate())) + 1, 0))
The above code will result in no records returned because it is checking to see if today's date is between 1900-01-01 00:00:00.000 and the last possible recorded date of last month (the last day and 23:59:59.997 - SQL Server DATETIME columns have at most a 3 millisecond resolution).
The following code will return a record as the date we are searching for is one month ago.
select 'howdy'
,dateadd(mm, -1, getdate())
where dateadd(mm, -1, getdate())
between dateadd(mm, 0, 0)
and dateadd(ms, -3, dateadd(mm, datediff(mm, 0, dateadd(mm,-1,getutcdate())) + 1, 0))
A break down of the where clause:
WHERE getdate() -- date to check
between dateadd(mm, 0, 0) -- begin date
and dateadd(ms, -3, dateadd(mm, datediff(mm, 0, dateadd(mm,-1,getutcdate())) + 1, 0)) -- end date
Finally, a variety of dates can be ascertained in this manner here is a pretty complete list:
select dateadd(mm, 0, 0) as BeginningOfTime
,dateadd(dd, datediff(dd, 0, getdate()), 0) as Today
,dateadd(wk, datediff(wk, 0, getdate()), 0) as ThisWeekStart
,dateadd(mm, datediff(mm, 0, getdate()), 0) as ThisMonthStart
,dateadd(qq, datediff(qq, 0, getdate()), 0) as ThisQuarterStart
,dateadd(yy, datediff(yy, 0, getdate()), 0) as ThisYearStart
,dateadd(dd, datediff(dd, 0, getdate()) + 1, 0) as Tomorrow
,dateadd(wk, datediff(wk, 0, getdate()) + 1, 0) as NextWeekStart
,dateadd(mm, datediff(mm, 0, getdate()) + 1, 0) as NextMonthStart
,dateadd(qq, datediff(qq, 0, getdate()) + 1, 0) as NextQuarterStart
,dateadd(yy, datediff(yy, 0, getdate()) + 1, 0) as NextYearStart
,dateadd(ms, -3, dateadd(dd, datediff(dd, 0, getdate()) + 1, 0)) as TodayEnd
,dateadd(ms, -3, dateadd(wk, datediff(wk, 0, getdate()) + 1, 0)) as ThisWeekEnd
,dateadd(ms, -3, dateadd(mm, datediff(mm, 0, getdate()) + 1, 0)) as ThisMonthEnd
,dateadd(ms, -3, dateadd(qq, datediff(qq, 0, getdate()) + 1, 0)) as ThisQuarterEnd
,dateadd(ms, -3, dateadd(yy, datediff(yy, 0, getdate()) + 1, 0)) as ThisYearEnd
Using the above list a range of any type can be determined.
The following will find you the start of the last month:
-- Start of last month
SELECT CAST('01 '+ RIGHT(CONVERT(CHAR(11),DATEADD(MONTH,-1,GETDATE()),113),8) AS datetime)
You would then find the start of this month, using the following, minus one.
-- Start of the month
SELECT CAST('01 '+ RIGHT(CONVERT(CHAR(11),GETDATE(),113),8) AS datetime)
When I have to work with dates in SQL Server I often reference Robyn Page's SQL Server DATE/TIME Workbench. The workbench (tutorial) is well laid out and contains just about everything I have ever needed when working with dates on SQL Server.
How about this?
select DATEADD(month, -1, GETDATE())
I would suggest using the first day of last month and the first day of the current month for the operation and rather than using BETWEEN use >= and <. That's my personal opinion, but I believe you will find there are performance and maintainability benefits to this approach.
Here's the sql. You will notice I've included the last day of the last month value just in case you end up going with another approach.
Keep in mind, these dates are based off of 12:00AM that day. In other words, getting values between 6/1/2009 and 6/30/2009 won't get you what you want as all of 6/30/2009 is excluded. If you use the first day of July (7/1/2009) you are covered.
Again, I recommend avoiding BETWEEN all together as shown below. Best of luck.
Declare #LastMonthFirstDay datetime
Declare #LastMonthLastDay datetime
Declare #ThisMonthFirstDay datetime
Set #LastMonthFirstDay = DATEADD(MONTH, DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP) - 1, 0);
Set #ThisMonthFirstDay = DATEADD(MONTH, DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP), 0);
Set #LastMonthLastDay = DATEADD(DAY, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP), 0));
Select * From Table
Where DateEntered >= #LastMonthFirstDay
And DateEntered < #ThisMonthFirstDay;
Try using the DATEADD function. You can add a -1 with the MONTH (mm) datepart and it should work. Here is a link
where year(S.DATEENTERED) = year(dateadd(mm, -1, getdate())) and month(S.DATEENTERED) = month(dateadd(mm, -1, getdate()))
Might not be good performance-wise but you've got the idea.
GET FIRST DAY OF LAST MONTH
SELECT DATEADD(MM, DATEDIFF(MM, '01/01/2000', DATEADD(MM, -1,GETDATE())), '01/01/2000')
GET LAST DAY OF LAST MONTH
SELECT DATEADD(SS,-1,DATEADD(MM, DATEDIFF(MM,'01/01/2000',GETDATE()),'01/01/2000'))
Then search based on this range.
Try:
declare #lastm int
set #lastm = datepart(mm,getdate()) - 1
...
where datepart(mm,s.dateentered) = #lastm