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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:14:36+00:00 2026-05-23T07:14:36+00:00

Apologies for the terrible title, not really sure how to explain this without a

  • 0

Apologies for the terrible title, not really sure how to explain this without a sample:

I’m trying to return a column in a query that alternates between 0 and 1 as the hour of a datetime column over the result set changes. Does anybody have suggestions of a good way to do this?? OR how it could be done with CTEs

I started looking into using CTEs, but didn’t get very far and am hoping there’s an easier way. See below for a very basic example of what I’m hoping to achieve, with current output and the output I want. NOTE that I’ve created a column [band] that does what I want provided the times are all on the same day. Of course it breaks when then the dates cover more than one day.

declare @test as table(id int, dt datetime, comment varchar(50))

insert into @test values(1, '2011-01-01 07:00', 'one')
insert into @test values(2, '2011-01-01 07:30', 'two')
insert into @test values(3, '2011-01-02 07:50', 'three')
insert into @test values(4, '2011-01-03 08:00', 'four')
insert into @test values(5, '2011-01-03 08:50', 'five')
insert into @test values(6, '2011-01-03 09:00', 'six')
insert into @test values(7, '2011-01-03 10:00', 'seven');

select *, DATEPART(HOUR, dt) % 2 as [band]
from @test

CURRENT OUTPUT

1   2011-01-01 07:00:00.000 one     1
2   2011-01-01 07:30:00.000 two     1
3   2011-01-02 07:50:00.000 three   1
4   2011-01-03 08:00:00.000 four    0
5   2011-01-03 08:50:00.000 five    0
6   2011-01-03 09:00:00.000 six     1
7   2011-01-03 10:00:00.000 seven   0

REQUIRED OUTPUT

1   2011-01-01 07:00:00.000 one     1
2   2011-01-01 07:30:00.000 two     1
3   2011-01-02 07:50:00.000 three   0
4   2011-01-03 08:00:00.000 four    1
5   2011-01-03 08:50:00.000 five    1
6   2011-01-03 09:00:00.000 six     0
7   2011-01-03 10:00:00.000 seven   1

Note here that I want the [band] column to alternate as the datetime in the rows change to new hours. Whether they’re the next hour in the same day or another hour in the following day.

  • 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-23T07:14:36+00:00Added an answer on May 23, 2026 at 7:14 am

    How about?

    SELECT *, 
        (dense_rank() over (order by Dateadd(hh,Datediff(hh,0,dt),0))) % 2 as [band]
    FROM @test
    

    I don’t have sql at hand right now. But

    1. The dateadd/datediff cleans the minutes off the hours
    2. The dense_rank then orders them like rownumber but doesn’t number duplicates
    3. then the %2 does what you were doing in your original query.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Greetings, Apologies in advance that I have not researched this toughly enough to answer
Apologies - am not good at SQL. Perhaps this is a simple query -
I am really new in this so I hopefully don't make any terrible mistake.
Apologies for using someone else's brain for this, but I'm sure this is a
Apologies for the appalling title. I have mocked up this code to mimic an
(apologies if this is a duplicate ... i posted but saw no evidence that
Apologies if this question has been asked before. I'm hoping that someone can step
Apologies in advance for the long-winded question. I'm really a database programmer, but have
I am just getting started with IoC containers so apologies if this is a
I am just checking out F#, so apologies if this is a silly question,

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.