subtracting month AND days with dateadd() - tsql

Trying to get minus 1 month and 6 days from todays date GETDATE()??
SELECT (DATEADD(mm,-1,GETDATE()) + (DATEADD(d,-6,GETDATE())))
This is returning : 2132-11-02 01:10:33.500

SELECT DATEADD(dd, -6, DATEADD(mm,-1, GETDATE()))

Related

T-SQL Retrieve Last 6 Weeks Data to the Previous Saturday

SELECT TOP 100 *
FROM FactSalesDetail
WHERE TradingDate >= DATEADD(ww, -6, (Select MAX([TradingDate]) From FactSalesDetail))
ORDER BY TradingDate
Can anyone advise how I can convert the above WHERE Clause from retrieving the last 6 weeks data from Max Date in my Fact Table to the last 6 weeks to the Previous Saturday?
So as of Today that would Saturday 1st Jan and then back 6 weeks from that?
Using the current weekday can get you last Saturday.
And if you bring ##DATEFIRST into the equation then it won't depend on the DATEFIRST setting.
SELECT TOP 100 *
FROM FactSalesDetail
WHERE TradingDate >= CONVERT(DATE, DATEADD(WEEK, -6, DATEADD(DAY, -(##DATEFIRST+DATEPART(WEEKDAY,GETDATE()))%7,
GETDATE())))
AND TradingDate <= CONVERT(DATE, DATEADD(DAY, -(##DATEFIRST+DATEPART(WEEKDAY,GETDATE()))%7,
GETDATE()))
ORDER BY TradingDate
Test snippet for date range
SET DATEFIRST 7;
SELECT
datename(weekday, date_now) AS weekday_now, date_now
, datename(weekday, date1) AS wd1, date1
, datename(weekday, date2) AS wd2, date2
FROM
(
SELECT
CAST(GETDATE() AS DATE) AS date_now
, CONVERT(DATE, DATEADD(WEEK, -6, DATEADD(DAY, -(##DATEFIRST+DATEPART(WEEKDAY,GETDATE()))%7,
GETDATE()))) AS date1
, CONVERT(DATE, DATEADD(DAY, -(##DATEFIRST+DATEPART(WEEKDAY,GETDATE()))%7,
GETDATE())) AS date2
) q
weekday_now
date_now
wd1
date1
wd2
date2
Tuesday
2022-01-04
Saturday
2021-11-20
Saturday
2022-01-01
db<>fiddle here

Select all rows 7 days prior from a specific date

I want to get all rows 7 days prior from 01/09/2017
I know I can do
Load_DTM <= '2017-01-09' and Load_DTM >= '2017-01-02'
But can I not use DateAdd or DatePart?
i.e. DateAdd(dd, -7, '2017-01-09')
Load_DTM BETWEEN DATEADD(dd,-7,'2017-01-09') AND '2017-01-09 11:59:59'
ought to work.
You should be able to do exactly what you showed in your example:
SELECT * FROM Table WHERE DateField = DATEADD(DAY, -7, '2017-09-01')
Since running:
SELECT DATEADD(DAY, -1, GETDATE())
Gives you:
2017-03-15 19:26:29.833

Change Month and Year in a datetime

I have a datetime field in my query.
Select Account, Period, JEDate
From table
I would like to replace the year of the "JEDate" datetime field with a Parameter "#Year" (int).
I would also like to replace the month with the "Period" (int) field.
Example:
If the JEDate was 1900-01-01 00:00:00.000 (or any other date), the year was 2013 and the month was 2, I would like the JEDate to be 2013-02-01 00:00:00.000.
How do I do this?
Thank you
Perhaps:
UPDATE dbo.TableName
SET JEDate = CONVERT(DATETIME, CAST(#Year as varchar(4)) + '-' + CAST(Period as VARCHAR(2)) + '-' + '01', 102)
WHERE JEDate IS NOT NULL
Demo
An alternative approach that adds the difference between the original year and month, and the new year and month:
select dateadd(year, #year-Year(JEDATE), dateadd(month, Period-Month(JEDATE), JEDATE))

Get yesterday's rows startign from 10AM

For previous day I used use the below expression .
DATE_INSERTED >=DATEADD(day, DATEDIFF(day,0,GETDATE())-1,0)
AND DATE_INSERTED < DATEADD(day, DATEDIFF(day,0,GETDATE()),0)
How to get rows from yesterday 10Am to today 10AM
-- yesterday at midnight:
DECLARE #yesterday DATETIME = DATEADD(DAY,DATEDIFF(DAY,1,GETDATE()),0);
SELECT
...
WHERE DATE_INSERTED >= DATEADD(HOUR, 10, #yesterday) -- 10 AM yesterday
AND DATE_INSERTED < DATEADD(HOUR, 34, #yesterday); -- 10 AM today
Instead of using zeroes, use some date(time)s that have the desirable properties:
DATE_INSERTED >=
DATEADD(day, DATEDIFF(day,'20010102',GETDATE()),'2001-01-01T10:00:00')
AND DATE_INSERTED <
DATEADD(day, DATEDIFF(day,'20010102',GETDATE()),'2001-01-02T10:00:00')
I.e. if you add the total number of days that have occurred since 2nd January 2001 onto 10:00am on the 1st January 2001, you'll always obtain a value which is "yesterday at 10am". The second one is almost identical.

T-sql Monday before date

I work at a college and our student management systems academic year start date is determined by the Monday on or before the 1st of August. I need to match this in my query, is there a way to easily get the date of the Monday on or before this date.
The accepted answer didn't work for me because I needed both a Sunday week and a Monday week in the same query. This works across different "datefirst" settings:
SELECT DATEADD(d, -((DATEPART(WEEKDAY, '20110515') - DATEPART(dw, '19000101') + 7) % 7), '20110515')
"DATEPART(dw, '19000101')" will determine your "datefirst" setting since 1900-01-01 was on a Monday. If you want a Tuesday based week, you can change 19000101 to 19000102.
BTW, '20110515' is the only date format that works across all SQL Server culture settings. Dates like '2011-05-06' will get mis-interpreted in certain countries. (credit to Itzik Ben-Gan for pointing this out)
set datefirst 1; -- Make Monday the first day of the week
select dateadd(dd, -1*(datepart(dw, '2009-08-01')-1), '2009-08-01')
Returns July 27th, 2009, which is the Monday on or before August 1. Change it to 2005 when Aug 1 was a Monday and the query will return 08-01
You could use datepart to get the weekday, and then do a little math to back into your monday. This example is using the US default of datefirst 7 (in which Monday is day 2 of the week). Adjust the days to add to be which day of the week Monday is for your locale.
select dateadd(dd, -datepart(dw, '2009-08-01') + 2, '2009-08-01')
very hacky
DECLARE #weekday int
SELECT #weekday = DATEPART(WEEKDAY, '1-Aug-2009')
SELECT CASE
WHEN #weekday = 1 THEN '1-Aug-2009' ELSE DATEADD ( dd,(#weekday-2)*-1, '1-Aug-2009')
END
This is a generic algorithm that will return the first Monday of any month (#inputdate):
DATEADD(wk, DATEDIFF(wk, 0, dateadd(dd, 6 - datepart(day, #inputDate), #inputDate)), 0)
It is a common method for getting the first monday of the month in SQL Server. This link explains how the above calculation works along with many other date calculations.
Here is how the above algorithm could be used to get the Monday on or before the 1st day of a month:
-- Set month to get Monday before or at 1st of month.
DECLARE #inputDate DATETIME
SET #inputDate = '2009-08-01'
-- Get first Monday of month.
DECLARE #firstMonday DATETIME
SET #firstMonday = DATEADD(wk, DATEDIFF(wk, 0, dateadd(dd, 6 - datepart(day, #inputDate), #inputDate)), 0)
-- Determine date for first Monday on or before 1st of month.
DECLARE #startDate DATETIME
SET #startDate = #firstMonday
IF #firstMonday > #inputDate
SET #startDate = DATEADD(wk, -1, #firstMonday)
SELECT #startDate