HiveQL TimeStamp Error - hive

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

Related

MariaDB converting datetime string to date

Trying CONVERT()
Trying STR_TO_DATE()
Trying CAST() - neither DATE or DATETIME work
Trying CAST(STR_TO_DATE())
Hi, I cannot figure out what I'm doing wrong with trying to convert my varchar column Procedure_Date containing a string in the dd/mm/YYYY hh:mm:ss format. I am trying to extract just the date from this string but I seem to be cursed. Using MariaDB and have gone through all the docs there. Greatly appreciate any advice!
You can use cast
SELECT Procedure_Date, CAST(STR_TO_DATE(Procedure_Date, '%d-%m-%Y') AS DATE) FROM uniTable5;
Use: DATE('YOUR DATETIME')
EXAMPLE: DATE('2022-04-25 14:50:01')
OUTPUT: 2022-04-25. Result in DATE.

Convert timestamp to varchar

I am writing a sql query in AWS Athena, where I have a timestamp field and I had to extract the dates of June 2021 (only dates, without time).
I am able to convert timestamp to date but then having trouble with converting to varchar.
Can anybody help please. Thanks in advance.
select substr(CAST(server_time AS VARCHAR)) from matomo_log_link_visit_action where server_time like "2021-06-%" ;
You can use the format_datetime function. For example:
select format_datetime(my_timestamp, 'yyyy-MM-dd')
see the docs: https://prestodb.io/docs/current/functions/datetime.html

NULL values in a string to date conversion

I have a table with the following data:
logs.ip logs.fecha logs.metodo
66.249.93.79 19/Nov/2018:03:46:33 GET
All data columns are string and I want to convert logs.fecha into date with the following format: YYYY-MM-dd HH:mm:ss
I try the following query:
SELECT TO_DATE(from_unixtime(UNIX_TIMESTAMP(fecha, 'yyyy-MM-dd'))) FROM logs
Results of the query are NULL in all rows.
How can I make the conversion string to date for all rows? I know I must use ALTER TABLE but I don't know how to do it.
Thanks
The reason you get null is because the format of the input string is different from the input passed to unix_timestamp. The second argument to unix_timestamp should specify the string format of the first argument. In from_unixtime you can specify the output format desired. If nothing is specified, a valid input to from_unixtime returns an output in yyyy-MM-dd format.
The error can be fixed as below.
from_unixtime(unix_timestamp(fecha,'dd/MMM/yyyy:HH:mm:ss'),'yyyy-MM-dd HH:mm:ss')
You just have to tell Oracle the date format you are reading with TO_DATE.
Try:
SELECT TO_DATE(fecha,'DD/MON/YYYY:HH:MI:SS') FROM logs

Convert Date to formatted date

I have a column for dates and the values are like this 1181202 and i want to convert it to normal date format "02-12-2018" so i can compare it with another date however when i am trying the following it returning wrong year and date
SELECT CONVERT(Datetime, Text_UPD_DATE,106) -- i have tried all numbers
From Notes
it's returning 5134-01-09 00:00:00.000
Can you please advise on the correct command
At a total guess, based on your one example:
CONVERT(date,'20'+STUFF(Text_UPD_DATE,1,1,''),112)

SQlite compare dates without timestamp

In SQLite, how to compare date without passing timestamp?
The date format is 2018-03-18 08:24:46.101655+00 and I want to compare against only date part as 2018-03-18.
I have tried as where mydate='2018-03-18' but that didn't return any records.
Similarly, tried Date(mydate)='2018-03-18' but that didn't help either.
How can I compare dates ignoring the timestamp part?
select * from mytable
where strftime('%Y-%m-%d', mydate) = '2018-03-18'
This is not one of the supported date formats.
To extract the date part from the string, use substr():
... WHERE substr(mydate, 1, 10) = '2018-03-18'
It might be a better idea to store dates in a correct format in the database to begin with.
It is looking that there is problem with date format.
Sqlite doesn't understand data like '+00' in date.
So date() and strftime() will not work here if data type is 'timestamp with time zone'.
Try by using like clause.
Try using strftime
SELECT strftime('%Y %m %d', 'columnName');
you can find it here strftime.php