What is the SSRS expression for end day of week? - sql-server

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

Related

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.

DateAdd/DateDiff functions in H2 and SQL Server

I am writing an munit test case and mocking my real SQL Server database tables with H2 in-memory instance.
My query has date condition to return records from previous month, so I was looking for something that works with SQL Server as well as H2. Finally I found below which syntactically works in both.
select
DATEADD(MONTH, DATEDIFF(MONTH, '1900-01-01', GETDATE()) - 1, '1900-01-01');
In SQL Server, it properly gives me start date of previous month but in H2, it is giving me start date of current month. If change -1 to 2 then H2 works but SQL Server returns the wrong date.
Can someone please help with how I can get correct syntax?
Same problem with last day of previous month -
select
DATEADD(D, -1, DATEADD(MONTH, DATEDIFF(MONTH, '1900-01-01', GETDATE()), '1900-01-01'))

How do I use dateadd to get the first day of last year?

I'm trying to pull data that falls in between January 1st, 2014 and today's date last year (ie. August 3rd, 2014). How would I go about using ``dateadd` to get the date 1/1/14? I'm using the following code to get the date 8/3/14.
dateadd (yy, -1, getdate())
I want to avoid explicitly searching for 1/1/14 because in a year's time I'd like the sql query to find 1/1/15 without me having to go back in and rewrite it.
Use DATEFROMPARTS:
DATEFROMPARTS(YEAR(GETDATE()) - 1, 1, 1)
DATEADD(yy, DATEDIFF(yy,0,getdate())-1, 0)
DATEFROMPARTS() is the best way but it requires SQL2012 or later. If you're on an earlier vierion, try this:
You can use GETDATE() to get the current date
You can use the function YEAR() to extract the year from any date
Subtract 1 from it to get last year
Append 1/1/ to the front of it
Convert it back to a date again
select convert(datetime, '1/1/' + convert(varchar(max),year(getdate())-1))

Excel incorrectly converts Date into Int

I'm pulling the data from SQL database. I have a couple columns with date which need to be converted into Int type, but when I do this the date changes (-2 days). I tried Cast and Convert and it's always the same.
Converting to other type works fine and returns the correct date, but doesn't work for me. I need only the date part from datetime and it needs to be recognised as a date by Excel.
Why is this happening? Any ideas how to get it sorted?
I'm using the following query:
SELECT wotype3, CONVERT(INT,wo_date2 ,103), CAST(duedate AS int) FROM Tasks WHERE
duedate > DATEADD(DAY,1, GETDATE())
AND wo_date2>0
AND wo_date2<DATEADD(WEEK,3,GETDATE())
ORDER BY wotype3
I've had big problems with this, checking my SQL Server's calculation results with "expected results" which a user had created using Excel.
We had discrepancies just because of this 2-day date difference.
Why does it happen ?
Two reasons:
SQL Server uses a zero-based date count from Jan 1 1900, but Excel uses a 1-based date count from Jan 1 1900.
Excel has a bug in it (gasp!) which makes it think that the year 1900 was a leap year. It wasn't. SQL Server correctly refuses to let you have a date value containing "29-Feb-1900".
Combine these two discrepancies, and this is why all dates, from March 1 1900 onwards, are always 2-days out.
Apparently, this Excel bug is a known issue, to keep it in line with Lotus 1-2-3.
The Intentional Date Bug
Microsoft's own explanation
From now on, I think I'll justify bugs in my code with the same excuse.
;-)
For SQL Server 2008 and above, you can use the DATE datatype.
declare #dt datetime = '12/24/2013 10:45 PM' -- some date for example
SELECT #dt as OriginalDateTime, CAST(#dt as DATE) as OnlyDate
For versions prior to SQL Server 2008, you would need to truncate the time part using one or the other functions. Here is one way to do that:
declare #dt datetime = '12/24/2013 10:45 PM' -- some date for example
SELECT #dt as OriginalDateTime, CAST(FLOOR(CAST(#dt AS FLOAT)) as DATETIME) as OnlyDate

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