larger value of two different columns where there are nulls - mysql

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.

Related

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.

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

How to separate time from datetimepicker vb.net

I have one datetimepicker which custom format MM/dd/yyyy h:mm tt
I have database and has a column "Date_Time" the value of the DateTimePicker is saved to the column Date_Time formatted like this MM/dd/yyyy h:mm tt
now i want to get the Time only not the entire value of datetimepicker just the hh:mm tt from the column Date_Time
SORRY FOR MY GRAMMAR
How about DateTime.TimeOfDay?
It returns the time that has elapsed since midnight (which is what h:mm tt stands for in your code).
Dim Time As TimeSpan = DateTimePicker1.Value.TimeOfDay 'Would return for example 3:14 PM
The answer above is right.
If you need to get time string, you can use also another way, which includes a formating:
Dim myTimeString = DateTimePicker1.value.ToString("hh:mm")
You can do that for any part of the DateTime value.
You are heading for a new problem. If you zero out the Date portion and store the result to a DateTime column, you will end up storing something like: 0001-01-01 16:43:12. A column defined as DateTime will always have a Date, as will a DateTime variable.
The first problem may be getting MySQL to accept a non-Date in a DateTime column. Using a column defined as DateTime(3), mine throws a generic fatal error exception trying to store just a TimeSpan to it:
cmd.Parameters.Add("#p3", MySqlDbType.DateTime).Value = DateTime.Now.TimeOfDay
cmd.ExecuteNonQuery()
If MySqlDbType.Time is used as the type, I get an exception that the time is an invalid value for the column...and it is.
If you manage to store it somehow, the next problem will be when/if you want to put that value back in a DateTimePicker: the minimum date you can enter is 1/1/1753 (first full year of the current calendar). So your DateTime var with the Date zeroed out wont work. You'll first have to restore the date portion, but the Date, Year etc are all readonly.
Solution 1
Define the column as Time(0) which will store hours, minutes and seconds. Use the value in the parens to specify fractional seconds, for instance Time(3) will also store milliseconds. When you read the data, store it to a TimeSpan.
Then in your UI use a different control, otherwise you have the same problem - adding some Date data to it to make it usable in a DateTimePicker
Solution 2
Use a DateTimePicker and a DateTime column, but just ignore the Date portion in your code. This will allow you to use what is in the Database as is with the control.
You can get the time selected with DateTime.TimeOfDay but storing and reusing it may be problematic.

How can I store the date to the table without a time stamp?

I am in a form, using Short Date format (I have tried General, Long, Medium) - each stores a time stamp along with the date in the table. like x/xx/xxxx hr:min:ss am/pm.
This should be so simple.
As noted in the comments to your question, values in Access Date/Time columns always have both a date and a time component. However, in most cases Access will display only the date part if the time is exactly midnight, so if you force the time to midnight when storing the value then it will look like a date-only value.
By changing the Date value to Integer you can hide the time portion of the Date and Time.
Example:
dValue = Now() shows Date and Time
dValue = Int(Now()) shows only the Date

SSRS: Summing TimeSpan values in a report

I have a report and a datasource where one of the columns are of type TimeSpan. The TimeSpan value appears to display correctly in the report when I use Fields!TheTime.Value, no problem there.
07:02:00
05:41:00
But I would like to do a Sum on those values to get the total time of a group. In C# and such I can of course do a TimeSpan + another TimeSpan, so I know they can be added. I tried
=Sum(Fields!TheTime.Value)
But it ends up printing out as a long number of some sort. For example for the outputted times above, I would get 457800000000 as the sum. And what is that even supposed to be?
Anyways, how can I sum timespan values in a report? For the above timespans I would like to end up with 12:43:00 as the sum. Unless my head failed me at math once again... but you get the idea :p
sigh The solution annoyingly simple... Why couldn't I just have tried that in the first place? Oh well... maybe because I didn't realise I had access to TimeSpan class... maybe because I had thought myself blind... But anyways, here it is:
=TimeSpan.FromTicks(Sum(Fields!TheTime.Value))
D'oh!
#Svish - I deleted my previous post because I had a fit uncertainty about my answer but I concur with #pfunk.
I finally got SSRS back up and had a play around and it certainly looks like your big number is the number of ticks so it looks like a bit of formatting of the result will work for you.
Interestingly enough my previous convoluted answer was a workaround for summing DateTime values (using SQL Server DATETIME datatype in my query) which you cannot do in SSRS (and SQL) because you cant sum a DATETIME. I'll include it here again for future reference but I think was on a bit of a tangent earlier :)
The below code converts a DateTime field into a double, sums the result and then converts it back to DateTime and formats it for hh:mm:ss
=Date.FromOADate(Sum(Fields!TheTime.Value.ToOADate())).ToString("hh:mm:ss")
What is probably happening is that when you display Fields!TheTime.Value, SSRS is smart enough to know to display that as a DateTime type field
when you add the sum in there it thinks it is a numeric type field and displays it as such (ie, it is summing the number of "ticks" in each timespan field)
try specifically formatting the summed value as a datetime in the field properties and it will probably show correctly