0

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.

2 Answers 2

1
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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

Updated question. Please look again. Sorry for the inconvenience.
then specify that in the question.

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.