-1
(hour(date_column ) * 4)
+ ifn(minute(date_column ) = 0, 0
     , ifn(minute(date_column ) <= 15, 1
          , ifn(minute(date_column ) <= 30, 2
               , ifn(minute(date_column ) <= 45, 3, 4))))
 as date_no

What are the equivalents of these functions in PostgreSQL?

I am trying to extract value based on 15 mins interval in an hour.

Zegarek
  • 6,424
  • 1
  • 13
  • 24
Arun P.A
  • 17
  • 3
  • 3
    What exactly does the code do? Can you please **[edit]** your question (by clicking on the [edit] link below it) and add some sample data and the expected output based on that data as [formatted text](https://meta.stackoverflow.com/a/251362). See [here](https://meta.stackexchange.com/questions/81852) for some tips on how to create nice looking text tables. (Please [edit] your question - do **not** put code or additional information in comments) –  Nov 17 '22 at 14:54
  • 1
    Are you just asking how to convert IFN() function calls into standard SQL syntax, such as CASE clause? – Tom Nov 17 '22 at 15:13
  • Please see: [Why is “Can someone help me?” not an actual question?](http://meta.stackoverflow.com/q/284236) – EJoshuaS - Stand with Ukraine Nov 17 '22 at 15:54

1 Answers1

1

It would help if you explained your goal and current logic in a bit more detail, but for now it looks like this would suffice:

select extract('hours' from date_column) * 4 
    + ceil(extract('minutes' from date_column) / 15) as date_no
from your_table;
  1. extract() is used in PostgreSQL for the same thing hour() and minute() are in SAS.
  2. You can use CASE the exact same way you would in SAS but there's no IFN() counterpart.
  3. CEIL() works the same in both, so you could also shorten your SAS version replacing all the IFN's.
  4. You're effectively binning date/times, so you could look into date_bin(). Similar effect can probably be achieved in SAS through the use of INTNX() or plain ROUND() (1, 2).

Demo

Zegarek
  • 6,424
  • 1
  • 13
  • 24