SQL Server - notify 30,15, 7 days from time - sql-server

I'm trying to right an SQL Query that will do the following task:
Example:
ExpirationDate = '2018-04-30 00:00:00.000'
when the property is set to expire in 30 days ... 1 month before, I want SQL Server to email me informing that the item is about to expire based upon (Expirationdate)
could someone please give me some pointers as to where I go next... this is what I have tried so far..
the query below will find the item however I now want SQL Server Agent to send me an email for each row that is returned, not when nothing is returned
SELECT *
FROM [dbo].[Property]
WHERE expirationdate <= DateAdd(day ,30 , GetDate())

Here is an excellent link on how to email results from a sql server job agent:
https://www.brentozar.com/archive/2014/10/send-query-results-sql-server-agent-job/
As for your query: I wouldn't SELECT *. Explicitly select the columns you want returned in your email response.
Next I would define a variable to hold the value of your cutoff, this will prevent you doing a calculation for each row within the query. Example:
DECLARE #day30UpperBound DATETIME = DATEADD(Day, 31, cast(getdate() as date));
DECLARE #day30LowerBound DATETIME = DATEADD(Day, 30, cast(getdate() as date));
Then you can query results with:
SELECT explicitColumnList FROM [dbo].[Property]
WHERE expirationdate BETWEEN #day30LowerBound AND #day30UpperBound
This specific example should give you those Properties with an expiration date that fall in the window of 30 days from current at midnight, to 31 days from current at midnight - so a full 24 hour window 30 days from current day.
You can easily reproduce this for your 15 day, and 7 day reports. You could put all 3 reports into one job agent. Or you could make a different job for each of the 3.
You'll have to research a little on how to do only business days if that's what you want.

Related

SQL Server Dateadd 5 Min Ago

I work flood protect script and i need check user submit form last 5 minutes.
My SQL Server datetime column value is 2016-02-16 20:01:56.000
and i tried;
select number from person where number='111111111' and date > dateadd(minute, -5, '2016-02-16 18:01:56.000')
or
select number from person where number='111111111' and date > dateadd(mm, -5, getdate())
This query always getting value is '111111111' i change time, example '2016-02-16 15:01:56.000' or '2016-02-16 12:01:56.000' but getting 111111111 value already?? Where is my wrong?
EDIT
I found problem, insert and set datetime from my local time but my server in a another country, and time zone different 10 hour :)
You have the wrong datepart
minutes = mi,n
month = mm

How to get last 5 minutes of data with datetime data type in SQL Server

I would like to run a query against data that is being consumed by an Esri ArcGIS Server SQL Server database, the data reads as 4/15/2015 4:21:45 PM when I use
SELECT *
FROM ServiceRequest.DBO.History_Table
WHERE: Time = CONVERT(DATE, GETDATE())
I return all records with today's date, but how would I extend this so that I retrieve the last 5 minutes? My History_Table column is a date data type.
You should query from the parameter less five minutes using the DATEADD function
https://msdn.microsoft.com/en-us/library/ms186819.aspx?f=255&MSPPError=-2147217396
Then use the minutes parameter
WHERE [dateColumn] > DATEADD(minute, -5, GETUTCDATE())

SQL query to search

i am new at sqlserver and i just started learning it , and i have a question .
**i want an SQL query that can bring up all rows that were added in the past 6 months. in table 'users' the date is in field 'joined' but its in UNIX time stamp format
**and i want then like another SQL query the same as above but it also with the results sets the field 'activate' in each row to 'yes'
i tried that code :
select * from users where time_of_post is like '07/**/2011'
** = anyday but i can`t implement it right .
Thank you a lot for your help in advance .
select *
from users
where datediff(mm, time_of_post, getdate()) <= 6
What you want to use is the DATEDIFF() function, and get the difference between today's day (GETDATE()) and the stored dates (time_of_post). If that is less than or equal to 6 months (as denoted by the first parameter, mm) then that should be what you're looking for.
EDIT:
If you're looking to use this logic for an UPDATE, you'd do something like this:
update users
set activation = 'yes'
where datediff(mm, time_of_post, getdate()) <= 6
and activation <> 'yes'

TSQL: Date BETWEEN Query - Ignoring Time

I am trying to do a query between 2 dates. I would like to do it without having to worry about the time. When a user enters the 2 dates they want to search on, there is no selection for time. This means that the dates that they enter default to 12:00 AM.
The dates in the table do have times though. I just would like to ignore the times all together so the search brings back any records form said date range.
Here is my SQL:
TheDate BETWEEN #EnteredBeginDate AND #EnteredEndDate
So when a user does a range search between 8/6/2009 AND 9/9/2009 I want to return the records:
8/6/2009 11:33:02 AM
8/6/2009 11:39:17 AM
9/9/2009 8:21:30 AM
What's happening now is I only get back:
8/6/2009 11:33:02 AM
8/6/2009 11:39:17 AM
Can someone please recommend the best way to do this in SQL? I know how to do it in C#.
Just use DATEADD for the enddate to set it to midnight on the NEXT day...
TheDate BETWEEN #EnteredBeginDate AND DATEADD(day, 1, #EnteredEndDate)
If you want to be really precise, you could subtract a second or millisecond from that to make it 11:59:59 on your specified date:
TheDate BETWEEN #EnteredBeginDate AND DATEADD(second, -1, (DATEADD(day, 1, #EnteredEndDate)))
If you're on SQL Server 2008 or 2008 R2, you could use the new DATE datatype:
TheDate BETWEEN CAST(#EnteredBeginDate AS DATE) AND CAST(#EnteredEndDate AS DATE)
That strips off the time portion and looks only at the date
If you are using SQL Server 2008, then do this:
TheDate BETWEEN cast(#EnteredBeginDate as date) AND cast(#EnteredEndDate as date)
TheDate BETWEEN #EnteredBeginDate AND dateadd(day, 1, #EnteredEndDate)
If just the date is passed into SQL Server, it will make the time 12:00 AM. So, the end date on the the between clause is 9/9/2009 12:00 AM. Just add a day or modify the date passed into the query to include the correct time.

SQL Date Subtraction Between Two Years

I am having an issue with a query I am trying to convert from MS Access. The query flags record for removal when it is older than 90 days but when I convert this query to sql server is is removing too many records.
UPDATE DT.SM_T_CountTotals
SET IsActive = 0
WHERE Convert(varchar, DT.SM_T_CountTotals.PostDate, 101) <
Convert(varchar, GetDate()- 90, 101)
When I run this query in MS Access I get a total of 3793 records that are flagged but in SQL server I get 69061 records that are flagged for removal. The GetDate()-90 value is correct at 10/26/2010 but it is flagging everything from this year to be removed.
I am sure it is something easy that I am overlooking. Help please?
I figured it out:
UPDATE DT.SM_T_CountTotals
SET IsActive = 0
WHERE DT.SM_T_CountTotals.PostDate < Convert(varchar, GetDate()- 90, 101)
You're comparing VARCHAR values, not DATEs.
101 converts to MM/DD/YY, so you're comparing month, then day, then year.
You should be using 112 (yymmdd)
Calculations between two dates can be easily done in the native data type rather than convert it to string. One can (and you have) get incorrect answers from such conversions.
Use DateDiff in the where clause to get the records that are more than 90 days old.
http://msdn.microsoft.com/en-us/library/ms189794.aspx
UPDATE DT.SM_T_CountTotals
SET IsActive = 0
WHERE ABS (DATEDIFF (dd, Getdate(), DT.SM_T_CountTotals.PostDate)) > 90

Resources