Search code examples
sqlpostgresqlsql-date-functionssqldatetime

Derive a new column based on Time stamp


I have a column 'customer_date' with datatype text.

Based on the time i need to derive a new column

Condition :

  • when customer_date <12 then 'AM' else 'PM


customer_date

01Mar2018 10:03:54
02Mar2018 13:03:54

expected output

  customer_date             session

01Mar2018 10:03:54        AM
02Mar2018 13:03:54        PM

It throws me error when i wrote

select 
case EXTRACT(HOUR FROM customer_date)<12 then 'AM' else 'PM' end as session
from my table;

Solution

  • This works when I test it:

    select (case when EXTRACT(HOUR FROM customer_date::timestamp) < 12 then 'AM' else 'PM' end) as session
    from mytable;
    

    I added the explicit conversion and the when.