I am working on a stored proc and need last 9 months data. Need a syntax Which will automatically deletes the oldest data when new data will be added to the table(Latest 9 months data).
That syntax is gonna be used in Select Syntax.
I have used
select * from tablename t
left outer join calendartable r on
t.fiscal_month=r.fiscal_month
where t.date > dateadd(m,-9,date)
I know it is wrong. Could you guys please help me with this.
Thanks
You probably want GETDATE to calculate the nine month boundary from now:
where t.date >= dateadd(m,-9, GETDATE())
Beware that if t.date is a date and time field not just date you'll see odd behaviour on the nine-month boundary unless you also round away the time before the comparison.
Or if you're comparing it against another value e.g. the date of the inserted record in your trigger then what you've got is probably OK, e.g. something like
declare #latest date
select #latest = inserted.date
delete from ... where t.date < dateadd(m, -9, #latest)
although I suggest you actually archive off the data, not delete it.
Since you've clarified you want whole months, i.e. 9 months from the end of last month, you could use
declare #today date;
declare #firstOfMonth date;
declare #nineMonthsAgo date;
set #today = GETDATE();
set #firstOfMonth = DATEADD(d, 1-DAY(#today), #today);
set #nineMonthsAgo = DATEADD(m, -9, #firstOfMonth);
... WHERE date >= #nineMonthsAgo AND date < #firstOfMonth
seems like it's pretty close. Do you need 9 months from right now, or 9 months from a given date?
how about:
declare #date datetime
set #date = dateadd(m, -9, getdate()) -- 9 months from right now
select *
from tablename t
left outer join calendartable r
on t.fiscal_month=r.fiscal_month
where t.date > #date
If You need to delete the data when new data is added, you will need to do it in a trigger. And the sintax inside the trigger would be like this.
DELETE t
FROM
tablename t
left outer join calendartable r on
t.fiscal_month=r.fiscal_month
where datediff(month, t.date, GETDATE()) > 9
And the select to retrieve your data will be similar.
select * from tablename t
left outer join calendartable r on
t.fiscal_month=r.fiscal_month
where datediff(month, t.date, GETDATE()) < 9
Related
I am a bit of a noob with SQL, so I was searching for some bit of code that might help me find missing date values for a time interval when I stumbled upon this code.
DECLARE #StartDate DATETIME DECLARE #EndDate DATETIME
SET #StartDate ='2014-03-01' SET #EndDate = GETDATE()
;WITH Dates(Date) AS
(
SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, #StartDate)) AS Date
UNION ALL
SELECT DATEADD(day, 1, Date) AS Date
FROM Dates
WHERE Date <= #EndDate
)
SELECT d.Date, r.Value
FROM Dates d
LEFT JOIN Times r ON d.Date = r.Date
Link to the code
It works really well for my problem but I am not able to understand how it increments the date.
I would ask the author but their blog no longer exists and their twitter is inactive too.
Edit: someone said the post is lacking a question. I want to know how this CTE is recursively adding the +1 to each date from #StartDate to #EndDate.
This is a recursive CTE, or Common Table Expression.
The first line of of the CTE SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, #StartDate)) AS Date is the seed, or root portion. The next UNION section takes that date, adds a day, and recurses.
Notice that inside the Dates block you are selecting FROM Dates, so it will continue produce rows with an incremented date until the WHERE clause is satisfied.
I have multiple device IMEIs and they're sending data continuously. There's a table where the time can be seen when a device has sent the last data in the following format: 12/17/2020 4:05:02 PM. Now I want to get those devices which have sent data within last 4 months. I have got the joins but cannot understand the condition I need to make in the Where clause.
SQL Server is really flexible about converting strings to date, so you should be able to just convert() or cast(), and do direct filtering.
So:
where convert(datetime, mycol) >= dateadd(month, -4, getdate())
This filters on the last 4 months of data, starting from the current date/time. If you want entire months (the current month and the three preceding months)
where convert(datetime, mycol) >= dateadd(month, -3, datefromparts(year(getdate()), month(getdate()), 1)
Now I want to get those devices which have sent data within last 4 months.
This sounds like an exists. If you mean 4 months to the day and the datetime column is stored correctly as a datetime, then:
select d.*
from devices d
where exists (select 1
from table2 t2
where t2.imei = t.imei and
t2.datetime >= dateadd(month, -4, getdate())
);
If you mean in the current calendar month or the previous three, then:
select d.*
from devices d
where exists (select 1
from table2 t2
where t2.imei = t.imei and
t2.datetime >= dateadd(month, -3, datefromparts(year(getdate(), month(getdate()), 1))
);
If the "datetime" column is stored as a string, then fix the data. Use correct types in the table. You can convert to a datetime, because SQL Server will recognize the format:
select d.*
from devices d
where exists (select 1
from table2 t2
where t2.imei = t.imei and
try_convert(datetime, t2.datetime) >= dateadd(month, -4, getdate())
);
However, you should be storing such values using the correct database type.
I am not a SQL expert at all...we are trying to us the following to pull data from the previous week. I am fairly certain the 4 is incorrect, as we want data from Sunday through Saturday. The statement that was created was:
SELECT * FROM Table
Where [Date] Between DATEADD(wk,DATEDIFF(wk,7,GETDATE()),0) AND
DATEADD(wk,DATEDIFF(wk,7,GETDATE()),4)
Assuming you are working on Sql Server, you can use cte structure like below, set regarding data into cte and filter in your main query:
;with cte (ID) as ( --data from previous week only
Select Id from table
Where [Date] >= DATEADD(WEEK,-1,DATEADD(week,datediff(week,0,getdate()),0))
AND [Date] < DATEADD(week,datediff(week,0,getdate()),0)
)
select *
from table t
inner join cte on t.Id = cte.ID
where DATEPART(DW, [Date]) >= 0 --sunday
and DATEPART(DW, [Date]) <= 6 --saturday
In the filter, edit 0 and 6 to filter your data based on date as you wish.
If you use this, it returns previous Saturday and Sunday:
SELECT DATENAME(DW,(DATEADD(day, -6, getdate()))) ,DATENAME(DW,(DATEADD(day, -5,
getdate())))
[![Day Name][1]][1]
So, for example, you can use DATEADD(day, -6, getdate()) to get Saturday.
I need to ignore data anything that expected within the last 10 days back should be received if not ignore it. I am not sure how to write this in SQL. I have EXPDATE column. I am not sure my statement correct or not.
I believe the logic should be like this
Expected Date + 10 Days < Today`s date?
GETDATE() < DATEADD(DAY, +10, GETDATE()) - I found this online but where can I plug in my ExpectedDate column?
Thanks in advance!!
You can do it either way-
First add 10 days to EXPDATE and compare it with todays date like below.
select * from MyTable Where DATEADD(DAY, 10, EXPDATE) < GETDATE()
The other way, you can substract 10 days from today and compare it with EXPDATE.
select * from MyTable Where DATEADD(DAY, -10, GETDATE()) > EXPDATE
I would prefer 2nd one and would use variable to calculate 10 days back date as it's constant and then use it in where clause like below.
Declare #myDate datetime
SET #myDate = DATEADD(DAY, -10, GETDATE())
select * from MyTable Where #myDate > EXPDATE
I have a question. I have a SQL Server 2008 table with a field column. I have for example the Following dates:
1/1/2001
5/5/2004
8/5/2009
10/7/2011
5/5/2012
1/13/2014
Id like to be able to show all dates >= the current date (7/29/2011) as well as largest table date that is < current date. In this example, the result would be all dates >= 8/5/2009.
Can someone help guide me please??
select max(date) [date] from table where date < getdate()
union
select date from table where date >= getdate()
If I understand correctly, you want to include the date prior to the current date. GETDATE() will get the current date (with time). If you're alright with that, then this should work. Otherwise, you may have to parse out just the date from GETDATE()
SELECT TheDate
FROM DateTable
WHERE TheDate >= (SELECT MAX(TheDate) FROM DateTable WHERE TheDate < GETDATE())
This gets all dates greater than or equal to the most recent date before the current date.
I am not entirely sure I understand, but this looks like a BETWEEN the relevant dates. Or is there something more I am missing?
Assuming your table is called DateTable and your field is called TheDate, do it like this:
SELECT TheDate
FROM DateTable
WHERE TheDate >= DATEADD(d, -2, GETDATE())
Good luck!
It depends on the SQL server you're using. In postgres, for example, you need something like
SELECT fields FROM table WHERE date_field >= CURRENT_DATE - 1
But other SQL servers have different ways to specify "yesterday"
SELECT d1.*
FROM dates d1
LEFT JOIN dates d2 ON d1.Date < d2.Date AND d2.Date < GETDATE()
WHERE d2.Date IS NULL
Explanation:
Select every date for which there does not exist a date that is both earlier than today and later than the one being inspected.
Lots of guessing here based on loose narrative and unknown data types.
DECLARE #t TABLE(d DATE);
INSERT #t SELECT '20010101'
UNION ALL SELECT '20040505'
UNION ALL SELECT '20090805'
UNION ALL SELECT '20111007'
UNION ALL SELECT '20120505'
UNION ALL SELECT '20140113';
DECLARE #now DATE = SYSDATETIME();
WITH t AS
(
SELECT d, rn = ROW_NUMBER() OVER (ORDER BY d)
FROM #t
)
SELECT t.d
FROM t LEFT OUTER JOIN t AS x
ON t.rn = x.rn - 1
WHERE COALESCE(x.d, #now) >= #now
ORDER BY t.d;