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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:27:23+00:00 2026-06-11T13:27:23+00:00

I asked a question for a complex query yesterday, and I was offered an

  • 0

I asked a question for a complex query yesterday, and I was offered an example. I really would like to get it to work but there was a syntax error in it that I can’t figure out. Keep in mind that I was just introduced to CTE’s earlier this week so hopefully this is an easy one.

I don’t think I need to post the full code on here, so I’ll just summarize

 with cte as (select dateadd(hour, 1, cast(cast(getdate() -1 as date) as datetime)) as midnnight),
 allhours as (
 select 0 as hour, midnight as timestart, dateadd(hour, 1, timestart) as timeend from cte union all
 select 1 as hour, dateadd(hour, 1, midnight), dateadd(hour, 2, midnight) from cte union all
 ....
 select 23 as hour, dateadd(hour, 23, midnight), dateadd(hour, 24, midnight) from cte union all
) 
select ah.hour,...

The (…) denotes unnecessary code that I omitted to make it less messy

But I am getting a syntax error on the parenthesis between select 23 and select ah.hour
“Incorrect syntax near ‘)’. Expecting SELECT, or ‘(‘.

Any help is greatly appreciated.

-J

  • 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-11T13:27:24+00:00Added an answer on June 11, 2026 at 1:27 pm

    You had a few syntax errors including a UNION ALL that was not needed at the bottom, and your first SELECT in the allhours was referencing an alias, so try this:

    ;with cte as 
    (
      select dateadd(hour, 1, cast(cast(getdate() -1 as date) as datetime)) as midnight
    ),
    allhours as 
    (
      select 0 as hour, midnight as timestart, dateadd(hour, 1, midnight) as timeend 
      from cte 
      union all
      select 1 as hour, dateadd(hour, 1, midnight), dateadd(hour, 2, midnight) 
      from cte 
      union all
      select 23 as hour, dateadd(hour, 23, midnight), dateadd(hour, 24, midnight) 
      from cte
    ) 
    select *
    from allhours
    

    see SQL Fiddle with Demo

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

Sidebar

Related Questions

I asked this question earlier: How to get delta between two text items But
I asked this question previously but i think i made it too complex, I
I asked this question over Security site, and people there suggested I should have
I asked this question earlier. I am intrigued by std::set but I have another
A similar question has been asked, but since it always depends, I'm asking for
I know this question has been asked here before, but I don't think those
So the other day, I asked this question about how to combine three complex
I asked question before asking if it is possible to save complex class composition
I know this question is asked many times but I just want to clear
I've just asked question about Task but realized that I actually want to ask

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.