I intended to extend certain records in a table by adding 366 days to its date keys:
to_date(date_add(from_unixtime(unix_timestamp('20150101' ,'yyyyMMdd'), 'yyyy-MM-dd'), 366)) as new_date
2016-01-01
But how to convert this value back to format of original key i.e. 20160101 ?
Since your requested date is 2016-01-01 it seems you want to add 365 days and not 366.
select from_unixtime(unix_timestamp(date_add(from_unixtime(unix_timestamp(
'20150101','yyyyMMdd')),365),'yyyy-MM-dd'),'yyyyMMdd');
Demo
hive> select from_unixtime(unix_timestamp(date_add(from_unixtime(unix_timestamp(
> '20150101','yyyyMMdd')),365),'yyyy-MM-dd'),'yyyyMMdd');
OK
20160101
date_add gives you a date type as output.
As you have already used from_unixtime and unix_timestamp, I'll assume that you are already aware of their functionalities.
In Hive/Impala, there's no native DATE_FORMAT function like MySQL/MariaDB, so you'll have to convert the output of your date_add to unix_timestamp and then use from_unixtime on the output to achieve the desired format.
Something along the lines of:
select
from_unixtime(unix_timestamp(
date_add(from_unixtime(unix_timestamp('20150101' ,'yyyyMMdd')), 365)),
'yyyyMMdd');
Related
I am trying to convert a date column (ie. 2012-10-02) to the first day of the year with time (ie. 2012-01-01T00:00:00) in sql.
Is there a way to do so in the SELECT query?
for BigQuery use below
select timestamp_trunc('2012-10-02', year)
with output
2012-01-01 00:00:00 UTC
Note - if you column is of date type - the output will be
2012-01-01T00:00:00
and finally, you can use datetime_trunc instead of timestamp_trunc and you will get expected result - 2012-01-01T00:00:00
Look at the YEAR() function.
It would allow you to extract just the year, and then just as the date and time you need.
My date column "timestamp" is currently listed as:
2020-11-16 20:27:38.033 +0000
It's formatted as timestamptz and I've tried every search on here and google to find a method to only pull the date part (in this example 2020-11-16) from the column so I can effectively start grouping data by Date.
Any help would be greatly appreciated.
Assuming (as you haven't stated) that the column is a string. This shows how to convert:
postgres=# SELECT ('2020-11-16 20:27:38.033 +0000'::timestamp)::date;
date
------------
2020-11-16
If it were already a timestamp, then just the ::date cast would work.
You can use ::DATE casting or use TO_CHAR() conversion if the aim is just to display in that format
such as
SELECT your_ts_column::DATE AS val_as_date,
TO_CHAR(your_ts_column, 'YYYY-MM-DD') AS val_as_str
FROM your_table
Demo
One of my columns is a date type in the following format: YYYY-MM-DD. I want to extract YYYY-MM. So far, the resources I've come across show me that I can extract either year using SELECT extract(year from order_date)... but I can't figure out how to extract both the year and the month. I tried the following but it didn't work: https://www.w3schools.com/sql/func_mysql_extract.asp
I just want to point out that it is often convenient to leave the value as a date. If so, use date_trunc():
select date_trunc('month', order_date) as yyyymm
If you really want a string, you should accept Nick's answer.
In PostgreSQL you can use TO_CHAR():
SELECT TO_CHAR(order_date, 'YYYY-MM')
Output (if order_date = '2020-04-06'):
2020-04
Note if your column is not already a date or timestamp you will need to cast it to a date or timestamp (e.g. order_date::date).
Demo on dbfiddle
I want to insert the current date into one of the columns of my table. I am using the following:
to_date(SYSDATE, 'yyyy-mm-dd'));
This is working great, but it is displaying the year as '0014'. Is there some way that I can get the year to display as '2014'?
Inserting it as TRUNC(sysdate) would do. Date actually doesn't have a format internally as it is DataType itself. TRUNC() actualy will just trim the time element in the current date time and return today's date with time as 00:00:00
To explain what happened in your case.
say ur NLS_DATE_FORMAT="YY-MM-DD"
The Processing will be like below
select to_date(to_char(sysdate,'YY-MM-DD'),'YYYY-MM-DD') from dual;
Output:
TO_DATE(TO_CHAR(SYSDATE,'YY-MM-DD'),'YYYY-MM-DD')
January, 22 0014 00:00:00+0000
2014 - gets reduced to '14' in first to_char() and later while converted again as YYYY.. it wil be treated as 0014 as the current century detail is last!
to_date is used to convert a string to a date ... try to_char(SYSDATE, 'yyyy-mm-dd') to convert a date to a string.
The to_date function converts a string to a date. SYSDATE is already a date, so what this will do is to first convert SYSDATE to a string, using the session's date format as specified by NLS settings, and then convert that string back to date, using your specified date format (yyyy-mm-dd). That may or may not give correct results, depending on the session's NLS date settings.
The simple and correct solution is to skip the to_date from this and use SYSDATE directly.
Try this to_date(SYSDATE, 'dd-mm-yy')
I have a field which its format is date with time as: YYYY-MM-DD HH-MM-SS for example: 2000-08-12 00:00:00 I want to get just the date part and change its format to DD/MMM/YYYY for example the expected result of the previous example will be: 12/Aug/2000
The field definition is: Ddate timestamp without time zone DEFAULT now()
I read the whole page of Date/Time Functions and Operators and other sources as well but I couldn't find any information that is helpful.
You can use the to_char function to format your column:
SELECT TO_CHAR(ddatte, 'dd/Mon/yyyy') FROM mytable
try with:
to_char(your_Field, 'dd/mm/yyyy')