Вопрос

I have saved date as text type. There are a few invalid dates those are preventing me from running any date related operation. For e.g.

select case when deliver_date = '0000-00-00 00:00:00' then '2014-01-01' else deliver_date::date end as new_date, count(*) as cnt  from some_table group by new_date
Error in query: ERROR: Error converting text to date

I am using the following work-around that seems to be working.

select left(deliver_date,10) as new_date, count(*) as cnt  from sms_dlr group by new_date

But I will like to know if it is possible to convert this column to date.

Это было полезно?

Решение

Try dropping the ::date part:

select
    cast(
        case
            when deliver_date = '0000-00-00 00:00:00' then '2014-01-01'
            else deliver_date
        end
        as date
    ) as new_date,
    count(*) as cnt
from some_table
group by new_date

Другие советы

You need to separate the valid and invalid date values.

One solution is to use regular expressions- I'll let you decide how thorough you want to be, but this will broadly cover date and datetime values:

SELECT 
    CASE 
    WHEN 
        deliver_date SIMILAR TO '[0-9]{4}-[0-9]{2}-[0-9]{2}' 
        OR deliver_date SIMILAR TO '[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}'  
    THEN TO_DATE(deliver_date, 'YYYY-MM-DD')
    ELSE TO_DATE('2014-01-01', 'YYYY-MM-DD') 
    END as new_date,
    COUNT(*) as cnt
FROM some_table
GROUP BY new_date
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top