How to convert Date-time into Date using Netezza

2019-07-05 05:28发布

问题:

I am doing some calculation but my calculation is off because my date field is showing the time-stamp and i only want to use as Date only when i am doing the calculation. How can i just ignore the minutes and just use the date when doing the calculation? Here is what i have:

SELECT EF.DSCH_TS,
       CASE WHEN EXTRACT (DAY FROM  EF.DSCH_TS - EF.ADMT_TS)>=0 THEN 'GroupA' END AS CAL
FROM MainTable EF;

回答1:

You may want to consider rewriting your case statement to return an interval. This will allow for a little more flexibility.

        SELECT EF.DSCH_TS,
    CASE 
WHEN  age(date(EF.DSCH_TS),date(EF.ADMT_TS))>= interval '6 days' 
    THEN 'GroupA' END AS CAL
        FROM MainTable EF;


回答2:

Netezza has built-in function for this by simply using:

SELECT DATE(STATUS_DATE) AS DATE,
       COUNT(*) AS NUMBER_OF_             
FROM X
GROUP BY DATE(STATUS_DATE)
ORDER BY DATE(STATUS_DATE) ASC

This will return just the date portion of the timetamp and much more useful than casting it to a string with TO_CHAR() because it will work in GROUP BY, HAVING, and with other netezza date functions. (Where as the TO_CHAR method will not)

Also, the DATE_TRUNC() function will pull a specific value out of Timestamp ('Day', 'Month, 'Year', etc..) but not more than one of these without multiple functions and concatenate.

DATE() is the perfect and simple answer to this and I am surprised to see so many misleading answers to this question on Stack. I see TO_DATE a lot, which is Oracle's function for this but will not work on Netezza.

With your query, assuming that you're interested in the days between midnight to midnight of the two timestamps, it would look something like this:

SELECT EF.DSCH_TS,
  CASE
    WHEN EXTRACT (DAY FROM (DATE(EF.DSCH_TS) - DATE(EF.ADMT_TS)))>=0 THEN 'GroupA'
  END AS CAL
FROM MainTable EF;


回答3:

Use date_trunc() with the first argument of 'day'. I think this is what you want:

SELECT EF.DSCH_TS,
       (case when date_trunc('day', EF.DSCH_TS) >= date_trunc('day', EF.ADMT_TS) THEN 'GroupA' END) AS CAL
FROM MainTable EF;