How to change a date format in postgres? - postgresql

I am trying to convert a character varying date field to the format YYYYMMDD and below is the select query I tried. The output format is 1999-04-27 (YYYY-MM-DD). Admindate is the field name in the table.
select to_date( admindate,'MMDDYYYY') from test;
Can someone please advice what I am doing wrong?

Use to_char to convert the converted date into the desired format.
select to_char(to_date(admindate, 'MMDDYYYY'), 'YYYYMMDD')
from test;

you can try this
SELECT TO_CHAR(NOW() :: DATE, 'dd/mm/yyyy');

Related

SAS Date conversion from text YYYY/MM to DATETIME20. Format

Hi I have a date format in one table in Text 'YYYY/MM'. example 2018/01, 2020/08 etc.
I need to join it with another table where the date is in Number type( and DATETIME20 format attached it it) and convert it to month and compare.
Is there any way to do it in PROC SQL as the rest of my query is in PROC SQL?
eg. Table 1: Month= 2018/01; Table 2: Date =20.01.2018 10:48:17 . They should be joined in the PROC SQL query.
I would also like to calculate difference in Months between these two dates.
Thank you in advance.
Convert both to the same DATE value. To convert a datetime value to a date use the DATEPART() function. To move to the first day of the month use the INTNX() function with the month interval. To convert a string like '2018/01' to a date you could use INPUT() function with YYMMDD informat by appending '/01'.
proc sql ;
create table want as
select *
from table1,table2
where input(cats(table1.month,'/01'),yymmdd10.)=intnx('month',datepart(table2.date),0)
;
quit;

How to create 'Week column' based on another date column?

I have column 'jobstarttimeiso' and I want to create another column for Weeks of the year based on the date. How would I go about doing that? I am using Redash to query from redshift database. Please help! Thank you.
The extract function will return a week number e.g.
select extract(week from jobstarttimeiso) as weeknumber
In general:
EXTRACT ( datepart FROM { TIMESTAMP 'literal' | timestamp } )
See: https://docs.aws.amazon.com/redshift/latest/dg/r_EXTRACT_function.html
alternatively you can also use to_char()
TO_CHAR (timestamp_expression | numeric_expression , 'format')
with the parameter IYYY as the format for ISO 8601 week-numbering year (4 or more digits)

Postgresql date inconsistent format

I am building an app using node with a Postgres backend. I have a column for date where the data type is date. The dates in the database are stored in the format YYYY-MM-DD. However, when I do a query in my app, the date returned is in the format YYYY-MM-DD + (time zone information). For example:
2016-01-06T05:00:00.000Z
Does anyone know how I can prevent this?
Thanks in advance!
Use to_char
SELECT to_char(dateColumn, 'YYYY-MM-DD') AS formattedDate FROM ...
Example:
ubuntu=> SELECT to_char(to_timestamp('2016-01-07', 'YYYY-MM-DD'), 'YYYY-MM-DD');
to_char
------------
2016-01-07
(1 row)

Convert varchar2 to Date ('MM/DD/YYYY') in PL/SQL

I need to convert string from varchar to Date in 'MM/DD/YYYY' format. My input string is '4/9/2013' and my expected output is '04/09/2013'. i.e. 2 digit month, 2 digit date and 4 digit year seperated by'/'
I have below data:
DOJ varchar2(10 Byte)column has '4/9/2013' value.
I am using 'select TO_DATE(DOJ,'MM/DD/YYYY') from EmpTable. But I am getting result as 09-APR-13. I want it in 04/09/2013 format.
Please advise. Thanks in advance.
Easiest way is probably to convert from a VARCHAR to a DATE; then format it back to a VARCHAR again in the format you want;
SELECT TO_CHAR(TO_DATE(DOJ,'MM/DD/YYYY'), 'MM/DD/YYYY') FROM EmpTable;
An SQLfiddle to test with.
First you convert VARCHAR to DATE and then back to CHAR. I do this almost every day and never found any better way.
select TO_CHAR(TO_DATE(DOJ,'MM/DD/YYYY'), 'MM/DD/YYYY') from EmpTable
Example query:
SELECT TO_CHAR(TO_DATE('2017-08-23','YYYY-MM-DD'), 'MM/DD/YYYY') FROM dual;

Cannot insert date(Y-m-d H:i:s) in Postgres "timestamp"

I am trying to insert data in Postgres with cakephp which includes a date.
The column in my database is a timestamp without timezone and I have a datetime string "Y-m-d H:i:s" (also tried with an int).
Must I cast that value?
You do not have to cast the inserted values as long as the text literal is unambiguous and in accepted format.
Don't mistake a date ('2011-10-21') for a timestamp ('2011-10-21 12:10:23').
This is perfectly legal for a timestamp:
INSERT INTO tbl (timestamp_col)
VALUES ('2011-01-01 0:0:0');
But a date you have to cast, resulting in '2011-01-01 0:0:0' in this case:
INSERT INTO tbl (timestamp_col)
VALUES ('2011-01-01'::timestamp);
ISO 8601 format 'yyyy-mm-dd' for a date and 'yyyy-mm-dd hh:mm:ss' for a timestmap are unambiguous for any locale. Other formats may depend on your locale.
use
'Y-m-d H:i:s'::timestamp
Don't forget single quotes
for example
select '2011-01-01'::timestamp