SQL Select - Current Date - 7 Year - sql

Hi I am trying to get (current Datetime - Years).
Below is my Query..
print getdate()
print (getdate()-(365.25*7))
Result:
Dec 30 2013 10:47AM
Dec 30 2006 4:52PM
is giving correct result.
But When i try
print getdate()
print (getdate()-year(7))
Result:
Dec 30 2013 10:52AM
Oct 17 2008 10:52AM
Can anyone please tell what is wrong in it?

Rather use DATEADD with the datepart set to YEAR.
Returns a specified date with the specified number interval (signed
integer) added to a specified datepart of that date.
Something like
SELECT GETDATE() Today, DATEADD(YEAR,-7,GETDATE()) Today7YearsAgo
SQL Fiddle DEMO
The year(7) part return 1900, which is the year part of 1900-01-08 00:00:00.000 (CAST(7 AS DATETIME)). Then getdate()-year(7) equates to getdate()-1900, which is a day subtraction.

Try this
print getdate()
print DATEADD(Year, -7, GETDATE())

DATE ADD() Returns a specified date with the specified number interval (signed integer) added to a specified datepart of that date.
print getdate()
print DATEADD(Year, -7, GETDATE())

Try MSSQL DATEADD function
select DATEADD(Year,-7,GETDATE());

Related

SQL Server : DATEADD 29 February not return 30,31 January

I'm working in SQL Server 2014 and need calculate exactly one month previous from a SQL Server DateTime, but I'm unable figure out how to use DATEADD correctly.
Examples:
SELECT DATEADD(MONTH, -1, '20200229')
returns 2020-01-29.
SELECT DATEADD(MONTH, -1, '20200301')
returns 2020-02-01.
But in this second statement, I would like to get 2020-01-30 and 2020-01-31
Any ideas?
Seems you want end of previous month, in that case add EOMONTH() to your script which goes as follows
Select EOMONTH( DATEADD(MONTH,-1,'20200229'));
Select EOMONTH( DATEADD(MONTH,-1,'20200301'));
From the docs:
If the following are true:
datepart is month the date month has more days than the return month
the date day does not exist in the return month
Then, DATEADD returns the last day of the return month.
For example, September has 30 (thirty) days; therefore, these statements return 2006-09-30 00:00:00.000:
SELECT DATEADD(month, 1, '20060830');
SELECT DATEADD(month, 1, '20060831');
2020 is a Leap Year. Most of the time your 2/29 would not work anyway.
Also there is no 2/30 & 2/31 so 1/30 & 1/31 would not appear.
Use the EOMONTH() function with the optional second parameter, which is an int and is the number of months difference to the date:
select eomonth('2020-02-29', -1);
Returns:
2020-01-31
Kindly try this.
SELECT DATEADD(DAY,1,DATEADD(MONTH, -1, '20200229')), DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, DATEADD(MONTH, -2, '20200301')) + 1, 0))

SQL datediff with negative parameter

I am a complete noob to SQL and trying to understand why this query returns "115":
select datediff(yy, -3, getdate())
datediff takes three Parameter. The first is the interval, the second the start date and the third the end date. You are passing -3 as start date, there we can show:
SELECT CAST(-3 AS datetime) -- results in '1899-12-29 00:00:00.000'
And because 2014 - 1899 is 115, you will get this as result.
Because DATEDIFF() calculates an interval between 2 dates, and you specified year -3 in one of them.
Firstly, the date "zero" is 12/30/1899 on SQL server.
Secondly, your "-3" was an incorrect date format and it thus replaced it with its 0
2014 - 1899 = 115
Use DATEADD() instead to achieve what you want to do.
DateDiff gives difference in years/date/months etc based on what you have given in first parameter. second and third parameter are datetime values which will be used to calculate the difference i.e. (param2 datetime value - param3 datetime value).
Now in your case param2 is "-3"
run this queries in you ms sql and observe the outputs :
select CAST(-3 as datetime)
select GETDATE()
select datediff(yy, -3, getdate())

Break this simple SQL/HQL Query down - DATEADD, DATEDIFF?

select * from table
where Date BETWEEN DATEADD(dd, DATEDIFF(dd, 84, GETDATE()),0)
and DATEADD(dd, DATEDIFF(dd, 77, GETDATE()),0)
Can someone please explain what this query is doing? Specifically, what is the function of DATEADD and DATEDIFF in this query?
They're trying to get the membership renewals between 84 and 77 days ago, stripping out the time.
GETDATE() returns the datetime but the user isn't interested in the time.
DATEDIFF(dd, 84, GETDATE()) gets the number of days between the current date and the 84th day after Jan 1, 1900.
DATEADD(dd, # days from above, 0) adds those number of days to Jan 1, 1900.
The net is you get 84 days ago at 00:00:00 AM.
If you just did DATEADD(dd, -84, GETDATE()) then you'd have 84 days ago + the current time.
Other ways to do the same thing are to cast the datetime to a date (assuming MS SQL Server).
.. CAST((GETDATE() - 84) AS DATE)
.. CAST(DATEADD(day, -84, GETDATE()) as DATE)
Your expression is using SQL Server syntax to convert a datetime to a date (that is, to remove the date component. The format is quite arcane, because the expression is doing getdate() - 84 as an integer and then adding this to the 0 date. The elimination of the "time" component occurs because the datediff() returns an integer.
I don't think this will work in HQL. For instance, the dd should be 'dd' and HQL has a different method of getting the current date.
And, in more recent versions of SQL Server, you can just do cast(getdate() - 84 as date).

Select data from today until the last 15th of month?

I want to select a range of data from today until the last 15th (so this month or last month). How would this be done in TSQL?
Following Get last Friday's Date unless today is Friday using T-SQL it seems like I would have to make use of DATEDIFF at the least.
I'm considering building a date string, like:
set #date='yyyy-mm-15'
But yyyy or mm would not be able to be simply the current year/month, in case it has to look back a month/year.
This should give you what you want
SELECT *
FROM YourTable
WHERE YourDate > CAST((
CASE
WHEN DAY(getdate()) < 15
THEN (CAST(YEAR(GETDATE()) AS CHAR(4)) + CAST(MONTH(dateadd(month, - 1, getdate())) AS CHAR(2)) + '15')
ELSE CAST(YEAR(GETDATE()) AS CHAR(4)) + CAST(MONTH(GETDATE()) AS CHAR(2)) + '15'
END
) AS DATETIME)
EDIT
changed WHEN DAY(getdate()) > 15 to WHEN DAY(getdate()) < 15, as the results were inverted :)
Format the current date in the format yyyy-mm-15, convert back to date, compare to the current date and if this is later that today, subtract 1 month.
That's what I'd do in ANSI SQL... But apparently, MS SQL Server doesn't know "interval". Well, you can make use of DateAdd instead:
DateAdd() - Returns a new datetime value based on adding an interval to the specified date.
Where first parameter specifies on which part of the date to return a new value: yy(yyyy) for Year, mm(m) for Month, dd(d) for Day etc.
For example: select dateadd(d,2,getdate()) - adds 2 days to current date and returns new date.

DATEDIFF Getting the previous month

I want to get the previous month relative to the current date
SELECT datediff(mm,-1,2-2-2011)
This query gives 67 which is a wrong value .. where i went wrong ?
You can use DATEADD
eg.
SELECT DATEADD(month, -1, GETDATE())
This 2-2-2011 is not a valid date literal - you are subtracting 2 from 2 and then 2011 from the result - proper date literals are '2-2-2011' and #2-2-2011#. You can use GETDATE() to get the current date, instead of relying on a literal.
Nor should you be using DATEDIFF - it gives you the difference between dates.
You should be using DATEADD to calculate new dates.
Try this:
SELECT DATEADD(mm,-1, GETDATE())
This will get the date a month ago.
If you just want the month, you need to also use DATEPART:
SELECT DATEPART(mm, SELECT DATEADD(mm,-1, GETDATE()))
SELECT datepart(mm, dateadd(mm,-1,'2011/1/1') )
If you want the month before the current month, you want
SELECT MONTH(DATEADD(mm, -1, GETDATE()))
If you want the date for a month before the current date, you want
SELECT DATEADD(mm, -1, GETDATE())
BTW, SELECT datediff(mm,-1,2-2-2011) computes the number of months between day -1 and day -2011, which is 67 (2010 / 30). That's nowhere near what you seem to actually want.
You need to use DATEADD - not DATEDIFF
DATEDIFF calculates the difference between two dates - it doesn't add day or months to an existing date....
Also, you need to put your dates into single quotes: use '2-2-2011' instead of simply 2-2-2011.
And lastly: I would strongly recommend using the ISO-8601 date format YYYYMMDD (here: 20110202) - it will work regardless of the language and regional settings on your SQL Server - your date format will BREAK on many servers due to language settings.
DATEDIFF calculates the difference between your stating and ending dates
every date previous to the current date has a positive number and every date next to the current date has negative number, this works in geting your specific date weather it a day,month,year or hour to understand this better below is the syntax of datediff
DATEDIFF (your datetime type, your starting date,your ending date)
the function does (your ending date)-(your starting date)
in your case the below datediff will work just pefectly
SELECT DATEDIFF (month,[you_date_or_datetime_column],GETDATE()) = 1
You can use DATEADD try this code
For previous month
SELECT DATEADD(month, -1, GETDATE())
For 7 day previous
$date = date('Y-m-d',strtotime("-7 days"));
SELECT * FROM users WHERE `date` LIKE '%$date%'
$date varibale get previous date date