I have a VARCHAR field storing an iso 8601 date as follows: 2015-01-13T23:17:00
I would like to convert the date into a TIMESTAMP.
It seems that I have to strip out the 'T' to use the TIMESTAMP_FORMAT function:
SELECT
T.F1,
T.F2,
TIMESTAMP_FORMAT(
REPLACE(T."log_date", 'T', ' '), 'YYYY-MM-DD HH24:MI:SS'
) TS
FROM
"MYSCHEMA"."MYTABLE" T
This feels a bit clumsy. What is the recommended way of parsing ISO 8601 dates in dashDB?
It seems that the approach I taken in dashDB although a bit kludgey but is similar to what others have recommended for DB2. See some answers for DB2, here.
Related
I've date in dd-mm-yyyy format, I need the date in yyyy-mm(month) format. I'm using postgresql.
Thanks in advance.
date values don't have any format. Any formatting you see is applied by your SQL client. To turn a date into a string with a specific format, you can use to_char()
to_char(the_column, 'yyyy-mm')
I need to execute a INSERT statement writing a date with a YYYY-MM-DD format.
Would to_date('2021-09-28','YYYY-MM-DD') work?
YYYY-MM-DD is the the ISO 8601 standard date format and unambiguous default in Postgres. Just insert your date literally.
The type date is stored as a 4-byte integer quantity internally, which does not preserve any format. You can format any way you like on output with some basic locale settings or settings of your client, or explicitly with to_char().
Input with to_date('2021-09-28','YYYY-MM-DD') works, too. But you don't need to_date() while operating with ISO format.
there is surprisingly little on the internet on converting a date that's in the Julian format YYYYDDD to YYYYMMDD. Does anyone know how to do it in PostgreSQL?
You can use to_date()
select to_date('2020123', 'yyyyddd')
Online example
I'm trying to add only the current date in "DD/MM/YYYY" format in a field of type ' timestamp in PostgreSQL' .
I try:
select to_char(now(),'DD/MM/YYYY') as date;
But PostgreSQL return me:
TIP : You will need to rewrite the expression or apply a type conversion.
There is no such thing as "only the date" in a timestamp field. A timestamp field will store timestamps.
Try using the date type instead. Please read about this here.
Also, please consider using the ISO 8601 format instead. Getting used to it helps in a lot of cases.
Human-readable formats like "DD/MM/YYYY" should only be used for presentation.
If you want to use timestamp fields and insert a human-readable formatted dates, then you are looking for:
to_timestamp('05/04/2016', 'DD/MM/YYYY')
If it is about the current date, then Postgres provides the CURRENT_DATE function, which you may use:
SELECT CURRENT_DATE;
INSERT INTO t (timestamp_field) VALUES (CURRENT_DATE);
I am saving the date as varchar in db2 with yyyy/mm/dd format i need to convert it to date datetype in the query how to achieve this?
I tried
select DATE(CRDTR2) from ASAODLIB.SSLR204 where CRDTR2 BETWEEN
'2015/03/01' AND '2015/03/31';
query and got the below error
The syntax of the string representation of a datetime value is
incorrect.. SQLCODE=-180, SQLSTATE=22007, DRIVER=3.68.61
can someone help me.
If your DB2 version is new enough, use to_date: select DATE(TO_DATE(CRDTR2, 'YYYY/MM/DD')) from ...
...because it doesn't recognize that format. I'd turn it into *ISO first, via REPLACE:
SELECT DATE(REPLACE(CRDTR2, '/', '-'))
FROM ASAODLIB.SSLR204
WHERE CRDTR2 BETWEEN '2015/03/01' AND '2015/03/31'
Incidentally, there's a couple other things here.
You should be storing dates as an actual date type, which would make this a non-issue.
You shouldn't use BETWEEN, in preference for an exclusive upper-bound (< - the blog talks about SQL Server, but the problem is really due to representation. That, and most versions of DB2 allow you to specify fractional seconds in timestamps...).