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

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.

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

Use a report parameter instead of GETDATE in stored procedure

I have a SQL query which is saved as a stored procedure. I am using three different stored procedures to go back 1, 2, or 3 days depending on today's date. This makes it possible for my dashboard users to go back and skip weekends when comparing stats. This actually works. It looks at todays date and if it is Sunday go back 2 days, if it is Monday go back 3 days otherwise go back 1 day. The dashboard is created in Report Builder. Below is shown the between dates if today is Sunday (i.e. skip 2 days and go back to Friday which is the last normal working day). So much for the background. My challenge is now the user of the dashboard would like a BeginDate and EndDate date picker parameter in the report so I can no longer use these hard coded date strings but will have to incorporate a parameter into them. How do I replace a parameter say called #BeginDate in the following line where it says "GETDATE()":
BETWEEN DATEADD(DAY, -2, DATEADD(DAY, DATEDIFF(DAY,0,GETDATE()), 0))
AND DATEADD(SECOND, -86401, DATEADD(DAY, DATEDIFF(DAY,0,GETDATE()),0))
And based upon the date the end user selects then go and use one of the three stored procedures. I think I can make this work somehow, but I cannot figure out how to replace GETDATE() above with a parameter entered in by the user. If I just replace it with a parameter it throws back an error. I'm sure there is a really smart way of doing this. I'm still looking!!!
If you are passing a new parameter into the stored prcoedure:
BETWEEN DATEADD(DAY, -2, #BEGINDATE) AND DATEADD(SECOND, -86401, #BEGINDATE)
Adding seconds seems unnecessarily complicated:
>= DATEADD(DAY, -2, #BEGINDATE) and XXX < DATEADD(DAY, -1, #BEGINDATE)

What is the SSRS expression for end day of week?

I have the following MSSQL query to return the ending day of the week in MSSQL:
SELECT DateAdd(Day, 0 - DatePart(Weekday, GetDate()), GetDate());
I played around with the =DateAdd function, but it keeps throwing me an error for the Day parameter. Also, when I used DateInterval.Day... I get the same error.
However, when I try placing that query into an SSRS expression, it throws me an error. Does anyone know the direct conversion for that query above in SSRS?
SSRS Uses a dialect of Visual Basic, its Date functions are different from TSQL, you have to use
"d" instead of DAY for day interval
"w" instead of WEEKDAY for weekday
Now() instead of GetDate() for current date.
Try
=DateAdd("d", 0 - DatePart("w", Now()), Now())

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.

Getting today's midnight time as UTC

I have the following query which calculates today's midnight value (UTC) as a datetime:
SELECT CONVERT(DATE,GETDATE())+(GETDATE()-GETUTCDATE())
Result: 2011-11-03 19:00:00.000 (for GMT-5 on Nov. 4, 2011)
Not only that, but on occasion, it returns values like these:
2011-11-03 19:00:00.003
2011-11-03 19:00:00.007
2011-11-03 19:00:00.010
..., which are wrong!
There must be a better way to do this.
I already answered this with a solution using DATEADD and DATEDIFF with GETDATE() and GETUTCDATE(), similar to the example given in the original question, but since then I've discovered the datetimeoffset data type added in SQL Server 2008. This stores a datetime along with a timezone offset.
How you use this type will depend on whether you want to change the data type of your existing data. If you don't want to change anything, the following statement will return a datetime type with the local time of midnight:
SELECT CONVERT(datetime, SWITCHOFFSET(CONVERT(datetimeoffset,
CONVERT(date, GETDATE())),
DATENAME(TzOffset, SYSDATETIMEOFFSET())))
You could also convert any UTC time into local time using:
SELECT CONVERT(datetime, SWITCHOFFSET(CONVERT(datetimeoffset,
#myutctime,
DATENAME(TzOffset, SYSDATETIMEOFFSET())))
The datetimeoffset type is only available using SQL2008 and above. If you need to do this with 2005 and below, you can use a solution similar to the one in the original question, but altered to account for the fact that GETDATE() - GETUTCDATE() is not an atomic operation and will likely involve milliseconds of difference between when the two are executed.
SELECT DATEADD(minute,
DATEDIFF(minute, GETUTCDATE(), GETDATE()),
CONVERT(datetime, CONVERT(date, GETDATE())))
This will take the number minutes between GETDATE() and GETUTCDATE() and add them onto the local midnight time. Unfortunately, you have to convert back from date to datetime as DATEADD won't work with minutes if you give it a date. I'd suggest wrapping this into a user-defined function to make it look less verbose, e.g.
CREATE FUNCTION dbo.MidnightASUTC(#dt as datetime)
RETURNS datetime
AS
BEGIN
RETURN DATEADD(minute,
DATEDIFF(minute, GETUTCDATE(), GETDATE()),
CONVERT(datetime, CONVERT(date, #dt)))
END
SELECT dbo.MidnightAsUTC(GETDATE())
For a specific scenario like the one you've described ("today's midnight value (UTC) as a datetime"), a programmatic approach makes sense, but if you ever need to extend it to a different question (what was midnight UTC for this summer?), you may want to use a calendar table (to account for things like daylight savings time, etc).

Resources