Greater than / Less than date/time table expression - ms-access

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

Related

SSRS default param expression today() vs now()

I have a parameter of type date/time. I am creating a default value as expression. When I put
=IIF(Hour(TODAY()) < 1,
DateAdd("d", -1, FormatDateTime(Today(),DateFormat.ShortDate)),
FormatDateTime(Today(),DateFormat.ShortDate))
it works well and return a value (although always the same, since the hour is always 0 (12AM))
But I want to base this default value on the CURRENT date time attime of report. So when I replace today() with NOW(), it errors out and says that it "doesn't have expected type".
=IIf(Hour(now())<1,
DateAdd("d",-1, FormatDateTime(Today(),DateFormat.ShortDate)),
FormatDateTime(Today(),DateFormat.ShortDate))
Thanks
I tried multiple formulas to convert now() into something, but nothing worked.
It seems as though the expression didn;t evaluate the second part of the IIF because the HOUR(Today) was always 0. The Format would convert the date into a string which would cause the error. Evidently, the DateAdd was converting the string back into a date in order to subtract the day.
I think you can do without all the converting.
=IIF(Hour(NOW()) < 1,
DateAdd("d", -1, Today()),
Today())

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.

How to check if a string contains a date?

I'm trying to iterate on a DataSet, this contain a results of query such as SELECT * FROM tb 1, now the first three field contains a date, the format saved in the database table is this:
yyyy-MM-dd HH:mm:ss
but the code return this:
yyyy/MM/dd HH:mm:ss
in particular this:
For z = 1 To ds.Tables(0).Columns.Count - 1
Console.WriteLine(ds.Tables(0).Rows(x)(z).ToString())
Next
So I need to recognize if the current string have this format: yyyy/MM/dd HH:mm:ss and parse it into: yyyy-MM-dd HH:mm:ss I tough to a regex pattern for recognize it, but I'm not an expert of regex. Anyway, if there is another solution I'll glad to see. Note that only the first three value and the last one of the table is date, the other values aren't date but contain integer or other string value.
Dates do not have a format. From MSDN:
Represents an instant in time, typically expressed as a date and time of day.
...
Time values are measured in 100-nanosecond units called ticks, and a particular date is the number of ticks since 12:00 midnight, January 1, 0001 A.D. (C.E.) in the GregorianCalendar calendar...For example, a ticks value of 31241376000000000L represents the date, Friday, January 01, 0100 12:00:00 midnight.
So, a DateTime is just a Big Number. Representing them as "dd/MM/yyyy" is part of the magic of the DateTime type. Part of the issue is this:
Console.WriteLine(ds.Tables(0).Rows(x)(z).ToString())
Row items are Object. It wont act like a DateTime type unless/until you get it into a DateTime variable. That print as a DateTime simple because the DataTable knows the underlying type; but it will use the default format for your Culture. This makes it look like dates have a built in format (or even that the "format changed" if you tried to set it to something), but you are a human and 635882810022222112L would not make sense to most of us.
To change the output style, you first need to get it into a DateTime variable. Apparently, a preliminary step is to determine if an arbitrary column is a Date. Rather than testing the "format" of the output, test the underlying data type. This does assume a proper DateTime column in the DataTable:
If ds.Tables(0).Columns(n).DataType = GetType(DateTime) Then
'...
End If
' Or:
If ds.Tables(0).Rows(x)(z).GetType Is GetType(DateTime) Then
'...
End If
Then to change the display, first get it into a DateTime variable:
Dim dt As DateTime
If ds.Tables(0).Rows(x)(z).GetType Is GetType(DateTime) Then
dt = Convert.ToDateTime(ds.Tables(0).Rows(x)(z))
' cant change "format" but you can change how it displays:
Console.WriteLine(dt.ToLongDateString)
Console.WriteLine(dt.ToString("yyyy-MM-dd HH:mm tt"))
Console.WriteLine(dt.ToString("dd MMM, yyyy"))
End If
An easier way to get and convert to DateTime is to use the Field(Of T) extension:
Dim dt = ds.Tables(0).Rows(x).Field(Of DateTime)(y)
when I peform the insert usually do this: Date.Now.ToString("yyyy-MM-dd HH:mm:ss") so I apply a format to date to insert... if I don't format correctly the date as I shown I get this value 0000-00-00 00:00:00
That doesn't apply a format to a date. It converts the DateTime to a string. While "yyyy-MM-dd HH:mm:ss" is the correct format to use when passing date data as a string to MySql, it is not needed. The MySQL Data provider knows how to convert a Net DateTime var to the data MySql needs/wants and back again -- that's its job.
' this will work fine
cmd.Parameters.Add("#SomeDate", MySqlDbType.DateTime).Value = myDateTimeVar
The format requirement you read about is the what you need to use in the MySql shell or WorkBench UI because you are entering text/string there...from the keyboard. It does not mean code must convert DateTime variables to string in a specific format for storing.
I ended up using this
Try
Dim theDate As DateTime = dr.Item(colName)
Return theDate
Catch
' do something
End Try
I would be happy to see a better method.
Based off of what you seem to be asking a simple replace would do
For z = 1 To ds.Tables(0).Columns.Count - 1
Console.WriteLine(ds.Tables(0).Rows(x)(z).ToString().Replace("/","-"))
Next
if it comes in with / they are changed to - if it comes in with - they remain intact.
Depending on the flexibility you want in this, it may be necessary to TryParse to ensure that the value you're working with is actually a valid datetime.

Using DateDiff to Calculate the Difference in Days

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)

Reporting Services Remove Time from DateTime in Expression

I'm trying to populate an expression (default value of a parameter) with an explicit time. How do I remove the time from the the "now" function?
Something like this:
=FormatDateTime(Now, DateFormat.ShortDate)
Where "Now" can be replaced by the name of the date/time field that you're trying to convert.)
For instance,
=FormatDateTime(Fields!StartDate.Value, DateFormat.ShortDate)
Since SSRS utilizes VB, you can do the following:
=Today() 'returns date only
If you were to use:
=Now() 'returns date and current timestamp
=CDate(Now).ToString("dd/MM/yyyy")
Although you are hardcoding the date formart to a locale.
If you have to display the field on report header then try this...
RightClick on Textbox > Properties > Category > date > select *Format (Note this will maintain the regional settings).
Since this question has been viewed many times, I'm posting it... Hope it helps.
Just use DateValue(Now) if you want the result to be of DateTime data type.
If expected data format is MM-dd-yyyy then try below,
=CDate(Now).ToString("MM-dd-yyyy")
Similarly you can try this one,
=Format(Today(),"MM-dd-yyyy")
Output: 02-04-2016
Note:
Now() will show you current date and time stamp
Today() will show you Date only not time part.
Also you can set any date format instead of MM-dd-yyyy in my example.
In the format property of any textbox field you can use format strings:
e.g. D/M/Y, D, etc.
One thing that might help others is that you can place: =CDate(Now).ToString("dd/MM/yyyy") in the Format String Property of SSRS which can be obtained by right clicking the column. That is the cleanest way to do it. Then your expression won't be too large and difficult to visually "parse" :)
FormatDateTime(Parameter.StartDate.Value)
I'm coming late in the game but I tried all of the solutions above! couldn't get it to drop the zero's in the parameter and give me a default (it ignored the formatting or appeared blank). I was using SSRS 2005 so was struggling with its clunky / buggy issues.
My workaround was to add a column to the custom [DimDate] table in my database that I was pulling dates from. I added a column that was a string representation in the desired format of the [date] column. I then created 2 new Datasets in SSRS that pulled in the following queries for 2 defaults for my 'To' & 'From' date defaults -
'from'
SELECT Datestring
FROM dbo.dimDate
WHERE [date] = ( SELECT MAX(date)
FROM dbo.dimdate
WHERE date < DATEADD(month, -3, GETDATE()
)
'to'
SELECT Datestring
FROM dbo.dimDate
WHERE [date] = ( SELECT MAX(date)
FROM dbo.dimdate
WHERE date <= GETDATE()
)
My solution for a Date/Time parameter:
=CDate(Today())
The trick is to convert back to a DateTime as recommend Perhentian.
Found the solution from here
This gets the last second of the previous day:
DateAdd("s",-1,DateAdd("d",1,Today())
This returns the last second of the previous week:
=dateadd("d", -Weekday(Now), (DateAdd("s",-1,DateAdd("d",1,Today()))))
This should be done in the dataset. You could do this
Select CAST(CAST(YourDateTime as date) AS Varchar(11)) as DateColumnName
In SSRS Layout, just do this =Fields!DateColumnName.Value
Just concatenate a string to the end of the value:
Fields!<your field>.Value & " " 'test'
and this should work!