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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:53:48+00:00 2026-06-05T14:53:48+00:00

Using SQL Server 2005. Data is in 2 separate tables and I have only

  • 0

Using SQL Server 2005.

Data is in 2 separate tables and I have only been given write permissions.

Data looks like:

DateTime1  |  DateTime2
-----------------------
2012-06-01 | 2012-06-01
2012-06-02 | 2012-06-02
2012-06-04 | 2012-06-05
2012-06-02 | NULL
NULL       | 2012-06-05
2012-06-04 | 2012-06-05
NULL       | NULL

What I am trying to do is be able to count values in which DateTime1 and DateTime2 contain values, DateTime1 contains a date and DateTime2 is NULL, DateTime1 is NULL and DateTime2 contains values.

Overall Im trying to avoid DateTime1 being Null and DateTime2 being null.

My where statement looks like this:

Where (DateTime1 is not null or DateTime2 is not null)

The only problem is it is still showing where both are null values. Anyone know why this might be happening or how to solve it?

Thanks

EDIT
Full Query as requested by @Lamak

;With [CTE] As (
Select
    TH.ID
    ,AMT
    ,Reason
    ,EffDate
    ,DateReq
    ,CS_ID
    ,ROW_NUMBER()
        Over (Partition By ID Order By [pthPrimeKey] Desc) as [RN]
From
    DateTime1Table as [MC] (nolock)
    Left Join History as [TH] (nolock) on [TH].[ID] = [MC].[ID]
    Left Join Trans as [SUB] (nolock) on [SUB].TransactionReasonCode = [TH].Reason
    Left Join Renew as [RM] (nolock) on [MC].ID = [RM].ID
Where 
    ([MC].[DateTime1] is not null or [RM].[DateTime2] is not null)
    And [PostingDate] = DATEADD(dd, datediff(dd, 1, GetDate()),0)
)
SELECT 
[ID]
,[AMT] as [Earned]
,[Reason] as [Reason]
,[EffDate] as [Eff]
,[DateReq] as [Date_Cancel_Req]
,[pthUserId_Number] as [CSR]
FROM [CTE]
Where RN <= 1
  • 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-05T14:53:49+00:00Added an answer on June 5, 2026 at 2:53 pm

    The following will allow rows to be included if

    • only DateTime1 has a value
    • only DateTime2 has a value
    • both have values

    It will exclude rows where both values are NULL. Is that what you’re after? (I tried to follow the conversations but got lost, and wish you’d have a simpler repro with sample data – I think the CTE and all the other joins and logic really take away from the actual problem you’re having.)

    WHERE COALESCE([MC].[DateTime1], [RM].[DateTime2]) IS NOT NULL
    

    However, since you’re performing a LEFT OUTER JOIN, this may belong in the ON clause for [RM] instead of WHERE. Otherwise you won’t know if a row is excluded because the value in a matching row was NULL, or because there was no matching row. And maybe that’s ok, just thought I would mention it.

    EDIT

    Of course, that clause provides the exact same results as …

    WHERE ([MC].[DateTime1] is not null or [RM].[DateTime2] is not null)
    

    Want proof?

    DECLARE @a TABLE(id INT, DateTime1 DATETIME);
    DECLARE @b TABLE(id INT, DateTime2 DATETIME);
    
    INSERT @a SELECT 1, '20120602' ; INSERT @b SELECT 1, NULL;
    INSERT @a SELECT 2, NULL       ; INSERT @b SELECT 2, '20120605';
    INSERT @a SELECT 3, '20120604' ; INSERT @b SELECT 3, '20120605';
    INSERT @a SELECT 4, NULL       ; INSERT @b SELECT 4, NULL;
    INSERT @a SELECT 5, '20120602' ; INSERT @b SELECT 9, NULL;
    INSERT @a SELECT 6, NULL       ; INSERT @b SELECT 10, '20120605';
    INSERT @a SELECT 7, '20120604' ; INSERT @b SELECT 11, '20120605';
    INSERT @a SELECT 8, NULL       ; INSERT @b SELECT 12, NULL;
    
    SELECT * FROM @a AS a LEFT OUTER JOIN @b AS b
    ON a.id = b.id
    WHERE COALESCE(a.DateTime1, b.DateTime2) IS NOT NULL;
    
    SELECT * FROM @a AS a LEFT OUTER JOIN @b AS b
    ON a.id = b.id
    WHERE a.DateTime1 IS NOT NULL OR b.DateTime2 IS NOT NULL;
    

    Both queries yield:

    id DateTime1  id   DateTime2
    -- ---------- ---- ----------
    1  2012-06-02 1    NULL       -- because left is not null
    2  NULL       2    2012-06-05 -- because right is not null
    3  2012-06-04 3    2012-06-05 -- because neither is null
    5  2012-06-02 NULL NULL       -- because of no match
    7  2012-06-04 NULL NULL       -- because of no match
    

    So as I suggested in the comment, if you’re not seeing the rows you expect, you need to look at other parts of the query. If you provide sample data and desired results, we can try to help you narrow that down. As it is, I don’t think we know enough about your schema and data to determine where the problem is.

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

Sidebar

Related Questions

I'm using SQL Server (2005). Today I have some tables that everytime the data
i have using aqua data studio to run my query from sql server 2005.
I am using SQL server 2005. In one of the tables, I have a
The question is about Armenian. I'm using sql server 2005, collation SQL_Latin1_General_CP1_CI_AS, data mostly
I'm using SSIS to import data from Excel sheets into SQL Server 2005, the
[using SQL Server 2005] I have a table full of users, I want to
I´m using SQL Server 2005 and Visual Studio 2008, C#. In the data source
I've created a web enabled database using SQL Server 2005 to store the data
I am using sql server 2005 I have a table [say tblHistory] and this
How can we handle ascii characters using sql server 2005? Which data type can

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.