TIMESTAMP in hive? - sql

I have one column , data as 'Apr 06 2016 05:30:30' it is not in the time stamp formate, when using this one as timestamp I am getting null values. So stored as string, now I want to do some calculation on this when it is in time stamp formate. for that i converted into unixtimestamp and getting back to timestamp formate but the value of the date is changed. I used conversions as 'select from_unixtime(unix_timestamp(start_time, 'MMM DD YYYY HH:mm:ss')) from temp;'
I got value as '2015-12-27 05:30:30'.
I want final data as '2016-04-06 05:30:30'.
Please help me on this

You have just written the wrong format. The proper format string is 'MMM dd yyyy HH:mm:ss'. Take a look at https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html for reference for format strings.

Related

How to insert DD MON YYYY format value into a date column in a snowflake table

My csv file has a date column having values in DD MON YYYY format eg: 28 Nov 2022.
When i tried inserting it into a date column(datatype= DATE) it is showing the below error. I have also tried using TO_DATE , TO_VARCHAR but getting the same error.
Kindly help me to resolve this.
Error: Date '28 Nov 2022' is not recognized
I want to insert the value in the same format (DD MON YYYY) into a column of date data type ,without changing the format i.e '28 Nov 2022'.
I was reading documentation (https://docs.snowflake.com/en/sql-reference/data-types-datetime.html#date) and i read: "DATE accepts dates in the most common forms (YYYY-MM-DD, DD-MON-YYYY, etc.)."
So i think the format you are trying to write is a no-supported date format.
you can:
format your date in a supported date format before write field in db.
write in a varchar datatype field, but in this case you'll lose all tools on date type.
I don't see other ways!

How to convert string with GMT to date in Athena

I have a column filled with dates in string format, e.g. 2023-01-31 11:21:33 GMT.
I am trying to write a query that will select a year and a month and will do some calculations later on. My standard approaches using EXTRACT(YEAR FROM a)) etc. did not work. Therefore, I am trying to parse datetime using PARSE_DATETIME(a, 'yyyy-mm-dd hh:mm:ss'). The thing is, I don't know how to format "GMT" and google did not help with that.
The error message is INVALID_FUNCTION_ARGUMENT: Invalid format: "2023-01-31 11:21:33 GMT" is malformed at "GMT".
Use 'yyyy-MM-dd HH:mm:ss z':
select parse_datetime('2023-01-31 11:21:33 GMT', 'yyyy-MM-dd HH:mm:ss z')
Output:
_col0
2023-01-31 11:21:33.000 UTC
parse_datetime is Java date function which uses JodaTime’s DateTimeFormat pattern format which is mostly compatible with java.text.SimpleDateFormat with z matching general timezone.

In SQL How to convert time into UNIX timestamp

In hive there is some data I have. Now I want to convert the start_timestamp into unix_timestamp in second. How to do that? Because the start_timestamp has two formats:
First format:
2018-03-22 02:54:35
Second format:
May 15 2018 5:15PM
First format is 'yyyy-MM-dd HH:mm:ss', second is 'MMM dd yyyy hh:mm:aa'. If the format is wrong, unix_timestamp function will return NULL. Try to convert using one format, if NULL, try to convert using the other format. This can be done using coalesce function:
select
coalesce(unix_timestamp(start_timestamp ,'yyyy-MM-dd HH:mm:ss'),
unix_timestamp(start_timestamp ,'MMM dd yyyy hh:mm:aa')
) as UnixTimestamp
from my_table;
Use from_unixtime() to convert it back to given format if necessary, like in this answer.
See patterns examples here: SimpleDateFormat

Store date without time in oracle [duplicate]

I have a date field in a table that contains date in dd-MMM-yy format .
I want to create a function that get this date check it for not null and then change it to yyyy/mm/dd format
but the problem is that oracle doesn't accept dd-MMM-yy formate date as a input parameter and
it say : Please Use yyyy-MM-dd date format !!!
how can I first change dd-MMM-yy formate to yyyy-MM-dd so oracle accept it as input then change it to yyyy/mm/dd
: Please Use yyyy-MM-dd date format !!!
That is no way related to an Oracle error.
I have a date field in a table that contains date in dd-MMM-yy format .
No, you are confused. Oracle does not store dates in the format you see. It stores it internally in 7 bytes with each byte storing different components of the datetime value.
Byte Description
---- -------------------------------------------------
1 Century value but before storing it add 100 to it
2 Year and 100 is added to it before storing
3 Month
4 Day of the month
5 Hours but add 1 before storing it
6 Minutes but add 1 before storing it
7 Seconds but add 1 before storing it
If you want to display, use TO_CHAR with proper FORMAT MODEL.
While inserting, use TO_DATE with proper FORMAT MODEL.
What you see as a format by default, is your locale specific NLS settings.
SQL> select parameter, value from v$nls_parameters where parameter='NLS_DATE_FORMAT';
PARAMETER VALUE
--------------- ----------------------------------------------------------------
NLS_DATE_FORMAT DD-MON-RR
SQL> select sysdate from dual;
SYSDATE
---------
03-FEB-15
SQL> select to_char(sysdate, 'mm/dd/yyyy hh24:mi:ss') from dual;
TO_CHAR(SYSDATE,'MM
-------------------
02/03/2015 17:59:42
SQL>
Update Regarding MMM format.
By MMM if you mean the month name up to three characters, then use MON.

SQLite Data Time Function

I currently have a timestamp in this format Tue Jun 03 17:17:05 +0000 2014 in one column in my table. I want to count the number of records happening in specific intervals (15 minutes). I have tried to follow the answer found in Group records by time. Although my timestamp is in a different format and I haven't seen any support function available in SQLite to convert this. Is this possible in SQL?
The SQLite date and time functions can be used to convert a timestring to a canonical format, or to a Julian Day Number. Unfortunately, the SQLite date and time functions only accept timestring in a limited number of 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
If your timestring format has fixed field widths, you can use the substr function and the || string concatenation operator to convert it to a format SQLite understands. You'll have to use a case expression to convert the month names to numbers; here's an example.
You may use NEW_TIME in Oracle to convert the time to a specific timezone. Here is an example. This example is converting SYSDATE from PDT to GMT.
SELECT NEW_TIME (SYSDATE, 'PDT', 'GMT') FROM DUAL;
This thread is detailing how to add required minutes to your timestamp.