0

i have got problem with transfer 2 data function from oracle to postgresql can you help me:

Data:

INTERVALL = numeric(10,0)

START_DATE = timestamp without time zone

REPETITIONS = numeric(10,0)

This expression i copy from case state. I must write equivalent logical action in postgresql.

 case when (extract(DAY FROM (START_DATE + TRUNC(INTERVALL*(REPETITIONS-1) * 7)))
   - extract(DAY FROM CURRENT_TIMESTAMP)) >= 0 then 'OK'

and second:

case when (extract(DAY FROM add_months(START_DATE, TRUNC(INTERVALL*
  REPETITIONS-1) * 12))) - extract(DAY FROM CURRENT_TIMESTAMP)) <= 0 then 'ok'
4
  • 1
    Please, in order to avoid guessing games, describe your tables and provide better description of what you need to achieve. Commented Feb 21, 2013 at 21:46
  • i need only equivalent logical expression in postgresql Commented Feb 21, 2013 at 22:26
  • I think that for 1 this is good: SELECT extract(DAY FROM (CURRENT_DATE + interval '1' day * TRUNC(7 *(2-1)))) Commented Feb 21, 2013 at 22:55
  • 2
    I think comparing days is wrong and it is also wrong in Oracle. You would have better results if you explained your logic. Commented Feb 21, 2013 at 23:02

2 Answers 2

1
case when (
    extract(DAY FROM (START_DATE + INTERVALL * (REPETITIONS - 1) * interval '1 week'))
    >=
    extract(DAY FROM CURRENT_TIMESTAMP))
then 'OK'

case when (
    extract(DAY FROM (START_DATE + INTERVALL * (REPETITIONS - 1) * interval '1 year')))
    <= extract(DAY FROM CURRENT_TIMESTAMP))
then 'ok'
Sign up to request clarification or add additional context in comments.

3 Comments

Why you use * interval '1 week' ? not day ?
@ŁukaszWoźniczka, it would be much easier if you could explain logic of your calculations. For me 1 week interval here looks natural.
@ŁukaszWoźniczka You are multiplying by 7 so I suppose you want weeks.
0

PostgreSQL has way better support of date, time and interval types compared to ORACLE.

I guess (sorry, but you haven't described this), that you need to compare timestamp column with the current_timestamp based on the interval, that is calculated from columns INTERVALL and REPETITIONS. I also think that in the first case you have something to do with week intervals, and months in the second snippet. (I don't fully understand the logic behind TRUNC(…) parts of your code.

I recommend the following:

  1. rename start_date, removing _date suffix, as column's type is timestamp in fact, misleading;
  2. convert intervall to text and store interval type directly, like month or week;
  3. keep repetitions as is, defaulting to 1 for NULL values;
  4. PostgreSQL supports boolean type natively, so you don't really need the CASE … END construct here, depends on the Application though.

Therefore the following query can do it (also on SQL Fiddle):

SELECT start_dt, intervall, repetition,
       start_dt + (repetition||intervall)::interval
         >= current_timestamp AS is_it_ok
  FROM tab;

1 Comment

Its not my code :) I am only migration person :P Java developer not database admin ....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.