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

  • Home
  • SEARCH
  • 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 6729567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:18:20+00:00 2026-05-26T10:18:20+00:00

I have two datetime columns in a table, and I would like to select

  • 0

I have two datetime columns in a table, and I would like to select precisely those rows whose difference from the first column to the second column is one second short of one month. For example, it would select these rows (and no others) for datetimes in the year 2011:

col1                col2
2011-01-01 00:00:00 2011-01-31 23:59:59
2011-02-01 00:00:00 2011-02-28 23:59:59
2011-03-01 00:00:00 2011-03-31 23:59:59
2011-04-01 00:00:00 2011-04-30 23:59:59
2011-05-01 00:00:00 2011-05-31 23:59:59
2011-06-01 00:00:00 2011-06-30 23:59:59
2011-07-01 00:00:00 2011-07-31 23:59:59
2011-08-01 00:00:00 2011-08-31 23:59:59
2011-09-01 00:00:00 2011-09-30 23:59:59
2011-10-01 00:00:00 2011-10-31 23:59:59
2011-11-01 00:00:00 2011-11-30 23:59:59
2011-12-01 00:00:00 2011-12-31 23:59:59

However, selecting for 2012, for example, would have to account for the leap year.

How would I write such a statement?

  • 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-26T10:18:20+00:00Added an answer on May 26, 2026 at 10:18 am
    SELECT *
    FROM Table
    WHERE col2 = DATEADD(second, -1, (DATEADD(Month, 1 Col1)))
    -- check for either direction
    OR col1 = DATEADD(second, -1, (DATEADD(Month, 1 Col2)))
    

    This says, “Show me rows where column2 is (one month more and one second less) than col1.”

    The built in date logic in SQL Server automatically accounts for things like leap years.

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

Sidebar

Related Questions

If I have a table that (among other columns) has two DATETIME columns, how
I have two identical tables and need to copy rows from table to another.
I have a table called Sessions with two datetime columns: start and end. For
I have two tables, both with the same columns. The first table contains original
I have a view like this: CREATE VIEW MyView AS SELECT Column FROM Table
I have a table with two DATETIME columns: Column1 Column2 1/1/1900 12:20:45 1/1/1900 23:22:25
I have a visit table with columns member_id visit_id visit_logout_datetime(values like this... ' now
Say I have an MSSQL table with two columns: an int ID column that's
I would like to create a function for a table that has three columns
I have a table with many two columns: id, mydate Id is a number;

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.