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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:53:30+00:00 2026-06-11T15:53:30+00:00

Possible Duplicate: SQL Server: How to select all days in a date range even

  • 0

Possible Duplicate:
SQL Server: How to select all days in a date range even if no data exists for some days

I wasn’t really sure how to word this question, but I’ll try to explain. I’m trying to build some basic reporting using queries like the following:

SELECT COUNT(*) AS count, h_date FROM (SELECT CONVERT(VARCHAR(10), h_time, 102) AS h_date FROM hits h GROUP BY h_date ORDER BY h_date

This returns results like this, which I use to build a graph:

8 2012.05.06
2 2012.05.07
9 2012.05.09

As you can see, it’s missing the 8th as there were no hits on that day. Is there a way to get a value of 0 for dates that have no results, or will I have to parse the results after the fact and add them manually?

  • 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-11T15:53:31+00:00Added an answer on June 11, 2026 at 3:53 pm

    You can use existing catalog views to derive a sequential range of dates between your start date and your end date. Then you can just left join to your data, and any missing dates will be there with 0s.

    DECLARE @min SMALLDATETIME, @max SMALLDATETIME;
    
    SELECT @min = MIN(h_time), @max = MAX(h_time)
      FROM dbo.hits 
      -- WHERE ?
    
    -- or if you just want a fixed range:
    
    -- SELECT @min = '20120101', @max = '20120131';
    
    ;WITH n(d) AS 
    (
      SELECT TOP (DATEDIFF(DAY, @min, @max)+1) 
       DATEADD(DAY, ROW_NUMBER() OVER (ORDER BY [object_id]) - 1, DATEDIFF(DAY, 0, @min))
      FROM sys.all_objects ORDER BY [object_id]
    )
    SELECT n.d, [count] = COUNT(h.h_time)
      FROM n
      LEFT OUTER JOIN dbo.hits AS h
      ON h.h_time >= n.d
      AND h.h_time < DATEADD(DAY, 1, n.d)
      -- AND --WHERE clause against hits?
      GROUP BY n.d;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Get script of SQL Server data I want to move some rows
Possible Duplicate: Selecting data from two different servers in SQL Server How can I
Possible Duplicate: SQL Server: Is there an “IF EXISTS” test for a linked server?
Possible Duplicate: Does SQL Server 2005 have an equivalent to MySql’s ENUM data type?
Possible Duplicate: SQL Server 2008 Generate a Series of date times I have to
Possible Duplicate: Select n random rows from SQL Server table I have database and
Possible Duplicate: SQL Server - use a parameter to select the top X of
Possible Duplicate: SQL: Using Select * Hi everyone! I wonder if it is really
Possible Duplicate: How to delete all rows from all tables in a SQL Server
Possible Duplicate: SQL Server 2008 Spatial: find a point in polygon I am working

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.