Subtracting One Year - sql

SELECT
#begindate = dateadd(mm,-1,CAST(datepart(mm,getdate()) AS VARCHAR(2)) + '/15/' + CAST(datepart(YYYY,getdate() -1) AS varchar(4))),
#enddate = CAST(datepart(mm,getdate()) AS VARCHAR(2)) + '/14/' + CAST(datepart(YYYY,getdate() -1) AS varchar(4))
Right now this code returns the dates May 15th - June 14th. Those are the required dates that I need, but what I also need is for it to be in the year 2013, and this returns the year 2014. I've tried doing a dateadd(yyyy, -1) and it didn't work. So I was wondering if anyone knew how I would be able to get last years date.

If DateAdd didn't work, there must have been an error in your implementation. Try it like this after your existing code:
SET #begindate = dateadd(year, -1, #begindate);
SET #enddate = dateadd(year, -1, #enddate)

declare #begindate datetime
declare #enddate datetime
set #begindate = dateadd(mm,-1,CAST(datepart(mm,getdate()) AS VARCHAR(2)) + '/15/' + CAST(datepart(YYYY, getdate()) - 1 AS varchar(4)))
set #enddate = CAST(datepart(mm,getdate() ) AS VARCHAR(2)) + '/14/' + CAST(datepart(YYYY, getdate()) - 1 AS varchar(4))
select #begindate
union all
select #enddate

Instead of working with strings you can use date variables and date arithmetic:
declare #lastyear date=dateadd(yy,-1,cast(getdate() as date))
declare #currentMonth date=dateadd(d,-day(#lastyear),#lastyear)
declare #prevMonth date=dateadd(m,-1,#currentMonth)
select #lastyear,DATEADD(d,14,#currentMonth),DATEADD(d,15,#prevMonth)
In SQL Server 2012 it's even easier because you can use the DATEFROMPARTS function to construct a new date from its parts.

Related

replace calendar dateto/from with year,month,day dropdown menus in ssrs

best way to do this would be to have parameters which link the day dropdown column to the month one so that there are correct days in every month?
You would also need to use your Year parameter for a leap day in February.
I would use a table of dates based on your other parameters:
DECLARE #YEAR AS INT = 2016 --FOR DEV/TESTING - REFERENCE PARAMETERS
DECLARE #MONTH AS INT = 2 --FOR DEV/TESTING
DECLARE #START_DATE DATE = CAST(#YEAR AS VARCHAR(4)) + '-' + RIGHT('0' + CAST(#MONTH AS VARCHAR(2)), 2) + '-' + '01'
DECLARE #END_DATE DATE = DATEADD(DAY, -1, DATEADD(MONTH, 1, #START_DATE))
;WITH GETDATES AS
(
SELECT #START_DATE AS THEDATE
UNION ALL
SELECT DATEADD(DAY,1, THEDATE) FROM GETDATES
WHERE THEDATE < #END_DATE
)
SELECT DAY(GETDATES.THEDATE) AS DAYS FROM GETDATES
OPTION (maxrecursion 0)

How to use dateadd function in SQL Server 2017

I am trying to add character type in SQL Server using DATEADD function.
I want the 25th of the next month of the variable.
Example
DECLARE #Date char(6)
SET #Date = '201712'
I want result = 20180125
DECLARE #Date char(6)
SET #Date = '201801'
I want result = 20180225
Thanks in advance
How about this?
dateadd(month, 1, dateadd(day, 25, cast(#date + '01' as date)))
You can shorten this to:
dateadd(month, 1, cast(#date + '25' as date))
Get the month end date by adding '01' to the yyyymm string using EOMONTH function and add 25 days to it.
select dateadd(day,25,eomonth(#date+'01'))

Finding week start and week end date

I have a week number and year and i need to display "total for mm/dd/yy to mm/dd/yy in a row of my ssrs report. My week starts with Monday. For example if my week number is '2' and year is '2010' then I have to display "total for 01/04/2010 to 01/10/2010 in my ssrs column. how to do this?
Try this
declare #year char(4) = '2010'
declare #week int = 2
declare #fromdate datetime
declare #todate datetime
set #fromdate = DATEADD(wk, DATEDIFF(wk, 6, '1/1/' + #year) + (#week-1), 7);
set #todate = DATEADD(wk, DATEDIFF(wk, 5, '1/1/' + #year) + (#week-1), 6) ;
;WITH dates AS
(
SELECT CONVERT(datetime,#fromDate) as Date
UNION ALL
SELECT DATEADD(d,1,[Date])
FROM dates
WHERE DATE < #toDate
)
select * from dates
SQL Server has a DATEPART function which calculates the ordinal week number of a year. However, you have to call DATEFIRST before this to define which day of the week represents the start of the week. In your case, you have stated that the start of your week is Monday (i.e. 1).
SET DATEFIRST 1;
SELECT SUM([your data column])
FROM [your table]
WHERE DATEPART(WEEKNUM, [your date column])=[your week parameter]
AND DATEPART(YEAR, [your date column])=[your year parameter]
Your description is not american standard nor isoweek. Seems like a mix of those. I never heard of that as a standard. It is nearly isoweek. So that is what this answer is based on.
Calculating iso year is a bit tricky, you can read about it here:
This is the syntax you need:
DECLARE #year int = 2010
DECLARE #week int = 2
;WITH CTE AS
(
SELECT
dateadd(wk, datediff(wk, - #week * 7,
cast(cast(#year as char(4)) as datetime) - 5), 0) startofweek
)
SELECT
replace('total for ' + convert(char(10), startofweek, 110)
+ ' to ' + convert(char(10), dateadd(day, 6, startofweek) , 110), '-', '/')
FROM CTE
Result:
total for 01/11/2010 to 01/17/2010
Isoweek 2 in 2010 is 2010-01-11
Try setting DATEFIRST (https://msdn.microsoft.com/en-ie/library/ms181598.aspx)
SET DATEFIRST 7
declare #wk int
declare #yr int
declare #EndOfWeek as datetime
set #wk = 2
set #yr = 2010
SET #EndOfWeek = dateadd (week, #wk, dateadd (year, #yr-1900, 0)) + 1 - datepart(dw, dateadd (week, #wk, dateadd (year, #yr-1900, 0)) )
SELECT
replace('total for ' + convert(char(10), dateadd(day, -6, #EndOfWeek) , 110)
+ ' to ' + convert(char(10), #EndOfWeek, 110), '-', '/')
The result:
total for 01/04/2010 to 01/10/2010
I hope it helps:
declare #year char(4) = '2014'
declare #week int = 2
select dateadd(week,#week,convert(date,#year+'-01-01',121))
Change the date format appropriate for you from this list

How to get full date from the given month and year in SQL SERVER?

I have following script where I need to get the full date
DECLARE #BeginDate DateTime
DECLARE #EndDate DateTime
Declare #month int
Declare #year int
set #month = 6
set #year = 2014
Select beginDate = CAST(#month AS VARCHAR(10)) + '/01/' + CAST(#year AS VARCHAR(10))
SELECT endDate = DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, #beginDate) + 1, 0))
This script returns endDate as null. How do I get full endDate by passing only Date (not time)?
I think you forgot to put # sign in front of the variables. If you change the last two lines with the following:
Select #beginDate = CAST(#month AS VARCHAR(10)) + '/01/' + CAST(#year AS VARCHAR(10))
SELECT #endDate = DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, #beginDate) + 1, 0))
it should work. By putting "select #enddate" at the end, I got 2014-06-30 00:00:00.000 .
Well I found it.
DECLARE #BeginDate DateTime
DECLARE #EndDate DateTime
Declare #month int
Declare #year int
set #month = 7
set #year = 2014
Select #beginDate = CAST(#month AS VARCHAR(10)) + '/01/' + CAST(#year AS VARCHAR(10))
SELECT endDate = DATEADD(d, -DAY(DATEADD(mm, 1, #beginDate)), DATEADD(m, 1, #BeginDate))
source

Date calculation in variable

I'm doing my best to set a date variable so I can compare it later. I would like something that basically says:
If the current day-of-month is less than 11, then date is 10th of LAST month
If the current day-of-month is greater-than-or-equal-to 11, then date is 10th of THIS month
Date is 11/6/2012 expected output:
#PODate = 10/10/2012
Date is 11/16/2012 expected output:
#PODate = 11/10/2012
Currently all I have is this:
DECLARE #PODate as DATETIME
Set #PODate = Convert(varchar(8),GetDate(),1)
Any tips or help would be greatly appreciated. Thank you!!
Trying to keep it as straightforward as possible:
declare #PODate datetime
select #PODate = DATEADD(month,
DATEDIFF(month,'20010110',CURRENT_TIMESTAMP) +
CASE WHEN DATEPART(day,CURRENT_TIMESTAMP) <= 10 THEN -1 ELSE 0 END,
'20010110')
The surrounding DATEADD/DATEDIFF pair are being used to normalize the date to the 10th of the current month. We then use a small CASE expression to subtract a month if the day is less than or equal to the 10th.
Whatever solution you pick, please try to avoid ones that do it as string manipulation. The usual cause of datetime related bugs in SQL is when people treat dates as strings. Keeping the data types appropriately is usually the best way to prevent these issues.
There are, admittedly, 2 strings in my solution, but these are fixed constant strings (all that matters is that they're both for the same year and month, and the second one is for the 10th of the month) and are in an unambiguous format.
Try this: SQL Fiddle
DECLARE
#PODate as DATETIME,
#LastMonth as DateTime,
#strDate as Varchar(50)
set #PODate = '11/16/2012'
set #LastMonth = DATEADD(MONTH, -1, #PODate)
if(DAY(#PODate) < 11)
SET #strDate = CAST(MONTH(#LastMonth) AS VARCHAR)+'/10/'+CAST(YEAR(#LastMonth) AS VARCHAR)
else
SET #strDate = CAST(MONTH(#PODate) AS VARCHAR)+'/10/'+CAST(YEAR(#PODate) AS VARCHAR)
Select CAST(#strDate AS DateTime)
DECLARE #PODate date = '20121116'
SELECT CASE WHEN DATEPART(day, #PODate) < 11 THEN DATEADD(mm, DATEPART(mm, GETDATE()) - DATEPART(mm, #PODate) - 1, DATEADD(day, 10 - DATEPART(day, #PODate), #PODate))
ELSE DATEADD(mm, DATEPART(mm, GETDATE()) - DATEPART(mm, #PODate), DATEADD(day, 10 - DATEPART(day, #PODate), #PODate)) END
Demo on SQLFiddle
DECLARE #currDate DATE = dbo.GetDate()
DECLARE #day INT = day(#currDate)
DECLARE #month INT
DECLARE #year INT
DECLARE #PODate DATE
IF( #day >= 11)
BEGIN
SET #month = month(#currDate)
SET #year = year(#currDate)
END
ELSE BEGIN
SET #month = month(dateadd(m,-1,#currDate))
SET #year = year(dateadd(m,-1,#currDate))
END
SET #PODate = convert(DATE,'10-' + convert(VARCHAR,#month) + '-' + convert(VARCHAR,#year))
PRINT #PODate
if #currDate = '11-jan-2013' , #PODate will be '10-jan-2013', and if #currDate = '07-jan-2013' , #PODate will be '10-Dec-2012'