Using DateDiff to Calculate the Difference in Days - reporting-services

In the expression below, I am trying to calculate the difference in days between the Created date and today's date. If it is less than 30 days then output "1", otherwise output "0"
=IIF(DateDiff("d",(Format(CDate(Fields!Created.Value), "MM/dd/yyyy")), (Format(CDate(Today()), "MM/dd/yyyy")))<30, "1", "0")
Both values in "Created" and Today() are formatted with the date and time so I use Format and CDate to extract just the date. When I run the report, it displays all "0" and I know that is incorrect. Is there something wrong with the expression?

Yes, there are a number of things wrong with that expression: You take dates, explicitly convert them to dates, then using format convert them to strings then implicitly convert them back to dates to do your date comparison. That's a lot of heavy lifting for no benefit. You are also using SQL syntax in a VBA expression. Your result is also a string when it probably should be an integer.
Your expression should look more like this:
=IIF(DateDiff(DateInterval.Day, Fields!Created.Value, Today) < 30, 1, 0)

Related

Convert date format while using date_add function in SQL

I have a variable (order_date_key) that contains the number of days since 1/1/1900. For example, 42711 represents December 9, 2016 or 42711 days since 1/1/1900. I want to convert that variable to the week of the year (e.g., 2016-50)
I was able to convert the variable to yyyy-mm-dd format using the date_add function, but when I try to use the DATE_FORMAT function with it, it returns an incorrect week number. For example, 2016-12-09 converted to 2016-5, but that date isn't the 5th week of the year.
Here's the code I'm using. The format also doesn't seem to work when I use the '%' symbol.
SELECT order_date_key,
DATE_ADD('1900-01-01', order_date_key) AS Year_month_day,
DATE_FORMAT(DATE_ADD('1900-01-01', order_date_key), 'Y-u') as year_week
Sample data: 42711, 42714, 42715, 42720
Desired output: 2016-50, 2016-51, 2016-51, 2016-52

Conditional formatting for current day date

I am just trying to getting my data to do a color fill if the date value equals today.
The data is coming from oracle:
=IIf(Fields!finishDATE.Value = Today(),"Yellow","Transparent")
This will not give me any errors nor will it do the function according to the expression. None of the data with the finish date equaling today highlights.
If today is 8/24/2021 it should look like this:
3/22/2021, 8/24/2021, 2/22/2021
As I'm not sure what format the data will come in from Oracle (I'm a MS SQL person) then this might be overkill but try this
=IIF (Format(Fields!finishDATE.Value, "yyyyMMdd") = Format(Today(), "yyyyMMdd"), "Yellow", Nothing)
All I'm doing here is comparing just the date parts of the date/datetime values.
Below is the output. The first column is the actual date column contents including a time, then for illustration only, the 2nd column shows it formatted to just the date part and the 3rd column show today() with the same format applied.
Finally, I used the keyword Nothing (SSRS almost equivalent of NULL) as this is the correct default value.

larger value of two different columns where there are nulls

I'm comparing two date columns about bugs, one is resolved bugs and one is closed bugs. I want the larger of the two, but there are null values (when a bug hasnt been resolved or closed yet). How do i take the greater of the two while ignoring nulls values? I saw other solutions, but you have to specify the dates in the code, which i cant do in my data set as its large. the date format is mm/dd/yy hh:mm:ss PM/AM
(GREATEST(dtResolved , dtClosed))
How about this:
GREATEST(COALESCE(dtResolved , dtClosed), COALESCE(dtClosed, dtResolved))
Using this logic, if both dates be not NULL, then you would get the greater of the two. If one be NULL, then you would get non NULL date.
Edit:
the date format is mm/dd/yy hh:mm:ss PM/AM
This sounds like you are storing your dates as text, always a bad idea. To make the above suggestion work, you'll have to convert your text to dates first:
STR_TO_DATE('02/28/2014 09:30:05 AM', '%m/%d/%Y %r')
Here's another example:
GREATEST(COALESCE(UNIX_TIMESTAMP(STR_TO_DATE(dtResolved, '%m/%d/%Y %r')), 0), COALESCE(UNIX_TIMESTAMP(STR_TO_DATE(dtClosed, '%m/%d/%Y %r')), 0))
This code will give you the bigger date in timestamp. The reason is that the GREATEST function will return your date as a string without any space or slashes between year, month or date. So timestamp is a good way to prevent that.
Just in case dtResolved and dtClosed are null and you would compare them, use COALESCE with 0 instead of both value. It will prevent your query from crashes. It will return 0.
Like Tim told you, converting your string date to date object isn't a bad idea. It is always preferable to work with date object for the multiple function that exist.

Greater than / Less than date/time table expression

I have the following expression in a MS-access table:
IIf([End Date/Time]>="12/8/2016 6:00:00",1,0)
12/08/2016 18:15:00 will return a '1', however
12/08/2016 14:23:29 returns a '0'
I'm assuming this is an issue with AM/PM. I tried putting '6:00:00 AM' in my expression but no change.
Also I would like to replace '12/8/2016' with 'yesterday' but date()-1 doesn't seem to be working.
EDIT: I figured out that the time needs to be '06:00:00'. That yield the correct dates. Still don't know how to get this automatically (ie yesterday at 06:00)
Thanks
Your issue is that you threat dates as strings. Use date always, no exceptions.
Further, if your field is not a date value, you must convert it.
Thus, this will work:
IIf(DateValue([End Date/Time]) >= #2016/12/8 6:00:00#, 1, 0)
and this:
IIf(DateValue([End Date/Time]) >= Date() - 1, 1, 0)
and this:
IIf(DateValue([End Date/Time]) >= DateAdd("d", -1, #2016/12/8 6:00:00#), 1, 0)
2 things. First, I believe you need to convert your string to a datetime. I think think you're getting wonky results because it's trying to compare them as a different format. Like a strings or numbers. To format as a date time you use a format fucntion:
Format("12/8/2016 6:00:00AM", "mm/dd/yyyy hh:nn:ss am/pm")
Second, to add a date you need the DateAdd function.
DATEADD('d',-1,"12/8/2016 6:00:00AM")
'd' defines the -1 as a 'day' being added.
So, putting it all together:
DATEADD("d",-1,Format("12/8/2016 6:00:00AM", "mm/dd/yyyy hh:nn:ss am/pm"))
And finally, if you want the want the rolling yesterday, and not perpetually 12/7/2016 (since you would just use that date if it was the case), you need to get today's date with this function:
Date()
So, throwing this into our mix we get:
DATEADD("d",-1,Format(DATE() & " 6:00:00AM", "mm/dd/yyyy hh:nn:ss am/pm"))

Convert Date to a number in mysql (Like how a date converts in Excel)

What is the correct function to use when i want to convert a date to a number. Like when you enter a date in excel for example (now()) and then it will show the date but when you click on the number format it will show you a number.
I tried to use Unix timestamp but its not exactly the output i was looking for.
The date i entered is today's date
=now()
and the output i was hoping to get is
42146
what's the correct function to get this result in mysql?
Thank you
Microsoft Excel bases date serial numbers from Jan 1, 1904 or from Jan 1, 1900 (depends on the setting in the workbook.)
To generate a date "serial number" similar to what Excel uses, just calculate the number of days between NOW() (or whatever date you want to convert), and the base date. For example:
SELECT DATEDIFF(NOW(),'1900-01-01') AS excel_serial_date_1900
(You might need to add 1 or subtract 1 to get the exact same value that Excel uses, I've not tested that.) If your Excel workbook is using the 1904 based serial date, use '1904-01-01' as the base date in the expression.
Note: the DATEDIFF function returns integer number of days, operates only on the "date" portion, and doesn't include any fraction of a day.
Reference: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_datediff