SQL Server Dateadd 5 Min Ago - sql-server

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

Related

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

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.

SSRS How to define parameters as 7 days ago plus 1 hour

I have a report made using SSRS and SQL Server and I have set a StartDate and EndDate parameters for the report.
I have set the default value for StartDate as =DateAdd("D", -7, Today())
I have set the default value for EndDate as =DateAdd("H", 1, Today())
How can I add 1 hour to the StartDate Parameter?
I've tried
=DateAdd("H", 1, (DateAdd("D", -7, Today())))
Would that work? I'm currently testing it but the report usually takes 6 hours to run.
Do the date math in hours instead of days.
=DateAdd("H", -169, Today())
Would that work? You can always try that in SQL Server. The answer is yes :o)
Although, you should use the NOW() function instead of the TODAY() function (see the Update below).
SQL Server:
SELECT DATEADD(HH, 1, DATEADD(DD, -7, GETDATE()))
SSRS/VS:
DateAdd(DateInterval.Hour, -167, Now())
Update: To Larnu's point, Today() will default as a date format of MM/DD/YYYY. If you only subtract 167 hours from Today, then the field will show in date time, but the result will be incorrect as it will show 1:00 AM for the time. So use the NOW() function, instead of TODAY(). The result will be 167 hours from the current time and will be in the format of MM/DD/YYYY HH:MM:SS.
Also, you can make the change in the parameter field and click on the Preview in VS to see these changes. You would not need to run the report to see this change. That, or I do not understand what you are trying to accomplish.

Set report date for date other than today in SQL reporting

I have a report that I have been asked to produce. The first column of data is total time entered from the first of the year to the end of the previous month. The next column of data is total time from beginning of "current" month to the end of "current" month.
For example. If this report was being run for March then the first column would be total time for Jan and Feb and the second column would be total time for March. If I were to run it in April then the first column would be total time for Jan/Feb/Mar and the second column total time for April etc.
I am using various expressions to get first date of the year, last date of previous month, first date of this month, last date of this month. All working fine and it runs like a dream if you run the report in the current month (i.e. March) but if you want to run the report in April for March's data it won't do it as it's reading the date on the computer and using that to calculate the prev month.
In Crystal reports you can set a report date. Is there something similar in SQL reporting? I'm assuming you DECLARE the report date in your initial query but I haven't yet found the right combination of functions.
This report is for an external bit of software that we run.
The only parameters I can use are #FromDate and #ToDate and these are set as text rather than date
I was planning on using the #ToDate to set the report date but would obviously have to convert it from text first
Any guidance very much appreciated
Try this. You could actually do it without all the declare statements, I just did those to break it down to be easy to read.
declare #date_entered datetime = '2/7/2017'
declare #start_of_year datetime = DATEADD(yy, DATEDIFF(yy, 0, #date_entered), 0)
declare #start_of_month datetime = DATEADD(month, DATEDIFF(month, 0, #date_entered), 0)
declare #end_of_month datetime = DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,#date_entered)+1,0))
declare #end_of_last_month datetime = DATEADD(day,-1,#start_of_month)
-- Now remove weekends from days found
declare #amount_of_days_previous tinyint = datediff(day,#start_of_year,#end_of_last_month) - (datediff(wk, #start_of_year, #end_of_last_month) * 2)
declare #amount_of_days_this tinyint = DATEDIFF(day,#start_of_month,#end_of_month) - (datediff(wk, #start_of_month, #end_of_month) * 2)
select #amount_of_days_previous * 8 as WorkHoursinDaysPrevious,
#amount_of_days_this * 8 as WorkHoursinDaysThis
I had a think over the weekend and realised that I was approaching the problem all wrong. I was trying to set the report date so that my query could read that date and then go back and find the relevant date (first date in the year, last date of previous month etc).
What I needed to do was use my date criteria (FromDate and ToDate) in my DATEADD selections.
CONVERT(datetime,#FromDate,103) AS FIRSTDAYOFYEAR,
DATEADD(D, - 1, DATEADD(MONTH, DATEDIFF(MONTH, '19000101', CONVERT(datetime,#ToDate,103)), '19000101')) AS LASTDAYPREVMONTH,
DATEADD(MONTH,DATEDIFF(MONTH, '19000101',CONVERT(datetime,#ToDate,103)), '19000101') AS FIRSTDAYMONTH,
CONVERT(datetime,#ToDate,103) AS LASTDAYMONTH,
This is now much better because my "first date of year" can be anything the user wants, not just the first day of the year.

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())

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.

Resources