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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:56:10+00:00 2026-06-13T17:56:10+00:00

I have a SQL statement similar to: SELECT DATEDIFF(Day, startDate, endDate) FROM Data WHERE

  • 0

I have a SQL statement similar to:

SELECT DATEDIFF(Day, startDate, endDate) FROM Data WHERE ProjectId=@id

In the case where Data doesn’t have any records for ProjectId, SQL Server returns null.

In Dapper, I execute this via:

value = conn.Query<int>("...").SingleOrDefault()

In this case, I expect the semantics of SingleOrDefault to mean “if this is null, return zero.” In fact, my code is even more zero-friendly:

int toReturn = 0;
using (var conn = ...) {
  toReturn = conn.Query<int>("SELECT DATEDIFF(...))");
}
return toReturn;

When I debug and step into this code, I find that the line yield return (T)func(reader) is throwing a null pointer exception.

Am I doing something wrong here, or is this by design?

(FYI, the work-around is to wrap my select in an ISNULL(..., 0))

  • 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-13T17:56:11+00:00Added an answer on June 13, 2026 at 5:56 pm

    In the case where Data doesn’t have any records for ProjectId, SQL Server returns null.

    In the case where Data doesn’t have any matching records, SQL server does not really return null – it returns no rows. This scenario works fine:

    var result = connection.Query<int>( // case with rows
     "select DATEDIFF(day, GETUTCDATE(), @date)", new { date = DateTime.UtcNow.AddDays(20) })
     .SingleOrDefault();
    result.IsEqualTo(20);
    
    result = connection.Query<int>( // case without rows
        "select DATEDIFF(day, GETUTCDATE(), @date) where 1 = 0", new { date = DateTime.UtcNow.AddDays(20) })
        .SingleOrDefault();
    result.IsEqualTo(0); // zero rows; default of int over zero rows is zero
    

    both of which work fine. The fact that you say ISNULL “fixes” it means that you are talking about a different scenario – the “I returned rows” scenario. Since that is the case, what your code is saying is “take this 1-or-more integers which contains a null, and map it as a non-nullable int” – that isn’t possible, and the mapper is correct to throw an exception. Instead, what you want is:

    int? result = connection.Query<int?>(...).SingleOrDefault();
    

    Now, if there are rows, it is mapping the value to int?, and then applying the single-or-default. It you want that as an int, then maybe:

    int result = connection.Query<int?>(...).SingleOrDefault() ?? 0;
    

    If you need to be able to tell the difference between “zero rows” and “null result”, then I would suggest:

    class NameMe {
        public int? Value {get;set;}
    }
    var row = connection.Query<NameMe>("select ... as [Value] ...", ...)
                        .SingleOrDefault();
    if(row == null) {
       // no rows
    } else if(row.Value == null) {
       // one row, null value
    } else {
       // one row, non-null value
    }
    

    or something similar

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

Sidebar

Related Questions

I have sql statement one; select linkscat.id, linkscat.category from linkscat, contentlinks, links where contentlinks.linksid
I have this SQL statement: SELECT * FROM history WHERE (fk_person = 2119) AND
I have a SQL statement like the following: select A from table1, (select B
I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0;
Okay, so I have this T-SQL statement: SELECT COUNT(DISTINCT RentalNo) as Count FROM RoomRental
I have a SQL statement similar to this: SELECT COUNT(*) AS foo, SUM(foo) AS
I have a SQL Statement where i need to display the value from another
I'm writing code using Oracle SQL Developer. I have a simple select statement that
I have a query with SELECT CASE statement that's working fine: SELECT (CASE `t`.`is_combined`
I have a sql statement that concatenates two columns, ItemNumber and Name, into one

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.