Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
437 views
in Technique[技术] by (71.8m points)

How do i convert a date in varchar (1-Jan-2020) to date type in DB2 sql?

I have a column with dates as below in my db2 table.

4-Jan-2020
7-Sep-2020
7-Dec-2020

I want to convert them to

2020-01-04
2020-09-07
2020-12-07

respectively. How do I achieve this? Thanks.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

On recent versions of DB2, use TO_DATE:

SELECT DATE(TO_DATE(date_col, 'DD-MON-YYYY'))
FROM yourTable;

Data:

WITH yourTable AS (
    SELECT '4-Jan-2020' AS date_col UNION ALL
    SELECT '7-Sep-2020' UNION ALL
    SELECT '7-Dec-2020'
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...