I have a table with 99 columns and a rowid:
[RowID] [int] NOT NULL,
[Col1] [nvarchar](max) NULL,
[Col2] [nvarchar](max) NULL,
[Col3] [nvarchar](max) NULL,
[Col4] [nvarchar](max) NULL,
.......
[Col99] [nvarchar](max) NULL
I need a function that can be used in a Micrsoft SQL Server view (must be a view and no variables in the view) to search for a unique string and then display as a new field in the view. My current approach is using a case statement to search through each column until the unique string is found. This approach is cumbersome to write out (inefficient) and I have several unique strings to search for.
SELECT RowID, COALESCE (
CASE WHEN Col1 LIKE '%UniqueString%' THEN Col1 ELSE NULL END,
CASE WHEN Col2 LIKE '%UniqueString%' THEN Col2 ELSE NULL END,
CASE WHEN Col3 LIKE '%UniqueString%' THEN Col3 ELSE NULL END,
CASE WHEN Col4 LIKE '%UniqueString%' THEN Col4 ELSE NULL END,
CASE WHEN Col5 LIKE '%UniqueString%' THEN Col5 ELSE NULL END,
CASE WHEN Col6 LIKE '%UniqueString%' THEN Col6 ELSE NULL END,
………..
CASE WHEN Col99 LIKE '%UniqueString%' THEN Col99 ELSE NULL END) AS [UniequeString]
FROM dbo.TABLE_A
The end view should have the following result: