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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:49:19+00:00 2026-06-06T15:49:19+00:00

If I insert the following values into my table Insert into Table1 (Field1, Field2,

  • 0

If I insert the following values into my table

Insert into Table1 (Field1, Field2, Field3, Field4, DateField) values (1, 1, 100, 5, "5/10/2012")
Insert into Table1 (Field1, Field2, Field3, Field4, DateField) values (1, 2, 100, 99, "5/10/2012")
Insert into Table1 (Field1, Field2, Field3, Field4, DateField) values (1, 3, 100, 3, "5/10/2012")

What my query is going to try and get is the value from Field4 with the maximum value in Field2 by a certain date.

Example

select isnull(Field4,0) from Table1 
where Field1 = 1 and Field3 = 100 and datediff(day,DateField,"5/21/2012") > 0
having Max(Field2) = Field2

Which works great. I get 3 which is expected. Now this is where my questions comes from. It is possible Field3 could have other values, such as 110. When I run that query

select isnull(Field4,0) from Table1 
where Field1 = 1 and Field3 = 110 and datediff(day,DateField,"5/21/2012") > 0
having Max(Field2) = Field2

I don’t get any results. It should be null, and the isnull(Field4,0) should then spit out 0. But it doesn’t. I’ve tried replacing the selection with count(*) to see if it returns 0, but it doesn’t return anything. I’m at a loss. I need it to return 0 as this will be going into a temp table and then summed up with values from another table. Thanks.

Edit – New question part
I understand that I may have been using the isnull for the wrong thing here. Which I can accept. However, if I wanted to write a case statement to handle nothing being returned, it doesn’t return 0 if no rows are returned.

select count(*) from Table1 
where Field1 = 1 and Field3 = 110 and datediff(day,DateField,"5/21/2012") > 0
having Max(Field2) = Field2

The above code doesn’t return anything, instead of 0 like I would think it would.

  • 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-06T15:49:22+00:00Added an answer on June 6, 2026 at 3:49 pm

    You do not get any results because none of the 3 records you inserted has 110 for Field3. As a result, no rows are returned from the query. Your ISNULL would only be used if instead of 5, 99, or 3, those values were NULL for a row returned.

    If this record was in your table:

    INSERT INTO Table1 (Field1, Field2, Field3, Field4, DateField)
    VALUES(1, 3, 110, NULL, "5/10/2012")
    

    This record would match your Field3 = 110 requirement, and then have Field4 Null, being set to 0 by your SELECT logic, but since it is not, nothing will be returned by your query.

    Part 2

    It appears that the HAVING is removing the 0 record from the result set since no records are left after the WHERE clause to match against the HAVING criteria.

    If you want to see whether any results match your criteria, you can use the EXISTS clause

    IF EXISTS(
       SELECT COUNT(*)
       FROM Table1 AS t 
       WHERE t.Field1 = 1
          AND t.Field3 = 110 
          AND DATEDIFF(day, t.DateField, "5/21/2012") > 0
       HAVING MAX(Field2) = Field2
    )
    BEGIN
       --Code for when the record Exists
    END
    ELSE
    BEGIN
       --No records logic here
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've the following query: INSERT INTO table ( memberid, field1, field2, field3 ) VALUES
I have the following query: INSERT INTO table (a, b, c) VALUES (NOW(), NOW()
I have the following query in VBA: DoCmd.RunSQL INSERT INTO table (value) VALUES ('example')
I have the following code: <? $query =$db->prepare(INSERT INTO a_table (id, a_field) VALUES ('',
I have the following Python code: cursor.execute("INSERT INTO table VALUES var1, var2, var3,") where
I'm using the following query: INSERT INTO role (name, created) VALUES ('Content Coordinator', GETDATE()),
The following query was successful when I used in mysql INSERT INTO user(`dev_id`,`email`) VALUES('123','456@gmail.com');
this query works fine using the php_mssql driver: INSERT INTO Table(columnName) VALUES ('text'); SELECT
I have the following code: $sql = INSERT INTO table VALUES ('', ...); $result
I have the following PostgreSQL query: INSERT INTO persoane_alocate(pa_id_pj,pa_id_corespondent_persoana,pa_procentaj,pa_tip_persoana,pa_data_inceput,pa_data_sfarsit,pa_afisare_in_monitor,audit_data_modificarii,audit_id_utilizator) VALUES ('670', '534', '0', '1',

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.