1

I want to select the name like 'Bo%' or name like 'Ni%' from a table and I want to give a specific name to all that name like Bo and a specific name to all that name like Ni. But if I give an alias name as this it not works.

name like 'Bo%' as Male or name like'Ni%' as female. So how can I do this?

OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
kanchana
  • 11
  • 1
  • 3

1 Answers1

1

SQL allows you to alias columns, or tables. You can't alternate a column name on a per value basis -- the name needs to be consistent so you can reference it for later operations.

You need to dedicate a column to each option you want to see:

SELECT CASE WHEN t.col LIKE 'Bo%' THEN t.col ELSE NULL END AS male,
       CASE WHEN t.col LIKE 'Ni%' THEN t.col ELSE NULL END AS female
  FROM YOUR_TABLE t

...but that means the male or female column could be null -- both will be if neither LIKE matches:

name    male  female
-------------------
Bob     Bob   NULL
Nirmal  NULL  Nirmal
Xander  NULL  NULL
OMG Ponies
  • 325,700
  • 82
  • 523
  • 502