Subtract 1 year and 1 day at SQL Server - sql-server

In order to subtract 1 year I use
SELECT DATEADD(year, -1, GETDATE())
Likewise for the day.
What is the syntax if I want the combination??
ex from 30/05/2020 ---> 29/05/2019

Wrap that in another DATEADD call.
SELECT DATEADD(DAY, -1, DATEADD(YEAR, -1, GETDATE()))

You could do the following, as SQL Server adds/subtracts days by default:
SELECT DATEADD(year, -1, GETDATE()-1)

Related

Using Parameters to generate date range

So I have multiple reports I am trying to merge into one report. The big issue is that one report is run every two weeks and the other is run once a month.
The date range for the report was created using this sql
SELECT
CASE
WHEN DAY(GETDATE()) <= 15 THEN
DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) - 1, 0) + 15
ELSE
DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) , 0)
END AS LO_DATE
So I tried adding a parameter that I could use that would basically say, hey if this is checked, then always run it for the whole month not the last two weeks. That sql looks like this.
IF (#RUN_FOR_MONTH = 'true')
SELECT
CASE
WHEN DAY(GETDATE()) <= 15 THEN
DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) - 1, 0) + 15
ELSE
DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) , 0)
END AS LO_DATE
ELSE
SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) , 0) AS LO_DATE
However I keep getting this error:
The report parameter ‘LO_DATE’ has a DefaultValue or a ValidValue that depends on the report parameter “RUN_FOR_MONTH”. Forward dependencies are not valid.
I am new to using SQL Server Report Builder, so if you need more information, please ask and I'll provide it.
REQUESTED CHANGE - I still get the same error
SELECT
CASE
WHEN #RUN_FOR_MONTH = 'true' THEN
DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) , 0)
WHEN DAY(GETDATE()) <= 15 THEN
DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) - 1, 0) + 15
ELSE
DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) , 0)
END AS LO_DATE
Parameters in SSRS are derived sequentially. If you have a parameter that is based off another's value, you must list those parameters in in the correct sequential order. Take the below
In this example, #DateFrom would be evaluated first, and then #DateTo; thus #DateFrom cannot depend on #DateTo (however the reverse is fine).
If, for example, the value of #DateFrom was DateAdd("d", -2, #DateTo) you would receive the error you have above. You would need to select #DateTo and click the Up Arrow icon (Or select #DateFrom and the down arrow)

What is different between DATEADD(DAY, 1, GETDATE()) and DATEADD(DAY, 1, DATEDIFF(DAY, 0, GETDATE()))

What is different between
DATEADD(DAY, 1, GETDATE())
and
DATEADD(DAY, 1, DATEDIFF(DAY, 0, GETDATE()))
Could someone help to show example case, how to use them?
The first version includes the time component of GETDATE(). The second does not. So, if the current time is 2018-01-01T05:43:26, then the first version returns:
2018-01-02T05:43:26
The second removes the time component, so it returns:
2018-01-02T00:00:00
I think a better version to get midnight when the next day starts is:
dateadd(day, 1, cast(getdate() as date))
In your first Version DateAdd() Adding Date in Current Date.
In your Second Version first Execute DATEDIFF(DAY, 0, GETDATE()) It Gives you Date Different and After that It Will Add One Day in DATEDIFF(DAY, 0, GETDATE()) Result.

Select a date without time (SQL Server (+) [duplicate]

This question already has answers here:
How to return only the Date from a SQL Server DateTime datatype
(46 answers)
Closed 6 years ago.
I would like to retrieve a date minus 1 year, but without time notation.
The following query
SELECT DATEADD(year, -1, GETDATE())
Output:
2015-03-30 10:48:04.220
What I want is 2015-03-30 00:00:00:000
Similar to:
(DATEADD(dd, DATEDIFF(dd, 0, GETDATE()), 0))
Which results in: 2016-03-30 00:00:00.000
What is the correct or easiest way to do this?
On SQL Server 2008 and higher, you should convert to date:
SELECT CONVERT(date, (DATEADD(year, -1, GETDATE())))
On older versions, you can do the following:
SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, DATEADD(year, -1, GETDATE())))

Date Range of the x-th week in SQL Server?

I know the year and the index of the week, i.e. 2014, the 5th week. how can I find out the starting and ending date of this week in SQL Server? I do not really care whether the week starting with Monday or Sunday.
In mySQL, it has a MakeDate which may be able to do this. Is there an existing way to do this in SQL Server?
Thanks
Try this.
DECLARE #date DATE='2014-01-01'
SELECT Dateadd(dd, -( Datepart(dw, Dateadd(wk, 5, #date) - 1) ), Dateadd(wk, 5, #date)) [WeekStart],
Dateadd(dd, 7 - ( Datepart(dw, Dateadd(wk, 5, #date)) ), Dateadd(wk, 5, #date)) [WeekEnd]
SELECT
dateadd(wk,5,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)) as firstdayof5thweek2014,
dateadd(dd,6,dateadd(wk,5,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0))) as lastdayof5thweek2014
SELECT DATEADD(yy, DATEDIFF(yy,0,getdate()), 0) ==>gives you start day of the current year
SELECT dateadd(wk,5,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)) ==> On top add 5 weeks to get the start date of 5th week in 2014
SELECT dateadd(dd,6,dateadd(wk,5,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0))) ==> On top add 6 days to get the last date of 5th week in 2014

SQL Server - Get first date in a week, given the week number?

I've got a query (for use in bug tracker.net) that calculates the number of bugs by week by status. But the query returns the week number, what I really want is the first date of the week
select datepart(wk, DateAdd(day, 0, DateDiff(day, 0, bg_reported_date)))
as [week], bg_status , st_name as [status], count(*) as [count]
from bugs inner join statuses on bg_status = st_id
group by datepart(wk, DateAdd(day, 0, DateDiff(day, 0, bg_reported_date))),
bg_status, st_name
order by [week], bg_status
The part that gets the week number is
datepart(wk, DateAdd(day, 0, DateDiff(day, 0, bg_reported_date))) as [week]
It returns this output:
week bg_status status count
----------- ----------- --------------------------------------------- ------
22 1 new 1
22 5 closed 32
But it would be better to say the first date of each week, eg 01-01-2010, then 08-01-2010, etc
Question is not a duplicate of How do you get the "week start date" and "week end date" from week number in SQL Server? (answer says how to get week start from a date not from a week number)
Not a duplicate of Calculate date from week number (question asks for c#)
Not a duplicate of Get first date of week from provided date (question asks for javascript)
I did search but couldn't find this question answered for SQL Server (2010 if it matters)
If you think about it in the right way, the answer to SO 1267126 can be applied to your problem.
Each of the bug reported dates that you have in the group maps to the same week. By definition, therefore, each of those bug dates must also map to the same start of the week. So, you run the 'start of the week from given date' calculation on the bug report dates, as well as the week number calculation, and group by both (modestly ghastly) expressions, and end up with the answer you seek.
SELECT DATEPART(wk, DATEADD(day, 0, DATEDIFF(d, 0, bg_reported_date))) [week],
DATEADD(dd, -(DATEPART(dw, bg_reported_date)-1), bg_reported_date)
AS [weekstart], bg_status, st_name AS [status], COUNT(*) AS [count]
FROM bugs INNER JOIN statuses ON bg_status = st_id
GROUP BY DATEPART(wk, DATEADD(day, 0, DATEDIFF(day, 0, bg_reported_date))),
DATEADD(dd, -(DATEPART(dw, bg_reported_date)-1), bg_reported_date),
bg_status, st_name
ORDER BY [week], bg_status
Since bg_reported_date is a DATETIME (see the comment; it includes a time component), it is necessary to cast it to DATE before determining the week-start (but the week number expression doesn't need the cast, and the 'day of week' part of the week start expression doesn't need the cast either):
SELECT DATEPART(wk, DATEADD(day, 0, DATEDIFF(d, 0, bg_reported_date))) [week],
DATEADD(dd, -(DATEPART(dw, bg_reported_date)-1),
CAST(bg_reported_date AS DATE)) AS [weekstart],
bg_status, st_name AS [status], COUNT(*) AS [count]
FROM bugs INNER JOIN statuses ON bg_status = st_id
GROUP BY DATEPART(wk, DATEADD(day, 0, DATEDIFF(day, 0, bg_reported_date))),
DATEADD(dd, -(DATEPART(dw, bg_reported_date)-1),
CAST(bg_reported_date AS DATE),
bg_status, st_name
ORDER BY [week], bg_status
NB: Untested code!
I realize this is a very old thread, but "Get first date in a week, given the week number" is exactly what I wanted to do and I do NOT have an actual date to work with, so the accepted answer would not work for me. I thought I'd post my solution for posterity. Note that I suspect different culture settings MAY break this, so test before using.
My answer is built starting from this one.
Let's assume you know a week number and a year and you want to get the start and end dates for that week of that year. Here's what I have:
--These 2 "declared" variables would be passed in somehow
declare #WeekNumber int = DATEPART(wk, GETDATE())
declare #ForYear int = YEAR(GETDATE())-1
--Since we don't have a raw date to work with, I figured I could just start with
--Jan 1 of that year. I'll store that date in a cte here, but if you are doing this
--in a stored proc or function, it would make much more sense to use another #variable
;with x as
(
--this method works in SQL 2008:
SELECT CONVERT(DateTime, ('1/1/' + CONVERT(varchar, #ForYear))) as Jan1ForSelectedYear
--If you are using 2014 or higher, you can use this instead:
--DATETIME2FROMPARTS(#ForYear, 1, 1, 0,0,0,0,0)
)
--Now that we have a date to work with, we'll just add the number of weeks to that date
--That will bring us to the right week number of the given year.
--Once we have THAT date, we can get the beginning and ending of that week
--Sorry to make you scroll, but I think this is easier to see what is going on this way
SELECT CONVERT(varchar(50), DateAdd(wk, (#WeekNumber - 1), (DATEADD(dd, ##DATEFIRST - DATEPART(dw, x.Jan1ForSelectedYear) - 6, x.Jan1ForSelectedYear))), 101) as FirstDayOfWeekXForSelectedYear,
CONVERT(varchar(50), DateAdd(wk, (#WeekNumber - 1), (DATEADD(dd, ##DATEFIRST - DATEPART(dw, x.Jan1ForSelectedYear) , x.Jan1ForSelectedYear))), 101) as LastDayOfWeekXForSelectedYear
FROM x

Resources