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 8322481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:24:10+00:00 2026-06-08T23:24:10+00:00

I am creating a scheduling application that allows employees to input their desired schedule

  • 0

I am creating a scheduling application that allows employees to input their desired schedule which is stored in a table. The current design I’m looking at is below using SQL 2008 R2.

CREATE TABLE [dbo].[Schedule] (
    [EmpNum] [varchar](10) NOT NULL,
    [Start] [datetime] NOT NULL,
    [Length] [decimal](18, 2) NOT NULL,
    [Reason] [varchar](1) NOT NULL,

    CONSTRAINT [PK_Schedule] PRIMARY KEY CLUSTERED 
    (
        [EmpNum] ASC,
        [Start] ASC
    )
)

A few things to note

  • A user may have entered no schedule for a specific date range and still want to see their name listed but with no schedule
  • A user may select nothing for a day in a week, but select something for other days
  • Start is the beginning of the shift
  • Length is the number of hours a shift is, may vary WILDLY from day to day and person to person
  • The Reason column is for what has been selected by the user (W – Work, P – PTO, etc)

Here is sample data

EmpNum     Start                   Length                                  Reason
---------- ----------------------- --------------------------------------- ------
000001     2012-08-02 09:00:00.000 12.00                                   W
000001     2012-08-04 08:00:00.000 9.50                                    P
000002     2012-08-02 08:30:00.000 10.00                                   W
000002     2012-08-03 19:00:00.000 12.00                                   W
000003     2012-08-03 08:00:00.000 8.00                                    P

The output I desire is something like this

EmpNum     [0]    [1]    [2]    [3]    [4]    [5]    [6]
---------- ------ ------ ------ ------ ------ ------ ------
000001     NULL   NULL   NULL   NULL   W      NULL   P
000002     NULL   NULL   NULL   NULL   W      W      NULL
000003     NULL   NULL   NULL   NULL   NULL   P      NULL

I’ve never used a PIVOT query before since we just upgraded from SQL 2000, so bear with me. I’ve constructed the below query which fails with the below error and I’m stuck.

Msg 102, Level 15, State 1, Line 14
Incorrect syntax near '('.

Query

declare @FirstDayOfWeek date
set @FirstDayOfWeek = '7/29/2012'

select EmpNum,
@FirstDayOfWeek [0],
dateadd(day, 1, @FirstDayOfWeek) [1],
dateadd(day, 2, @FirstDayOfWeek) [2],
dateadd(day, 3, @FirstDayOfWeek) [3],
dateadd(day, 4, @FirstDayOfWeek) [4],
dateadd(day, 5, @FirstDayOfWeek) [5],
dateadd(day, 6, @FirstDayOfWeek) [6]
from Schedule
pivot (
    max(Reason)
    for Start in ([0], [1], [2], [3], [4], [5], [6])
) as Pvt

Any thoughts on how to best implement this or how badly wrong I am here?

  • 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-06-08T23:24:13+00:00Added an answer on June 8, 2026 at 11:24 pm

    It looks like you are trying to PIVOT your data based on the Day of the Week 1-7. I suggest a slight change to this to get it to work:

    SELECT *
    FROM
    (
        select EmpNum,
            reason,
            datepart(dw, start) as DyOfWk
        from #Schedule
    ) s
    pivot (
        max(Reason)
        for dyofwk in ([1], [2], [3], [4], [5], [6], [7])
    ) as Pvt
    

    See SQL Fiddle with Demo

    Results:

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a table for an application that handles scheduling and deals with
We're creating a scheduling application and we need to represent someone's available schedule during
So I've been working on program in which I'm creating a hash table in
creating a landing page that sends an email when people submit their address. I'd
I am creating an application in which I have add a facility for post
Creating liquid layouts is an immense pain. Now, I totally understand that tables should
Creating a calculator-like dialog, I noticed that quickly clicking on a button in IE
Creating an installer for possible remote systems so that if they do not have
Creating a table without tbody using javascript createElement/appendChild will not add tbody in Firebug
Creating simple php login scripts is easy, with simple one table mysql integration. I

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.