Retrieve SQL Records Based on Past Date? - sql

I have some database records, and one of the columns contains the date the record was added (called COMP_DATE).
I need to make a query, that will run each day, which selects records whose COMP_DATE is exactly n years, 11 months, and 15 days ago (within a 24 hour window). (I.E. 15 days before n years ago).
What would be the best way of doing this? Should I just subtract (n*356 - 15 days)? How would I accomplish this?
Thanks.

the best way will be to do it the other way around to obtain n years 11 months and 15 days ago
Remove n+1 years and add 15 days, you'll have the less room for errors
DECLARE #n as INT=<your value here>;
SELECT *
FROM records
WHERE comp_date BETWEEN Dateadd(DAY, 15, Dateadd(YEAR, -#n-1, Getdate()))
AND Dateadd(DAY, 16, Dateadd(YEAR, -#n-1, Getdate()));
Hope that helped ;)

There are functions to do date arythmetic. For example, Dateadd
(Assuming this is Microsoft SQL Server based on the question's tags.)

select * from records where
comp_date = dateadd(year,-n,dateadd(month,-11,dateadd(days, -15, GetDate())));
*assumimg comp_date is of type Date.

if comp_date is datetime column type, you'll want to search on a range (a 24 hour range). Also, if you are looking for 15 days before n years ago, the query should look like this:
select * from records where
comp_date >= dateadd(year,-n,dateadd(day, -15, getdate()))
and comp_date < dateadd(year,-n,dateadd(day, -14, getdate()));
If indeed you are wanting n years 11 months and 15 days ago, use WOPRs answer.

Related

SQL/BIRT how to show entries greater than or equal to 30 days old?

I am trying to create a BIRT report that shows me tickets 30 days or older from when they were created
I have:
SELECT
sta.ticket_no,
sta.status_date
FROM
table1 sta
WHERE sta.status_date <= DATEADD(day, -30, sta.status_date)
It runs, but it does not return anything. We definitely have tickets older than 30 days.
Ideally, it would return the ticket number and the date it was created (so long as it was submitted 30 days or more ago)
I would suggest you calculate the difference using 30 days prior to today - using the native getdate() function
SELECT
sta.ticket_no,
sta.status_date
FROM
table1 sta
WHERE sta.status_date <= DATEADD(day, -30, getdate())

SQL datediff - People older than

I have an SQL table called member in which I have member information and in that information, their birth date.
I need to get a list of all the people that are more than 25 years old. For now, I got this :
SELECT * FROM members
WHERE DATEDIFF(year, birthday, GETDATE() ) > 25
The only thing is that it doesn't take into consideration all the people that turned 25 this year so far...
How do I add to this everyone that turned 25 in January and February of this year as well ?
Would someone be able to help me out ?
Thanks !
Don't use datediff(). Just subtract the years from the current date:
where birthday < dateadd(year, -25, getdate())
Try adding 25 years to their birthday and then comparing to now. If <=, then they are older than 25.
SELECT *
FROM members
WHERE DATEADD(year, 25, birthday) <= getdate()

Using Dates as a SQL Filter

I have a table of data that goes quite some time back, but I only want to results in my query for the last 13 weeks. There is a date column in that table.
I can use
SELECT DATEADD(Week, -13, GETDATE())
to get the date of 13 weeks back as a separate query - but I am having trouble linking that into the initial select to only return me the last 13 weeks of data. I have used that query as the data should refresh every day to go back 13 weeks to that date.
Is there any way I can do that?
Thanks in advance
This should be what you are looking for:
SELECT *
FROM TABLE_NAME
WHERE date_field >
(SELECT DATEADD(Week, - 13, GETDATE()))
I'm a bit confused on what your issue is. You should be able to use the dateadd() in your where clause:
SELECT *
FROM TABLE
WHERE DATECOLUMNTOCOMPARE > DATEADD(WEEK,-13,GETDATE())

What is happening in this query?

I am trying to get the last of month, and in order to that i have written the following, to calculate the no. of days between today and the last date.
select datediff(DAY,GETDATE(),dateadd(m,1,getdate()))-GETDATE()
the bold part gives me the no. of days between today and a month from today, say 30 or 31. and then I am subtracting today's date from 30 or 31, which is " -getdate() "
The output for the above query is
1786-06-06 11:44:30.540
Could you please explain what is happening in the query? I am not looking for a solution, I would like to know how is SQL-Server interpreting the query.
Thanks. :)
The bold part of the expressions does not return a date, it returns a number of days:
31
Convert that to a datetime:
SELECT CONVERT(DATETIME, 31);
This is 31 days after day 0 (1900-01-01):
1900-02-01
Now, subtract GETDATE() as an integer (41512 days after day 0):
SELECT 31 - 41512 = -41481
Now add -41481 days to day 0:
SELECT DATEADD(DAY, -41481, 0);
-- or
SELECT DATEADD(DAY, -41481, '19000101');
Or:
SELECT CONVERT(DATETIME, 31 - CONVERT(INT, GETDATE()));
Now, I strongly recommend a couple of things:
Don't use implicit date math. #date_var_or_col - 1 for example fails with new data types like DATE and DATETIME2.
Don't use shorthand like m. If you mean MONTH, just take the massive productivity hit and type out MONTH. To see why, tell me if this provides the results you expect:
SELECT DATEPART(y, GETDATE()), DATEPART(w, GETDATE());
I am subtracting today's date from 30 or 31, which is " -getdate() "
Sounds like you understand exactly what is happening, but maybe don't understand the results.
You are implicitly converting GETDATE() to a number, which represents the number of days (and fractional days) since 1/1/1900 12:00:00 AM
When you "subtract" GETDATE() (41,511 as of 8/27/2013) from 30 or 31 you get an answer of -41,480, or 41,480 days before 1/1/1900, which would be about 6/6/1786 (plus or minus a few hours for the fractional part).

problem with SQL query in DB2

How can i write a query in DB2 for following thing:
The difference between current timestamp and a timestamp field in dB should be >=4 hours AND
<= 24 hours
You don't really give enough information to answer the question (i.e., do you want data only from the past, only in the future, etc), but let's assume you want the data where the timestamp column ("tscolumn") is more than 4 hours old and less than 24 hours old:
select *
from table t
where t.tscolumn between current timestamp - 4 hours
and current timestamp - 24 hours
If my assumption is wrong it's pretty easy to rewrite this to meet your requirements.
Try following
select * from tableName where
date <= DATEADD(Hour, -4, CURRENT_TIME) and
date date >= DATEADD(Hour, -24, CURRENT_TIME)
select *
from table t
where timestampdiff(8,char(current timestamp - time_from_table)) between 4 and 24
here timestamp(8, - refers hours, below are the values for different arguments.
Value Description
1 Fractions of a second
2 Seconds
4 Minutes
8 Hours
16 Days
32 Weeks
64 Months
128 Quarters
256 Years