0

How can I make a valid request like:

UPDATE b2c SET tranche = '18 - 25'
WHERE (dateofbirth::date BETWEEN NOW()::date - 18 'year' AND NOW()::date - 25 'year')

Thanks for help

3 Answers 3

1
dateofbirth::date BETWEEN 
    (NOW() - interval '25 year')::date AND 
    (NOW() - interval '18 year')::date
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but I have 'nothink update', however I have in 'dateofbirth' some date like 1995-08-04, 1994-08-07, 1993-11-11...
sorry, I just had to switch 25 year with 18 year... I didn't know order of BETWEEN argument have an importance...
@Macbernie Fixed. The dates were inverted. It is the oldest first.
1

By using EXTRACT() and Age()

select EXTRACT(year FROM age('1995-08-04'::date))::int age 

Output:

age
integer
--------
      20

So you can write where condition like below

UPDATE b2c SET tranche = '18 - 25'
WHERE EXTRACT(year FROM age(dateofbirth))::int >=18 
 AND  EXTRACT(year FROM age(dateofbirth))::int <=25

Comments

1

You can use Postgres cast syntax:

UPDATE b2c SET tranche = '18 - 25'
WHERE dateofbirth::date BETWEEN 
    NOW()::date - '25y'::interval AND 
    NOW()::date - '18y'::interval

Comments

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.