I’m a newbie in SQL server.
I’ve the following table:
id dayoftheweek timeoftheday
1 mondayOpen 0:00
1 mondayClose 23:59
1 tuesdayOpen 0:00
1 tuesdayClose 23:59
1 wednesdayOpen 0:00
1 wednesdayClose 23:59
1 thursdayOpen 0:00
1 thursdayClose 23:59
1 fridayOpen 0:00
1 fridayClose 23:59
1 saturdayOpen 0:00
1 saturdayClose 23:59
1 sundayOpen 0:00
1 sundayClose 23:59
I want the following:
id day open close
1 monday 0:00 23:59
2 tuesday 0:00 23:59
I don’t know if i need to write a sproc (T-SQL) or if there are any built-in keywords in SQL , using which i can get the output i desire. I cannot create a new table but views, sprocs are allowed in my existing schema.
Please help me.
TIA
The table structure there is horrid, I’d do anything you could to get away from it. That said mssql has a few substring functions your can use.
This will give you all records that are open. We’ll do the same thing to get all closed records in a sec. That will get by the challenge of reading open vs closed
Second issue to address is picking the ‘day’ column out of that text. Every day ends in y and contains no other ‘y”s in it, so we can use charindex
lil disclaimer…I don’t have a mssql db available for testing, so I might be a char off here and there.
That will return the location in the string of the ‘y’ character.
Mondayopen has a y on the 6th character
That would return the left 6 chars of mondayOpen or mondayClose. Put the two together:
Hope I got that right…it should turn mondayopen and mondayclose to monday. Put the two together:
This should now return the dayofweek without the ‘open’ or ‘close’ along with the open time. Turn it into a subquery, join it to the close subquery and put together:
Hope I don’t have syntax errors in there, nothing to test it on and might have a typo ^^