If I have a field of strings in HH:MM format, how do I convert that field into a field that is in time format? The only thing I could find was the DateValue format and examples of how to output the same datetime for every row, and the CAST function, which I could not get to work.
In case that is not clear enough, the data in this field looks like:
16:32
05:15
19:20
All in string format. I want this into time format.
Related
How to import a csv dataset into SAS properly. The main problem is the records under the variable date_time is in the format of dd/mm/yyyy hh:mm and mm/dd/yyyy hh:mm:ss? How to use SAS informat to corrrect this problem?
Read the CSV file with your own data step so that you can preserve the text of the field by reading it as a character variable.
Then to properly convert ambiguous string like 10/12/2021 you will need some other way to learn whether that observation should use date style of MDY or DMY. Perhaps you know what country the data is from? Or you have some other date value on the record that might help. For example if you know the dates for this record should be after Nov 2021 then you can deduce that 10/12/2021 means the tenth of December.
Once you know which style to use for a particular observation use the appropriate INFORMAT to read the date. So if you read the text into DATESTRING and you calculated a variable named DATESTYLE that tells which type of date style to use for this observations then add this snippet of code to the data step to convert the string into a datetime value.
if datestyle='MDY' then do;
datetime=dhms(input(scan(datestring,1,' '),mmddyy10.),0,0
.input(scan(datestring,2,' '),time8.))
;
end;
else do;
datetime=dhms(input(scan(datestring,1,' '),ddmmyy10.),0,0
.input(scan(datestring,2,' '),time8.))
;
end;
I am receiving a Datetime field in my JSON which looks like the below screenshot
where first 4 characters are year, next two characters are month, next two characters are date, next two characters are hour, next two characters are minutes and last two characters are seconds.
I extracted the date successfully and tried to convert using to_timestamp function in SNOWFLAKE but returns me the result like
"2606-08-31 03:17:04.416"
code that I am working with to convert into Timestamp
select distinct to_timestamp(a.value) within group(order by to_timestamp(a.value)) from orders a
Could someone please help me with this?
The default format for conversion is AUTO, which will attempt to guess. It appears as though it is guessing incorrectly.
To specify a format, add a second parameter to your call to to_timestamp that is a string of the format. Here are the docs.
to_timestamp(a.value, 'YYYYMMDDHH24MISS')
Problem
I need to query out a date value for use in some ETL processing. However, the data is stored in MySQL as a VARCHAR column in values like 1/1/19.
What I've tried
A simple CAST(myDateColumn as DATE) fails as I get values like 0001-01-19 returned back.
Question
Am I able to pass a custom date format string into the CAST call somehow to tell it how to parse out the date parts? If not, could a SUBSTRING type of function help here?
As discussed in the comments above, you can use the STR_TO_DATE() function to parse a string into a proper YYYY-MM-DD date.
I'm trying to insert a row in a Cassandra table using the query below
INSERT INTO battery_time_series_by_producer JSON '{"timestamp":1514413581,"customer":"com.fetchcore-cloud.local","stream":"/sw8/sensor/battery","producer":"freight18","data":{"battery_level":1,"is_charging":true,"timestamp":1514413581}}';
and I get the following error
Expected a long or a datestring representation of a timestamp value, but got an Integer: 1514413581
The field timestamp in the table is indeed of the timestamp format. However, how can I convert this to a long given that this is just JSON
My version of Cassandra is 3.11.1
Cassandra will try to parse json object to closest data type possible, e.g. the value 1514413581 is converted to Integer type. If you are giving a larger number, it will be parsed as Long.
Kindly note that that timestamp type is representing a number of milliseconds since the standard base time known as the epoch. The values in your example represent the time in year 1970.
In short, it's either to use string format (e.g. "1514413581") or pass recent timestamp value (which automatically convert to Long later) in your cql statement. Hope it helps.
How do I import and preserve date format fields such as "8/21/2012" from Excel to MySQL?
I am using MySQL Workbench and the Excel MySQL Excel data transfer plug in. When I select the Excel data I want to import into MySQL, I get a window where I declare variable types for all fields. All fields and declarations work as expected except for date and time fields. Both date and time switch from 8/21/2012 to a number like 398475 etc. How can I import these fields into MySQL by preserving the dashed mm/dd/yyyy format? I assume the same procedure will work for time as well.
Alternatively, is there a way to convert the serialized datetime value (a float, representing the number of days since 1/1/1900) back to mm/dd/yyyy in MySQL ?
Thank you!
You can convert your date cells into a MySQL supported format using the TEXT function.
=TEXT(A1,"YYYY-MM-DD")
This will convert a date in cell A1 to the yyyy-mm-dd format that the MySQL date field expects.
To get the serialized date format (say 36422) into a useful date format in MYSQL use the interval function added to the base date that Excel uses (which is actually 1900-00-00 but since that doesn't exist you will have to use 1900-01-01 which is why we subtract 2 from the date column)
`'1900-01-01' + INTERVAL(Your_Date_Column - 2)DAY`
You can convert your datetime cells into a MySQL supported format using the TEXT function
=TEXT(A1,"YYYY-MM-DD HH:MM:SS")
Simply use Date (java.util package) for this.
Date d=Date date=cell.getDateCellValue();
Now Use it as you want.