1

I want to set 0 or 1 flag based on column value.if the value is > 0.0 AND <= 99.0 then 1 ,else 0 .

              Score         flag_score

              0.083642      1
              0.009542      1
              0.999999      1
              101.0000      0
1
  • 1
    select score Score, case when score > 0.0 AND score <= 99.0 then 1 else 0 end flag_score from table; Commented May 11, 2018 at 18:36

2 Answers 2

3

Have you tried case expression ?

select *, 
        (case when value > 0.0 and value <= 99.0 then 1 else 0 end) flag_score
from table;
Sign up to request clarification or add additional context in comments.

3 Comments

Case statement works..But cant we write if statement for the above case
@user8545255 No. There is no if statement in the SQL standard.
@user8545255: You could write your own if() function if it'd made you happy.
1

I would write this simply as:

select t.*, ( (value > 0.0) and (value <= 99.0) )::int as flag_score
from t;

Postgres has a nice shorthand for declaring flags, so case expressions are not needed.

If you want to actually set the value, you can do:

update t
    set flag = ( (value > 0.0) and (value <= 99.0) )::int;

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.