I need to make below situation. I need a.DATE_TIME field to find right column name in parentheses. Because there are columns for each days and I need calculate day from SHIFT_TABLE. Column names start with 'TPR' plus day like TPR13
DECLARE @SQLText NVARCHAR(MAX) = ''
SELECT @SQLText += 'SELECT (SELECT '
SELECT @SQLText += ( 'TPR' + Convert(varchar,DATEPART(day,a.DATE_TIME) )
) +
' from SHIFT_TABLE as s Where s.id = a.id ) from MAIN_TABLE as a where a.id=''1'' and a.ay=''12'' and a.yil=''2018'' '
print @SQLText
I take error message: The multi-part identifier "a.DATE_TIME" could not be bound.
SHIFT_TABLE occurs id and TPR01,TPR02,...TPR31 columns MAIN_TABLE occurs id, ay, yil and DATE_TIME columns
I have to calculate day from a.DATE_TIME.
As a result I try to reach like this, SELECT (SELECT TPR13 from SHIFT_TABLE as s Where s.id = a.id ) from MAIN_TABLE as a where a.id='1' and a.ay='12' and a.yil='2018'
How can I solve it? Many Thanks,
Convert(varchar,DATEPART(day,a.DATE_TIME) )function call. There are no tables involved here, which is why you get that error.CASE WHENclause in your SELECT to return different columns based on the day.from another tablethen?