I need convert string to datetime (date and time together).
I try this:
cast(to_date(from_unixtime(unix_timestamp('20190303164305', 'yyyyMMddHHmmss'))) as date) as date_data_chamada
timezone: Brazil
But this way returns just date, like this: 2019-03-03, and I need: 2019-03-03 16:43:05
Thanks!
Full code:
INSERT INTO p_b.este PARTITION (dt_originacao_fcdr)
SELECT
tp_registro_fcdr,
seq_registro_fcdr,
tp_cdr_fcdr,
dt_atendimento_fcdr,
data_atendimento_completa_fcdr,
cast(from_unixtime(unix_timestamp(data_atendimento_completa_fcdr, 'yyyyMMddHHmmss'),"yyyy-MM-dd HH:mm:ss")as timestamp) as date_data_atendimento_fcdr,
hr_atendimento_fcdr,
duracao_atend_fcdr,
hr_originacao_fcdr,
duracao_total_fcdr,
duracao_chamada_tarifada_fcdr,
st_chamada_fcdr,
fim_sel_orig_fcdr
FROM p_b.norm;
Remove date casting and to_date functions as you are expecting timestamp!
Example:
hive> select from_unixtime(unix_timestamp('20190303164305', 'yyyyMMddHHmmss'),"yyyy-MM-dd HH:mm:ss") as date_data_chamada;
RESULT:
2019-03-03 16:43:05
If you use to_date or cast('string' as date) then hive results only date(yyyy-MM-dd)!
Ex:
hive> select to_date(from_unixtime(unix_timestamp('20190303164305', 'yyyyMMddHHmmss'),"yyyy-MM-dd HH:mm:ss")) as date_data_chamada;
--2019-03-03
Pass the second argument format string to from_unixtime. Note that the returned type is string.
from_unixtime(unix_timestamp('20190303164305','yyyyMMddHHmmss'),'yyyy-MM-dd HH:mm:ss')
Related
Attempting to cast a string (formatted as YYYY-MM-DD) as ISO date. It's already in the right format but it is erroring out.
SELECT
*
FROM TABLE1
WHERE CHAR(VARCHAR((SUBSTRING(PARAMETER_VALUE,8,4)||
CASE SUBSTRING(PARAMETER_VALUE,4,3)
WHEN 'Jan' THEN '-01-'
WHEN 'Feb' THEN '-02-'
WHEN 'Mar' THEN '-03-'
WHEN 'Apr' THEN '-04-'
WHEN 'May' THEN '-05-'
WHEN 'Jun' THEN '-06-'
WHEN 'Jul' THEN '-07-'
WHEN 'Aug' THEN '-08-'
WHEN 'Sep' THEN '-09-'
WHEN 'Oct' THEN '-10-'
WHEN 'Nov' THEN '-11-'
WHEN 'Dec' THEN '-12-' END||
SUBSTRING(PARAMETER_VALUE,1,2))
),ISO) > CURRENT DATE;
I receive the following error: The statement was not processed because the data type, length or value of the argument for the parameter in position "1" of routine "SYSIBM.CHAR" is incorrect. Parameter name: "".. SQLCODE=-171, SQLSTATE=42815, DRIVER=4.19.56
I am using IBM Data Studio to run this.
PARAMETER_VALUE looks like it's DDMonYYYY if you wanted to use TO_DATE similar to:
SELECT
*
FROM TABLE1
WHERE TO_DATE(PARAMETER_VALUE, 'DDMonYYYY') > CURRENT DATE;
If that's not the correct format of that column then you can put together the correct string from here: https://www.ibm.com/support/knowledgecenter/en/SSEPGG_11.5.0/com.ibm.db2.luw.sql.ref.doc/doc/r0007107.html
I'm using the following code to convert a string datetime variable to datetime, but the converted string is missing SSS part.
Code used:
cast(FROM_UNIXTIME(UNIX_TIMESTAMP(oldtime, "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"),"yyyy-MM-dd HH:mm:ss.SSS") as timestamp) as newtime
The outcome:
2019-03-08T18:28:36.901Z is converted to 08MAR2019:18:28:36.000000
Some other oldtimes in string:
2020-03-09T16:05:06:827Z
2020-03-09T16:03:19:354Z
2020-03-11T16:03:57:280Z
2020-03-10T16:02:57:642Z
2020-03-10T16:04:07:455Z
2020-03-10T16:04:09:737Z
2020-03-10T16:03:57:280Z
2020-03-10T16:02:46:816Z
The SSS part '901' is missing in the converted time. Would like help on keeping the SSS part since I need to sort the records by their exact time.
Thank you!
from_unixtime is always until minutes(yyyy-MM-dd HH:mm:ss) to get millisecs we need to do some workarounds.
we will extract the millisecs from the old_time using regexp_extract then concat that to from_unixtime result and finally cast to timestamp.
Example:
select old_time,
timestamp(concat_ws(".", --concat_ws with . and cast
FROM_UNIXTIME(UNIX_TIMESTAMP(old_time, "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"),"yyyy-MM-dd HH:mm:ss"), -- from_unixtime and unix_timestamp to convert without millisecs
regexp_extract(string(old_time),".+\\.(.*)(?i)z",1))) as newtime from --regexp_extract to extract last 3 digits before z then concat
(select string("2020-03-11T21:14:41.335Z")old_time)e
+------------------------+-----------------------+
|old_time |newtime |
+------------------------+-----------------------+
|2020-03-11T21:14:41.335Z|2020-03-11 21:14:41.335|
+------------------------+-----------------------+
UPDATE:
Your sample data have : before milliseconds, Try with below query:
select old_time,
timestamp(concat_ws(".", --concat_ws with . and cast
FROM_UNIXTIME(UNIX_TIMESTAMP(old_time, "yyyy-MM-dd'T'HH:mm:ss:SSS'Z'"),"yyyy-MM-dd HH:mm:ss"), -- from_unixtime and unix_timestamp to convert without millisecs
regexp_extract(string(old_time),".+\\:(.*)(?i)z",1))) as newtime from --regexp_extract to extract last 3 digits before z then concat
(select string("2020-03-11T21:14:41:335Z")old_time)e
Simply replace 'T' with space ' ' remove 'Z' and replace last ':' with dot, like this :
select regexp_replace('2020-03-09T16:05:06:827Z','(.*?)T(.*?):([^:]*?)Z$','$1 $2\\.$3');
Result:
2020-03-09 16:05:06.827
Read also this answer if you need to convert to different format, preserving milliseconds: https://stackoverflow.com/a/59645846/2700344
I currently have the following date format:
"2017-Jan-30 12:45:02:345 EST"
But I need the format to be "MM/dd/yyyy HH:mm:ss"
Does anyone know how to do this? The issue is in the Date string below
#input=
EXTRACT
Date string,
Name string,
Location string
FROM #in
USING Extractor.Csv();
OUTPUT #input
TO #out
USING Outputters.Csv();
You can do something like this:
#input=EXTRACT
[Date] String,
[Name] string,
[Location] string
FROM #in
USING Extractors.Csv();
and then in SELECT statement you format your Date column:
#result = SELECT
DateTime.ParseExact([Date],"yyyy-MMM-dd hh:mm:ss:fff EST",null).ToString("MM/dd/yyyy HH:mm:ss") AS Date
Location,
Name
FROM #input
For more DateTime formats in c# you can check this
Maybe there are null values in your dataset so you can do something like this:
string.IsNullOrEmpty([Date]) ? null : DateTime.ParseExact(Date, "yyyy-MMM-dd HH:mm:ss:fff EST", null).ToString("MM/dd/yyyy HH:mm:ss") AS Date
Instead null you can return maybe default(DateTime), depends what you want.
When i cast datetime in SQLLite, it truncates the string.
for example
select cast("2017-04-23 9:12:08 PM" as datetime) as dt
returns
2017
SQLite's CAST can only cast to the defined storage classes and can therefore only be used to cast to
NONE (blob), TEXT, REAL, INTEGER or NUMERIC.
However the normal rules for determing column-affinity are applied to the type so by coding CAST(value AS datetime) you are effectively using CAST(value AS NONE) (i.e. a BLOB).
CAST expressions
Therefore you can't effectively use CAST. However you simply use the DateTime functions against an appropriate value (accepted formats) as per Date And Time Functions e.g. :-
SELECT datetime("2017-04-23 09:12:08") as dt;
results in
2017-04-23 09:12:08
or to show date manipulation
select date(dt), dt FROM (
select datetime("2017-04-23 09:12:08") as dt
);
results in
2017-04-23
and
2017-04-23 09:12:08
However considering that your format isn't one of the accepted formats you could convert the value. This is more complex but it can be done. Here's an example that will perform the conversion (not substantially tested though) :-
SELECT
CASE WHEN (CAST(hour AS INTEGER) + CAST(adjustment AS INTEGER)) > 9 THEN
datepart||' '||CAST(CAST(hour AS INTEGER) + CAST(adjustment AS INTEGER) AS TEXT)||':'||mins_and_secs
ELSE
datepart||' 0'||CAST(CAST(hour AS INTEGER) + CAST(adjustment AS INTEGER) AS TEXT)||':'||mins_and_secs
END AS converted
FROM (
SELECT substr(ts,1,10) as datepart,
CASE WHEN instr(ts,"PM") THEN 12 ELSE 0 END AS adjustment,
CASE WHEN length(ts) = 21 THEN substr(ts,12,1) ELSE substr(ts,12,2) END AS hour,
CASE WHEN length(ts) = 21 THEN substr(ts,14,5) ELSE substr(ts,15,5) END AS mins_and_secs
FROM (
select("2017-04-23 9:12:08 PM") as ts
)
);
This would result in 2017-04-23 21:12:08.
Using select("2017-04-23 9:12:08 AM") results in 2017-04-23 09:12:08
Using select("2017-04-23 11:12:08 PM") results in 2017-04-23 23:12:08
Using select("2017-04-23 11:12:08 AM") results in 2017-04-23 11:12:08
The closest I could come up with is:
select date(datetime(strftime('%s','2017-04-23 09:12:08'), 'unixepoch'))
Result:
2017-04-23
The dateformat you have is not recognised by SQLite:
"2017-04-23 9:12:08 PM"
It does not conform to the Time string formats recognised:
A time string can be in any of the following formats:
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
HH:MM
HH:MM:SS
HH:MM:SS.SSS
now
DDDDDDDDDD
Date And Time Functions
I just try to convert datetime to date, but without success. Help me to do it:
select date('startTime') from usertable ;
select datetime(substr(startTime, 7, 4 )) from usertable;
Just try this.. it will convert datetime to date.
SELECT strftime('%d-%m-%Y', fieldname)
SELECT strftime('%d-%m-%Y', 'now') gives output 5-06-2015