I have a column with elapsed time values in it and they are formated as hh:mm:ss or mm:ss. I had no control over this as the data was imported.
What I need to do is find a way to do a global update to change the values to seconds or find a way to use the sum function on the column in its current format.
One problem is that the data is a mix of mm:ss and hh:mm:ss. The hours field was not zero padded.
The statement
SELECT
SUBSTR(SUBSTR("0000000"||time,LENGTH(time),8),1,2)*3600 +
SUBSTR(SUBSTR("0000000"||time,LENGTH(time),8),4,2)*60 +
SUBSTR(SUBSTR("0000000"||time,LENGTH(time),8),7,2)
FROM
table
WHERE
...
should work with mix of hh:mm:ss, h:mm:ss, mm:ss, m:ss, ss and s formats and also the time separator should be any character.
Related
I load a dataset from excel
library(readxl)
df<-read_excel("excel_file.XLSX")
In the file there is a separate date column as Posixct
str(df$datecol)
I also have a time column that in R gets loaded as a date time. To bring it back as time I do........
df$Timecol<-format(df$Timecol,"%H:%M:%S")
However it turns into a character. This is where i think the problem lies
str(STOP_DATA$`Stop Frisk Time`)
I would think this part resolves the situation
df$merge_date_time<-as.POSIXct(paste(df$Datecol, df$TimeCol), format="%Y-%m-%d %H:%M:%S")
The date and time is then combined. What i want to do now is reference a timestamp column that is a Poxict data type.
str(df$Timestamp)
I would like to then find the time difference between them
df$TIME_SINCE <- difftime(df$Timestamp, df$merge_date_time, tz="UTC", units = "mins" )
but I end up with weird numbers that don't make sense. My guess its the Character data type for time. Does anyone know how to solve this?
I ended up finding out that this works
df$date_time<-paste(df$date, format(as.POSIXct(df$time), '%T'))
I removed the portion below from the script as it changed the file into a character.
df$Timecol<-format(df$Timecol,"%H:%M:%S")
I accepted the obscure POSIXT default with the proper time and odd dates (1899-12-31) and what the script did was replace 1899-12-31 with the proper correstponding df$date column.
I've a system generated date and time format. It looks something like this, "2017-04-12-02.29.25.000000" . I want to convert this format into a standard one so that my system can read this and later on I can convert it into minutes. Someone please help to provide a code in R.
If you're unsure of the format, the guess_formats function in lubridate is pretty helpful:
w <- "2017-04-12-02.29.25.000000"
> lubridate::guess_formats(w, orders = 'YmdHMS')
YOmdHMS YmdHMS
"%Y-%Om-%d-%H.%M.%OS" "%Y-%m-%d-%H.%M.%OS"
orders is the format you want the function to investigate and it outputs the correct representation. If the second entry in the string is the day you can try YdmHMS.
The difference in the two formats in the output in the above example is based on formatting of the second entry (always with a leading zero or not). Trying the first format gives:
> as.POSIXct(w, format = "%Y-%Om-%d-%H.%M.%OS")
[1] "2017-04-12 02:29:25 EDT"
In the as.POSIXct call you may specify the timezone tz if required.
I insert my date/time data into a CHAR column in the format: '6/4/2015 2:08:00 PM'.
I want that this should get automatically converted to format:
'2015-06-04 14:08:00' so that it can be used in a query because the format of DATETIME is YYYY-MM-DD hh:mm:ss.fffff.
How to convert it?
Given that you've stored the data in a string format (CHAR or VARCHAR), you have to decide how to make it work as a DATETIME YEAR TO SECOND value. For computational efficiency, and for storage efficiency, it would be better to store the value as a DATETIME YEAR TO SECOND value, converting it on input and (if necessary) reconverting on output. However, if you will frequently display the value without doing computations (including comparisons or sorting) it, then maybe a rococo locale-dependent string notation is OK.
The key function for converting the string to a DATETIME value is TO_DATE. You also need to look at the TO_CHAR function because that documents the format codes that you need to use, and because you'll use that to convert a DATETIME value to your original format.
Assuming the column name is time_string, then you need to use:
TO_DATE(time_string, '%m/%d/%Y %I:%M %x') -- What goes in place of x?
to convert to a DATETIME YEAR TO SECOND — or maybe DATETIME YEAR TO MINUTE — value (which will be further manipulated as if by EXTEND as necessary).
I would personally almost certainly convert the database column to DATETIME YEAR TO SECOND and, when necessary, convert to the string format on output with TO_CHAR. The column name would now be time_value (for sake of concreteness):
TO_CHAR(time_value, '%m/%d/%Y %I:%M %x') -- What goes in place of x?
The manual pages referenced do not immediately lead to a complete specification of the format strings. I think a relevant reference is GL_DATETIME environment variable, but finding that requires more knowledge of the arcana of the Informix product set than is desirable (it is not the first thing that should spring to anyone's mind — not even mine!). If that's correct (it probably is), then one of %p and %r should be used in place of %x in my examples. I have to get Informix (re)configured on my machine to be able to test it.
So I know I can use =FormatDate(MyDate ,"mm/dd/yy") to turn a date into a string. I am then trying to add on a time:
=FormatDate(AdminDate ,"mm/dd/yy") + MyTime
Which works, however, I need to format this back to a datetime field (as I need to compare against a preexisting datetime field). I try using Todate(), but the documentation is very light, and provides very little on what is acceptable in the formatting of the date area, and nothing in the way of time!
I have attempted:
=ToDate(FormatDate(MyDate ,"mm/dd/yy HH:mm:ss") + MyTime ,"mm/dd/yy HH:mm:ss")
but this will only work when there is no time (and it just nulls out the time) and any row with a time will return a #ERROR
Anyone have an insight on formatting datetimes?
Thanks
The correct way to use the FormatDate command to output date and time components together in 12 hour format is:
=FormatDate(AdminDate; "MM/dd/yy hh:mm:ss a")
and in 24 hour format is:
=FormatDate(AdminDate; "MM/dd/yy HH:mm:ss")
Note MM is used in months and mm in minutes.
I'm trying to do a query like this on a table with a DATETIME column.
SELECT * FROM table WHERE the_date =
2011-03-06T15:53:34.890-05:00
I have the following as an string input from an external source:
2011-03-06T15:53:34.890-05:00
I need to perform a query on my database table and extract the row which contains this same date. In my database it gets stored as a DATETIME and looks like the following:
2011-03-06 15:53:34.89
I can probably manipulate the outside input slightly ( like strip off the -5:00 ). But I can't figure out how to do a simple select with the datetime column.
I found the convert function, and style 123 seems to match my needs but I can't get it to work. Here is the link to reference about style 123
http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.help.ase_15.0.blocks/html/blocks/blocks125.htm
I think that convert's slightly wrongly documented in that version of the docs.
Because this format always has century I think you only need use 23. Normally the 100 range for convert adds the century to the year format.
That format only goes down to seconds what's more.
If you want more you'll need to past together 2 x converts. That is, past a ymd part onto a convert(varchar, datetime-column, 14) and compare with your trimmed string. milliseconds comparison is likely to be a problem depending on where you got your big time string though because the Sybase binary stored form has a granularity of 300ms I think, so if your source string is from somewhere else it's not likely to compare. In other words - strip the milliseconds and compare as strings.
So maybe:
SELECT * FROM table WHERE convert(varchar,the_date,23) =
'2011-03-06T15:53:34'
But the convert on the column would prevent the use of an index, if that's a problem.
If you compare as datetimes then the convert is on the rhs - but you have to know what your milliseconds are in the_date. Then an index can be used.