I have written the below query in SQL to get the result based on the days and hours. The days,start and endtime are stored in a table 'tblHeader' and the days are stored in 7 columns(Monday, Tuesday, Wendesday etc) and the start_time and end_time in another 2 columns in the same table. The table structure will be like
The days and hours values is dynamic. So when I run the below query I get results of all days, but I need only the day which is 'Y'.
Eg: If I run the query on Monday, then I need to get only the Monday data based on the dynamic time condition.
My Query is
select
-----
------
(CASE
WHEN
(SELECT DATENAME(dw, getdate())) LIKE 'Monday' THEN PriceH_strMonday WHEN
(SELECT DATENAME(dw, getdate())) LIKE 'Tuesday' THEN PriceH_strTuesday WHEN
(SELECT DATENAME(dw, getdate())) LIKE 'Wednesday' THEN PriceH_strWednesday WHEN
(SELECT DATENAME(dw, getdate())) LIKE 'Thursday' THEN PriceH_strThursday WHEN
(SELECT DATENAME(dw, getdate())) LIKE 'Friday' THEN PriceH_strFriday WHEN
(SELECT DATENAME(dw, getdate())) LIKE 'Saturday' THEN PriceH_strSaturday WHEN
(SELECT DATENAME(dw, getdate())) LIKE 'Sunday' THEN PriceH_strSunday END) as today
from tblPrice a
inner join
where
Today = 'Y'
and c.StartTime >= @mystarttime or c.EndTime <= @mystarttime
Now the Case statement result was displayed as a column named 'Today', when I include that column in the where condition, I faced an error stating Invalid column name 'Today'. How should I solve this?
Any help? Thanks in Anticipation.