Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

operation on column in SQL

Tags:

sql

mysql

I have a table in SQL database, where one column contains the character "C" or "P". I want to define a new column, where the value is 1, if this character is "C" and zero otherwise.

kind regards, Daniel

like image 747
Daniel Linders Avatar asked Apr 01 '26 14:04

Daniel Linders


1 Answers

if you want to define through projection, try

SELECT columnName, IF(columnName = 'C', 1, 0) newValue
FROM tableName

As an alternative suggested by Michael Berkowski,

SELECT columnName, (columnName = 'C') newValue
FROM tableName
like image 69
John Woo Avatar answered Apr 03 '26 03:04

John Woo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!