I want the time portion from a timestamp column.
E.g. if load_ts= 2016-01-04 11:34:35, then I want only 11:34:35. I have tried using select concat(hour(load_ts),':',minute(load_ts),':',second(load_ts)) as note_time from rushisourcepart; but it is not giving correct output.
Please give your suggestions.
You can use substr(load_ts, 12)
hive> select substr('2016-01-04 11:34:35', 12);
OK
11:34:35
Related
I am trying to insert the data into the final table in snowflake from the staging table. When the command is run it give the error:
Can't parse '20211101132344205550' as timestamp with format 'YYYYMMDD HH24:MI:SS.FF'
My table definition and insert statement is here.
I used the same kind of method last time it worked. Thank you so much in advance.
CREATE OR REPLACE TABLE dw.tb_fidctp_order(
sysdate DATE,
record_id NUMBER(18,0) ,
timestamp TIMESTAMP_NTZ(9),
<trim_excess>
);
INSERT INTO dw.tb_fidctp_order(
sysdate,
record_id,
timestamp,
<trim_excess>
)
SELECT
TO_DATE(LEFT(timestamp, 8), 'YYYYMMDD')
,CAST(record_id AS NUMBER(18,0))
,TO_TIMESTAMP(LEFT(timestamp,24),'YYYYMMDD HH24:MI:SS.FF')
<trim_excess>
FROM stg.tb_fidctp_order_input;
In Snowflake you need to define what your format is. So, if all of your timestamps are formatted as a straight string like that, then you are defining it incorrectly. Try something more like this:
SELECT to_timestamp(left(EXPIRY_DATETIME,24),'YYYYMMDDHH24MISSFF');
The to_timestamp() function is defining how the input string is formatted, not how you want the timestamp to be formatted as an output.
So the error message is the critical point, your formating string for timestamps have space and : time formatting, which needs to be removed.
Below I have used the tru_to_timestamp function because it returns NULL istead of erroring with is helpful to show the problem is your formatting:
SELECT
'20211101132344205550' AS a
,try_to_timestamp(a, 'YYYYMMDD HH24:MI:SS.FF') as b
,try_to_timestamp(a, 'YYYYMMDDHH24MISSFF') as c;
gives:
A
B
C
20211101132344205550
2021-11-01 13:23:44.205
which shows your ,TO_TIMESTAMP(LEFT(timestamp,24),'YYYYMMDD HH24:MI:SS.FF')
should be:
,TO_TIMESTAMP(LEFT(timestamp,24),'YYYYMMDDHH24MISSFF')
and then you would have no problem.
I need to make the date in the below particular format in big query, but somehow it is truncating leading zero's automatically. How to get the output as is.
Query used:
select FORMAT_DATETIME('%Y-%m-%d %H:%M:%E*S','0001-01-01')
output: 1-01-01 00:00:00
Desired output: 0001-01-01 00:00:00
Please help.
Use $E4Y for the year:
select FORMAT_DATETIME('%E4Y-%m-%d %H:%M:%E*S', '0001-01-01')
'%Y' uses only as many characters as needed for the year. '%E4Y' always uses 4 characters.
I have a date column which I need to convert to character. But doing so it retrieves '00-000-00' even though the actual data has date value. Also, when I convert it again to date it gives the next day value.
For example:
Value: 25-MAR-17 (Date Datatype)
TO_CHAR(Value, 'DD-MON-YY'): '00-000-00'
TO_DATE(Value, 'DD-MON-YY'): '26-MAR-17'
And it's not happening for all the values but only the recent values.
Here is the Dump value present in that date column:
select OrderNumber, LoanDate, Dump(LoanDate) from OrderDetail
where OrderNumber=283402
OrderNumber| LoanDate | Dump(LoanDate)
283402 | 26-MAR-17 | Typ=12 Len=7: 120,117,3,27,0,15,40
Can you please explain me why these weird things happening and how to handle this?
Putting as answer instead of comment, as it would be too big for comment
What you are saying about 0 is not possible. You might get zeroes, if you have set NLS_DATE_FORMAT to display only time. Also you might get next date due to timezone conversion.
To troubleshoot it follow these steps, edit the question, and give the output in the question.
SELECT * FROM nls_session_parameters;
Run select sysdate from dual; and show the output.
Run this alter session set nls_date_format = 'DD-MON-YYYY HH24:MI:SS';
Run select sysdate from dual and show the output.
Now select your date again, select <date column> from <your table> where rownum<2;
Run select to_char(date_column,'DD-MON-YYYY') from your_table where row_num<2;
I have the hiveQL query shown below in which I am trying to get the timestamp values for the target_end_date and date fields with the day value in each set to 1.
My timestamp values are coming out way off like 2013-07-10 coming out as 1970-01-01, any tips on how to fix it would be greatly appreciated.
select
to_date(target_end_date) as target_end_date,
to_date(date) as date,
timestamp(concat(year(target_end_date),'-',month(target_end_date),'-1')),
timestamp(concat(year(date),'-',month(date),'-1'))
from
pns_serial_renewal_vw
Try using unix_timestamp(). As the doc says, you need to pass yyyy-MM-dd HH:mm:ss format date string to this function.
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-DateFunctions
I dont have proper timestamp in table; is it possible to delete 1 day old logs even now?
I have a column name as SESSION_IN which is basically a VARCHAR datatype, and the value will be like
2013-10-15 02:10:27.883;1591537355
is there any way to trim the number after ; and is it possible to compare with "sysdate" identifier?
This SP should compare all the session IDs with current datetime and it should delete if it is older then 1 day.
You can igonre time part and convert date into required format somthing like this
SYSDATE - to_date('date_col','YYYY-DD-MM')
then you can perform operations.
Use the Substring function to extract the datetime portion from the record, then use convert to datetime to cast it to datetime, and then finally use datediff to check if it was inserted yesterday. Use all these caluses in a
DELETE FROM table
WHERE ___ query
For Oracle you could use something like this:
SELECT
TRUNC(to_timestamp(SUBSTR('2013-10-15 02:10:27.883;1591537355',1,
(
SELECT
instr('2013-10-15 02:10:27.883;1591537355', ';')-1
FROM
dual
)
), 'YYYY-MM-DD HH:MI:SS.FF'))
FROM
dual;
Which gives you just the date portion of your input string. Just subtract the amount of days you want to log at the end.
Hope following query helps you:
Select Convert(Datetime,Substring('2013-10-15 02:10:27.883;1591537355',1,23)), DateDiff(dd,Convert(Datetime,Substring('2013-10-15 02:10:27.883;1591537355',1,23)),Getdate())