Reputation: 395
I have a table which has two columns - city and country. Given a string 'X', how to know the column name which has the value 'X'?
For example, if 'X' is new york, query should return column name City and if 'X' is USA, query should return column name Country.
Upvotes: 0
Views: 2370
Reputation: 1707
select count(*) from tableName where city = "X" or country = "x"
If 0 is returned then it doesn't exist.
To check for column then:
select count(*) from tableName where city = "X"
If 0 is returned then it doesn't exist in city
select count(*) from tableName where country = "X"
If 0 is returned then it doesn't exist in country
Upvotes: 0
Reputation: 2774
SELECT
CASE
WHEN city = 'X'
THEN 'City'
ELSE 'Country'
END column_Value
FROM [table_Name]
WHERE city = 'X' OR country = 'X';
You can make use of the above query.
Upvotes: 1