Table structure like this
select * from TimeTable;
userid | 1am|2am|3am| 4am| 5am
1002 | 1 |1 |1 | 1 | 1
1003 | 1 |1 |1 | 1 | 1
1004 | 1 |1 |1 | 1 | 1
1005 | 1 |1 |1 | 1 | 1
I want select users that have column value 1 of specific timecolumn I have used following query but it is throwing error
com.microsoft.sqlserver.jdbc.SQLServerException: Conversion failed when converting the varchar value '[ 3PM]' to data type int.
select * from UserDetail u,TimeTable t
where u.userid=t.userid
and CONCAT(SUBSTRING(CONVERT(VARCHAR, getdate(), 100), 13, 2) ,'',RIGHT(CONVERT(VARCHAR, GETDATE(), 100),2)) = 1
Like this when I use hardcoded column name then it works fine I want to select a column name dynamically.
select * from UserDetail u,TimeTable t
where u.userid = t.userid
and [3AM] = 1
JOINsyntax. Easier to write (without errors), easier to read (and maintain), and easier to convert to outer join if needed.