Checking multiple columns for one value

后端 未结 3 1093
名媛妹妹
名媛妹妹 2020-11-28 05:45

I have a table that has columns like this for example:

id,col1,col2,col3,col4

Now, I want to check if ANY of col1, col2,

3条回答
  •  囚心锁ツ
    2020-11-28 05:53

    You could do something like: (Note: Assuming the columns are numeric values. And, just incase the concatenated value creates the character sequence you are looking for, use a delimiter to distinguish the column values. Pipe (|) is the delimiter in this example.)

    SELECT [ID]
        ,[Col1]
        ,[Col2]
        ,[Col3]
        ,[Col4]
    FROM [Table1]
    WHERE '123' IN (
        CAST([Col1] AS VARCHAR) + '|'
        + CAST([Col2] AS VARCHAR) + '|'
        + CAST([Col3] AS VARCHAR) + '|'
        + CAST([Col4] AS VARCHAR) + '|'
    )
    

提交回复
热议问题