Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7182485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:46:50+00:00 2026-05-28T17:46:50+00:00

I have an application that has several stores , and each store has several

  • 0

I have an application that has several stores, and each store has several records in store_hours.

The store_hours table looks like this:

store_id [int]
day [varchar(20)]
opening_time [varchar(20)]
closing_time [varchar(20)]

and the records for one store may look like this:

301, 'Monday', '7am', '8:30pm'
301, 'Tuesday', '7am', '8:30pm'
301, 'Wednesday', '7am', '8:30pm'
301, 'Thursday', '7am', '8:30pm'
301, 'Friday', '7am', '10pm'
301, 'Saturday', 'closed,' 'closed'
301, 'Sunday', 'closed,' 'closed'

We’re trying to make an “hours summary” column on the store table. The above query would result in something like ‘M: 7am-8:30pm, T: 7am-8:30pm, W: 7am-8:30pm, T: 7am-8:30pm, F: 7am-8:30pm, S: closed, S: closed’

I’m populating it by running a query similar to this:

UPDATE s
SET hours_summary = 
'M: ' + (SELECT TOP 1 ch.opening_time FROM store_hours ch WHERE c.id = ch.store AND [day] = 'Monday') + '-' + (SELECT TOP 1 ch.closing_time FROM store_hours ch WHERE c.id = ch.store AND [day] = 'Monday') + ', ' +
'T: ' + (SELECT TOP 1 ch.opening_time FROM store_hours ch WHERE c.id = ch.store AND [day] = 'Tuesday') + '-' + (SELECT TOP 1 ch.closing_time FROM store_hours ch WHERE c.id = ch.store AND [day] = 'Tuesday') + ', '
    (etc, for the other five days)
FROM stores s

Now, this will work (more or less), but I have a couple questions.

  1. This is ugly, in my opinion, and seems to have a lot of repetion. Is there anyway you can think of to improve it?
  2. For some records, if the store is closed, there is no record at all in the store_hours table. I haven’t come up with a good way to handle this. Suggestions?

EDIT

Several people have recommended using a pivot table. I see how I can get the opening or closing time in there, but I don’t see how I can do both, and I don’t see how I can summarize it into one query.

This is what I have so far:

SELECT centre, [Monday], [Tuesday], [Wednesday], [Thursday], [Friday], [Saturday], [Sunday]
FROM (SELECT centre, [day], opening_time FROM centre_hours) AS p
PIVOT (MAX(opening_time) FOR [day] IN ([Monday], [Tuesday], [Wednesday], [Thursday], [Friday], [Saturday], [Sunday])) AS pt

EDIT AGAIN

For those of you from the future wondering if/how I got around this, I ended up creating two separate pivot tables, then INSERTing the results of the first table, and UPDATEing with the results from the second.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:46:52+00:00Added an answer on May 28, 2026 at 5:46 pm

    Maybe this will help you. No update or insert. The same table as you original post

    DECLARE @store_hours TABLE
    (
        store_id int,
        [day] varchar(20),
        opening_time varchar(20),
        closing_time varchar(20)
    )
    
    INSERT INTO @store_hours(store_id,[day],opening_time,closing_time)VALUES(301, 'Monday', '7am', '8:30pm')
    INSERT INTO @store_hours(store_id,[day],opening_time,closing_time)VALUES(301, 'Tuesday', '7am', '8:30pm')
    INSERT INTO @store_hours(store_id,[day],opening_time,closing_time)VALUES(301, 'Wednesday', '7am', '8:30pm')
    INSERT INTO @store_hours(store_id,[day],opening_time,closing_time)VALUES(301, 'Thursday', '7am', '8:30pm')
    INSERT INTO @store_hours(store_id,[day],opening_time,closing_time)VALUES(301, 'Friday', '7am', '10pm')
    INSERT INTO @store_hours(store_id,[day],opening_time,closing_time)VALUES(301, 'Saturday', 'closed', 'closed')
    INSERT INTO @store_hours(store_id,[day],opening_time,closing_time)VALUES(301, 'Sunday', 'closed' ,'closed')
    
    SELECT
        *
    FROM
    (
        SELECT 
            [day], 
            opening_time+'-'+closing_time AS Times
        FROM @store_hours AS store_hours
    ) AS SourceTable
    PIVOT
        (
            MAX(Times)
            FOR [day] IN ([Monday], [Tuesday], [Wednesday], [Thursday], [Friday], [Saturday],[Sunday])
        )AS PivotTable;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Silverlight 4 out-of-browser application with a ScrollViewer that has several RichTextBoxes
In my application I have several activities, the main screen has 4 buttons that
I have an application that has a status table see Database best practices -
I'm working on an Android application that has several Activities. In it I have
I have a Win32 GUI application that has several edit controls (plain old EDIT
I have an application that has two threads. The first one (the main thread)
i have an application that has a dependancy on gdiplus. i need the application
I have an application that has a list of buttons and has customized tooltips.
I have an application that has a .sql file in it. The sql file
We have an application that has a WCF service (*.svc) running on IIS7 and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.