I am using DATEADD(year, -3, GETDATE()) to pull the last thee years worth of data on a rolling period.
I also have a column called Year, the view I am creating is using the MyTable.Year and not the sql keyword 'year'.
e.g DATEADD(MyTable.year, -3, GETDATE())
It resolves it every time in the view which is really annoying. I'm a bit rusty, been out of this for about 4 years.
How do I make sure it uses the keyword 'year', I find it strange it is doing this. Any explanation on this would also be helpful. SQL Server 2016
Thanks guys.
EDIT: I have edited my misplaced schema notation and identified the table name, sorry for confusion
I am not sure if I understand correctly, but you want to enforce the keyword, so you want to subtract 3 years from the current date, correct? This should work:
SELECT DATEADD(yy, -3, GETDATE())
or
SELECT DATEADD(yyyy, -3, GETDATE())
(unless you have columns named yy and yyyy ;-))
Otherwise forgive my misunderstanding...
It seems your editor has problems with the keyword YEAR and replaces it with a value from a column that is also called year
This can be solved by using a synonym for the keyword year in the DateAdd function.
So instead of
dateadd(year, -3, getdate())
use
dateadd(yy, -3, getdate())
You could put your table in a subquery and alias [Year] column.
Something like
SELECT ... FROM (SELECT [Year] AS "MyYear" FROM ...) X
(or use CTE)
Then Year would only mean the keyword.
Related
I have to check if a user is old enough. I tried as shown here, but this only works for years.
So, for example 11/7/2003 should be true but 12/12/2003 should be false. But, with my code each of these is true.
Here is my code:
[birthdate]date CHECK(DATEDIFF(year,birthdate,getdate()) >= 18),
How can I write this in another way that the months and days will matter?
Instead of doing arithmetic on the column and checking the result, do arithmetic on GETDATE and do a normal comparison
[birthdate]date CHECK(birthdate <= DATEADD(year, -18, getdate())),
This is good practice in any case for WHERE and JOIN predicates, as it means indexes can be used.
Want to find people who are at least 18, given then date of birth?
SELECT Cast(CURRENT_TIMESTAMP AS date) AS today
, DateAdd(yy, -18, Cast(CURRENT_TIMESTAMP AS date)) AS eighteen_years_ago
;
Anyone born on or before eighteen_years_ago is at least 18 years old.
Check the number of days from date A to B and replace the condition with >= 6570 (365*18).
This method does not check for leap years.
need quick help here.
I'm coming up with a column to track how many days a event is back-logged.
So I'm using this syntax which is working
DATEDIFF(DAY, GETDATE(), ESI.EventStatusDate) AS BackloggedDays
However, the modification I want to use is, the event status date + 3 days. Any ideas how i would add that to this syntax. Thanks
I think you need DATEADD() :
DATEDIFF(DAY, GETDATE(), DATEADD(DAY, 3, ESI.EventStatusDate)) AS BackloggedDays
We've been using MS Access, with the following syntax for MTD Data that works for us:
Between DateSerial(Year(Date()),Month(Date()),1)
And DateSerial(Year(Date()),Month(Date())+1,0)
We need to transition the above logic to SQL/SSRS for automatic emailed reports, but I cannot get this DateSerial logic to work with SQL.
In the Filter field of the SQL query, I can successfully use BETWEEN '8/1/2014' AND '8/31/2014' for MTD data, but would like to have a DateSerial logic applied so that reports don't need to be created for every month, quarter, year, etc.
When trying to use the DateSerial function, we get the error "Invalid or missing Expression". I've seen a few topics on this that Parameters are required, but really believe that this is a simple syntax issue for the filter field, since actual dates work with the BETWEEN command.
There are several different ways to get this. Here is just one way.
Get todays date. In this case 8/27/2014
Declare #Today date = cast(getdate() as date)
Get the first of the month, 26 days in the past
Declare #StartDate date = dateadd(d, -1 * (day(#Today) - 1), #Today)
select #Today, #StartDate
You can use the function CONVERT:
http://msdn.microsoft.com/en-us/library/ms187928.aspx
Or the function DATEFROMPARTS if you are using SQL Server 2012:
http://msdn.microsoft.com/en-us/library/hh213228.aspx
Or DATEADD:
select DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0); -- first day of current month
select DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE()), -1) -- last day of current month
This last one I took from: https://stackoverflow.com/a/11746042/1274092
See mine at:
http://sqlfiddle.com/#!3/d41d8/38333
This has been resolved. The ODBC driver does not apparently play well with SSRS. The DateSerial command would not work within the query itself. The workaround was to add the filter to the Dataset. This syntax is what works, but again only in the Dataset filter: [expression] Between [first value box] =DateSerial(Year(Now()),1,1) [second value box] =DateSerial(Year(Now()),12,31)
This gives us the YTD reporting data that we require.
I have an SSRS report which uses a #Year parameter, which is chosen by the user at run-time.
Fine, so far, but the SQL in the Data set Properties section contains a hard-coded date of '2010-08-31' , but, the year part of it needs to be the same as the #Year parameter which the user chooses. In other words, if you run the report in the year 2010 the results will be correct, but not if you run it now (in 2014).
The SQL at the moment is (miminum required):
SELECT DateDiff(Year, birth_dt, '2010-08-31')
--Date of Start of Academic Term
FROM table99
WHERE acad_period = #Year
...so my question is, what is the correct syntax for substituting the #Year value in place of '2010'?
EDIT : Please note that the actual format for the year is (eg) 12/13, 13/14
you can replace the line
SELECT DateDiff(Year, birth_dt, '2010-08-31')
with
SELECT DateDiff(Year, birth_dt, #Year+'-08-31')
To do the same with current date
SELECT DateDiff(Year, birth_dt, DATEPART(yyyy, getdate())+'-' + DATEPART(mm, getdate()) +'-'+DATEPART(dd, getdate()))
based on your clarification in comment, if you pass in '12/13' your query would be something like this.
SELECT DATEDIFF(Year, birth_dt, '20'+LEFT(#Year,2) + '-08-31')
FROM table99
WHERE acad_period = #Year
Since your year parameter isn't a simple year value but is instead a string like "13/14" presumably meaning the 2013/2014 school year, I would definitely handle parsing it outside of the query.
Add a computed #TermStart parameter to the dataset with the following formula:
=DateSerial(2000 + CInt(Split(Parameters!Year.Value,"/")(0)),8,31)
(So long as you aren't expecting any dates prior to 2000 of course)
Then you can use the #Year and #TermStart parameters in the query like so:
SELECT DateDiff(Year, birth_dt, #TermStart)
--Date of Start of Academic Term
FROM table99
WHERE acad_period = #Year
But as I mentioned in a comment above, that is not the correct way to calculate age. There are several ways to do that. My favorite is this:
SELECT datediff(hour,birth_dt,#TermStart)/8766
--Date of Start of Academic Term
FROM table99
WHERE acad_period = #Year
I'm working on a purging procedure on SQL Server 2005 which has to delete all rows in a table older than 1 year ago + time passed in the current year.
Ex: If I execute the procedure today 6-10-2009 it has to delete rows older than 2008-01-01 00:00 (that is 2007 included and backwards).
How can I get the date of the first second of the year?
I've tried this:
select cast((DATEPART(year, getdate()) -1 )AS DATETIME);
but I get 1905-07-02 00:00:00.000 and not 2008-01-01 00:00 (as I wrongly expected).
Can someone help me, please?
EDIT: This was returning current year, when the question was for previous year. Code has been corrected to reflect this.
use this
select DATEADD(yy, DATEADD(yy, DATEDIFF(yy,0,getdate()), 0), -1)
OR to use your variable:
select DATEADD(yy, DATEADD(yy, DATEDIFF(yy,0,#YourDateTimeValue), 0), -1)
This will work:
select cast('01 jan' + CAST((DATEPART(year, getdate())-1) as varchar) AS DATETIME);
(I know it's not the "best" solution and probably involves more casts than necessary, but it works, and for how this will be used it seems to be a pragmatic solution!)
SELECT DATEADD(year, DATEDIFF(year, 365, GETDATE()), 0)