how do I create a sql 2005 server store procedure to return a summary count for certain fields data based on date range

Posted on 2009-04-16
Last Modified: 2012-05-06
expert -  The user will enter a time range say period 04/01/2009 to 04/15/2009. Based on that period I need to count up and list all existing "tom" "dick" and "harry" records with dates that fall in that  time range? I am curious how expert would do this. Could you please provide an SQL coded example for me to follow.
Question by:tcmmaxt
    LVL 11

    Assisted Solution

    by:N R
    select Namefield, count(*)
    from tablename
    where datefield >= cast('04-01-2009' as datetime)
    and datefield <= cast('04-15-2009' as datetime)
    group by Namefield
    LVL 39

    Accepted Solution

    To make Gallitin's code a stored procedure as requested.
    create procedure up_GetData 
       @StartDate datetime
      ,@endDate datetime
    set nocount on 
    --This line will turn <= April 15 2009, which will only work if all datetimes have NO TIME (ie. midnight)
    --  into the following day and uses a < operator instead of <=.  It is proper for inclusion of all
    --  records on april 15.
    set @enddate = dateadd(d, datediff(d, 0, @enddate) 1,0) 
    select Namefield, count(*) cnt
    from tablename
    where datefield >= @startdate
    and datefield < @enddate
    group by Namefield

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
    INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now