I want to get the column from the dynamic string, In my table: MyTable2 I have many columns/fields, which are : MyVarCharColumnName1,MyVarCharColumnName2,MyVarCharColumnName3,MyVarCharColumnName4 These above fields are nvarchar(50), I wonder how I can get the value of them when join with another table as below example:
SELECT
*,
CASE
WHEN Tbl1.MyId IN ('1', '2', '3', '4')
THEN (SELECT 'MyVarCharColumnName' + bf.MyId
FROM MyTable2)
ELSE Tbl1.MyId
END AS TheNameofId
FROM
Tbl1
above is wrong because it will return MyVarCharColumnName1 as TheNameofId value instead of the value inside MyVarCharColumnName1
Is there are some sql function that can return the column name from a string? any way to convert 'MyVarCharColumnName1' to column object?