I have a time column with values in HH:MM:SS format. How do I get only the hour from that column?
I have tried using extract(hour) function.
select extract(hour from opentime::datetime)
from transaction
I got an error saying Amazon Invalid operation: Timestamp out of range. I assume this is because the opentime column isn't exactly DateTime but only time.
I need to get only the hour.
column value --> 09:00:00
expected output --> 09
If the input column is a string, then extract the hour with:
LEFT(opentime, 2)::integer
You can perform a SUBSTRING operation on TIME columns
select '13:21:23'::TIME as t, substring(t, 1, 2);
returns
t hour
13:21:23 13
Related
I searched up and down but couldn't find anything that works.
I have a date that is stored as a string in this format: '2021-9-01' so there are no leading zeros in the month column. This is an issue when trying to select a max date as it interprets September to be greater than October.
Any time I run something that tried to convert this it literally never finishes. I can pull back 1 row when selecting * from... but this fails to complete:
select unix_timestamp(bad_date, 'yyyy-m-dd') from mytable
I'm using hive query so not sure how to make this conversion work so I can actually get October (this month) to show up as the max date?
Correct pattern for month is MM. mm is minutes.
from_unixtime(unix_timestamp(bad_date, 'yyyy-M-dd'),'yyyy-MM-dd')
One more method is to split and concatenate with lpad:
select concat_ws('-',splitted[0], lpad(splitted[1],2,0),splitted[2])
from
(
select split('2021-9-01','-') splitted
)s
Result:
2021-09-01
I have a DateTime column (activation_dt) in DB2 table and I want to add 1 day to the date part and my output should be "date 00:00:00".
For example:
How it is - 5/9/2001 03:00:00
how it should be - 5/10/2001 00:00:00
I tried using Concat function but is not working. Date part I am doing as "date(activation_dt +1 day) as new_dt"
Please help how should I achieve this is DB2.
You could cast it to a DATE, then TIMESTAMP. Here is the invers, try out the individual parts.
values date(timestamp(current date))
BTW: What is CURRENT TIMEZONE?
Try this
VALUES date_trunc('DAY', CURRENT TIMESTAMP + 1 DAY)
it will always return the next day 00:00:00 - use activation_dt instead of current timestamp
I have a column with where dateTime stamp is present. Want to extract the value of the hour in 24 hours format while the time value stored in the field is in 12 Hours format, in AM and PM.
Maybe, You use the function to_char and set the parameter yyyy-mm-dd hh24:mi:ss.
Check this query:
SELECT TO_CHAR(column_name, 'yyyy-mm-dd hh24:mi:ss') FROM table_name;
I have a timestamp with timezone column in one of my tables. I need to extract both hours and minutes from the timestamp with timezone column using extract function but i am unable too.
I tried like this,
extract(hour_minute from immi_referral_user_tb.completed_time) >= '06:30'
but I am getting a syntax error.
I am using extract function in where clause
immi_referral_user_tb.completed_time = timestamp with timezone column
Is there any other way too accomplish this?
You can cast the column to a time data type and compare that to a time value:
immi_referral_user_tb.completed_time::time >= time '06:30'
I am block-inserting data from Stata (a statistics package) into a Teradata database. I am having trouble converting dates and timestamps from Stata's native format to Teradata's.
Stata stores dates as days since 01/01/1960, so that 01jan1960 is 0 and 02jan1960 is 1. Timestamps are stored as milliseconds since 01jan1960 00:00:00.000, so that 1000 is 01jan1960 00:00:01. Here are some examples:
timestamp Stata's tstamp date Stata's date
2015-04-13 03:07:08 1744513628000 2015-04-13 20191
2015-04-14 19:55:43 1744660543000 2015-04-14 20192
2015-04-08 11:41:39 1744112499000 2015-04-08 20186
2015-04-15 06:53:34 1744700014000 2015-04-15 20193
I tried 2 approaches. The first involves converting the dates/timestamps to strings in Stata before inserting and then doing something like this once the data is inserted:
ALTER TABLE mytable ALTER date_variable DATETIME
However, I cannot figure out how to do the second part from the documentation I have and after searching the various fora.
The second approach is leaving the dates and timestamps as integers, and then doing some of conversion once the integers are inserted. Perhaps I can also pre-convert dates in Stata to TD's internal format with:
gen td_date = ((year(stata_dt)-1900)*10000 + month(stata_dt)*100 + day(stata_dt))
However, I am not sure what the formula for timestamps would be. I am also not sure how to do the second part (making the integers into dates/timestamps).
You can't change the datatype of a column in Teradata from string to date/timestamp.
But when you insert a string into a date/timestamp column there will be an automatic typecast. So simply convert to a string with 'yyyy-mm-dd' or 'yyyy-mm-dd hh:mi:ss' format.
You could also do the conversion during load on Teradata using calculations, but IMHO the 1st solution is preferable:
-- add the number of days to the start date
DATE '1960-01-01' + stata_dt
-- I use a similar approach for Unix Timestamps starting 1970 :-)
-- split into days and seconds
CAST(DATE '1960-01-01' + (stata_ts / 86400000) AS TIMESTAMP(0))
+ ((stata_ts MOD 86400000 / 1000) * INTERVAL '00:00:01' HOUR TO SECOND)