Invalid Time String Error when trying to change type of data from string to time - sql

I am very new to data analytics and I need some help troubleshooting a SQL error I got. So, I have a column in this table which transferred over from Excel to SQL as a string type rather than a time piece of data. I want to make it into a time type so i can further analyze it.
So, I did the attached query to try and change the type of data using the CAST function. . However, it could not complete the query thanks to an outlier in the data set I have yet to clean the data and this was one of my first steps to so, but how do I remove this particular row that contains the invalid time string so the query can actually work? Or is there a better way to convert this entire column from text string to time?

BigQuery Time types adjust values outside the 24 hour boundary - 00:00:00 to 24:00:00; for example, if you subtract an hour from 00:30:00, the returned value is 23:30:00.
Based on your screenshot it looks like you are storing a duration? So 330 hours, 25 minutes and 55 seconds?
You would probably be best using timestamp, converting the hours to days and adding the remainder to your minutes and seconds.
You can then cast the resulting string to timestamp.
Edit
A much simpler solution is just cast('330:25:55' as interval) - thanks to #MatBailie

Related

Need help converting Integer to Time

I looked through online solutions for this, none worked for me so I'm posting here. I have separate date and time columns, both stored as integers. I am able to convert the date column to DATE, but not for the Time column. The column value is: 52700, when using TO_TIME(TO_CHAR(OHCRTM)) I get 14:38:20 but it should be 07:27:00. I've tried various formatting (TO_TIME(TO_CHAR(OHCRTM),'HH24.MI.SS') but I get a 'cannot parse' error. Any idea how I can get the correct time?
14:38:20 is the right answer, unless you can give us the logic that would make it 07:27:00.
52700 seconds are exactly 14 hours, 38 minutes, and 20 seconds.
Another option to read 52700 would be a time without the colons, ie 05:27:00. To parse it like that the needed SQL is:
select to_time(52700::string, 'HHMISS');
I guess 05 becomes 07 after a timezone conversion then?

SSRS/SQL How to plot HH:mm:ss on graph?

Hi all this is my first time posting on Stack Overflow.
I'm trying to plot a chart that displays names of some schedules we run, and their total run time duration. What I'm trying to achieve is to show our 5 slowest schedules. SQL data output is something like this:
SQL Output
Note for elapsed above, this is currently cast as TIME, however I have also had it in the format of 03:19:02 without the milliseconds afterwards as well by using CONVERT VARCHAR 108.
My graph
In the screenshot above you can see the schedule numbers on the left and the elapsed time on the bottom.
Things I have tried so far:
Setting the format under number to HH:mm or HH:mm:ss. Neither of these work, when I do this it results in this:
My attempt at custom Number formats
How it looks after I save the number format
As you can see it just plots a ridiculous HH:mm continuously on the bottom axis as opposed to an actual time. This happens whether my data is plotted as convert varchar 108 or CAST as TIME.
I've also attempted to go under Axis Options and changed Interval type to Hours or days or minutes etc, if I try any of those the axis just disappears as below:
Interval Type Changed to Hours
Interval Type Changed to Hours output with disappearing axis
Let me know what I'm doing wrong in the above team! Thank you!
It looks like you are not showing the actual duration but a count (which is always 1).
Look under Chart Data / Values and make sure your aggregate is e.g. a MAX and not a COUNT.

Create Excel DateTime Serial/Decimal Fraction values in SQL

I am trying to recreate a staff members Excel work in SQL to save time and also drive reporting.
In their spreadsheet, they take 2 time values, minus the smallest from the largest to arrive at a difference, convert that time value to a serialised time value:
They then sum that serial integer to define performance calculations.
Is there a conversion or similar process in SQL that can return the same/similar serial time value so I can perform equivalent calculations (or has anyone experience with a function that achieves this)?
I have tried the following line in the code (based on the Excel DateTime explanation here) and the value isn't the same result as Excel...
datediff(MINUTE,cf_pick_pack.date_start, cf_pick_pack.date_end) * (convert(float,1.00000000/1440)) as 'duration_serial'
SQL returns 0.00902777^, which is short of the 0.00923611 that Excel returns.
Ok, my bad. SQL was calculating the difference to the nearest minute because the datediff was set to minutes.
The following works...
datediff(SECOND,cf_pick_pack.date_start, cf_pick_pack.date_end) * (convert(float,1.00000000/86400)) as 'duration_serial'

Total Hours Format in Moment.js

I'm creating a data table using DataTables.net where a column contains the cumulative running hours of an event. I'm simply adding to the hours each time, so I have for example:
40:34:30
which is 40 hours, 34 minutes, 30 seconds.
My problem is I want to order this column by hours, and I haven't been able to find anything that supports this from Moment.js. Ideally I imagine it would be something like "HHH:mm:ss", or something like that. As it stands, the column recognises the fields as strings, so 0:12:34 is appearing above anything else in descending order despite only being 12 minutes long.
You can sort HH:mm:ss by re-formating it to seconds before sorting.
moment.duration('40:34:30').asSeconds;
gives you 146070. Then simply use seconds in your sorting script.
here is the solution: jsFiddle
And if you really need just the hour part; use Math.floor: jsFiddle

Store datetime -time only in Access database

I have a vb.net program, updating the time value in an access database. The database is connected using OleDB.
Basically this is what is happening:
Dim commandBuilder As New OleDb.OleDbCommandBuilder(dataEventAdapter)
eventDataset.Tables("EventList").Rows(selectedEvent)("EventTime") = Format(dateTimePick.Value, "hh:mm tt")
dataEventAdapter.Update(eventDataset, "EventList")
The time is taken from a datetime picker, and it should store only the time value.
The problem is, that the database already has values in it, which only has the time, like: 9:00 AM, but when I'm updating with this, it gets the date as well. And honestly I don't know where it gets the date from. If I
MsgBox(Format(dateTimePick.Value, "hh:mm tt"))
I get only the time, and nothing else.
How can I store the time only?
If you look at the datatypes available in MS-Access you will find that there isn't a type just for Time values but there is a type for Date/Time values. This means that Access will store always the date AND the time for the values that you supply. The display that you observe looking at the MS-Access grid is controlled by the Format setting in the structure page of your table and here you could change it to show just the Time part of your data.
Said that, there is the problem that you don't supply a DateTime value, but a string. Access is gracious(?) enough to not trigger an exception for this, but compensates adding a date by itself thus you should see the current day for every value that you supply.
So you shouldn't be concerned about how your value has been displayed, but more on how you pass that value to the database. If only the time part is meaningful for your program then leaving the database engine convert back your string to a datetime value is not an option. (Without talking about the localization issues that this automation will involve)
I suggest to pass a constant value for the Date part (like DateTime.MinValue or 1/1/1) and add your time to this value. In this way you could easily ignore the date part if you eventually need to use some queries on this data.
Dim dt As DateTime = new DateTime(1,1,1, dateTimePick.Value.Hour, _
dateTimePick.Value.Minute,
dateTimePick.Value.Second)
eventDataset.Tables("EventList").Rows(selectedEvent)("EventTime") = dt
You can make a simple experiment in Access. Open the Immediate window with Ctrl-G and enter
?Format(#00:00:00#,"yyyy/mm/dd hh:nn:ss")Enter
1899/12/30 00:00:00
?Format(#08:31:57#,"yyyy/mm/dd hh:nn:ss")Enter
1899/12/30 08:31:57
The result shows you the origin Access uses for its time axis.
Another experiment shows this:
?#1899/12/30 08:31:57#Enter
08:31:57
Access automatically displays only the time part for the date 1899/12/30.
Therefore I suggest to use this date as a base for time-only data.
Access uses Double values to store dates internally, where the integer part represents the number of days elapsed since 1899/12/30 and the decimal fraction represents the time as fraction of 24h (i.e. 0.25 is 06:00 am and 0.75 is 18:00).
?CDbl(#1899/12/30 08:00:00#)Enter
0.333333333333333
?CDbl(#1899/12/30#)Enter
0
?CDate(0)Enter
00:00:00
?CDate(0.25)Enter
06:00:00
In .NET you can use the System.DateTime.FromOADate(d As Double) As Date method for the conversion of Access Dates given as Double to .NET Dates (VB Date = System.DateTime).
You are confusing data types with formatting. In your database the column you are inserting into has a datetime datatype (Access has no data type for just time). This means that it stores everything that goes in there as a date + a time.
If in Access you are seeing values with only a time, it's likely that Access decided the date is useless (possibly because it was stored with a date of 1/1/1900).
Thing to remember is that the date still being stored. When you re-display the data just format it to only display the time. Judging from your code example you already know how to do that.