sql - use mysql dayname as a column name -


is possible use dayname output columname

currently using this

select *, left(thu,5) open1 ... ... 

but replace thu string dynamic current day(3 letters) , still use column name.

the short answer no, can't done. column references (like identifiers) in sql statement static. identifiers can't replaced expressions determined @ runtime.


the longer answer either need dynamically generate statement, or choose 1 of set of possible statements execute, based on whatever conditions.

a third approach might want, referencing possible column names want select from, , making return column conditional based on day of week.

something achieves same result:

select left(case dayname(now())             when 'sunday'    t.sun             when 'monday'    t.mon             when 'tuesday'   t.tue             when 'wednesday' t.wed             when 'thursday'  t.thu             when 'friday'    t.fri             when 'saturday'  t.sat             end,5) open1   t 

(to avoid issues different settings day of week names, based on language/region, i'd prefer use dayofweek function instead. , i'd more repeat left function, if repeated, seems easier read:

select case dayofweek(now())          when 1 left(t.sun,5)          when 2 left(t.mon,5)          when 3 left(t.tue,5)          when 4 left(t.wed,5)          when 5 left(t.thu,5)          when 6 left(t.fri,5)          when 7 left(t.sat,5)        end open1   t 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -