How to assign value to a variable in select query in postgresql

1.4k views Asked by At

I am migrating from mssql to postgresql

I am trying to assign a value to a temporary variable in select query

Below is the query in mssql

select flag = 0

It gives 0 as output wit flag as column identifier in mssql

I tried the following in postgresql

select flag := 0
select flag [:=0]

It says syntax error at or near :=

Does anybody know where I am going wrong?

1

There are 1 answers

0
AudioBubble On

It gives 0 as output wit flag as column identifier in mssql

Postgres honors the SQL standard and a column alias is defined with the AS keyword there.

So to get a column named flag with the value zero use the following:

select 0 as flag

flag = 0 in standard SQL (and Postgres) is a boolean expression that compares the the column flag with the value zero and returns either true, false or null depending on the column's value.