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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:54:31+00:00 2026-05-13T05:54:31+00:00

I have this SQL now: CREATE PROCEDURE dbo.sel_Track_HitsLast30Days( @projectID int ) AS BEGIN DECLARE

  • 0

I have this SQL now:

CREATE PROCEDURE dbo.sel_Track_HitsLast30Days(
@projectID int
)
AS
BEGIN
 DECLARE @FirstDay smalldatetime, @NumberOfMonths int, @priorMonth smalldatetime
set @priorMonth = (SELECT CAST(
(
STR( YEAR( dateadd(m,-1, getDate()) ) ) + '/' +
STR( MONTH( dateadd(m,-1, getDate()) ) ) + '/' +
STR( DAY( dateadd(m,-1, getDate()) ) )
)
AS DateTime 
))
Select @FirstDay = @priorMonth, @NumberOfMonths = 1
;WITH Days AS (
SELECT @FirstDay as CalendarDay
UNION ALL
SELECT DATEADD(d, 1, CalendarDay) as CalendarDay
FROM Days
WHERE DATEADD(d, 1, CalendarDay) < DATEADD(m, @NumberOfMonths, @FirstDay+1)
)
SELECT calendarday,foundDate.TotalbyDate,foundDate.date FROM Days

LEFT OUTER JOIN (

SELECT
COUNT(LEFT(visitDateTime, 11)) AS TotalbyDate,substring(convert( char(10), CONVERT( char(10), visitDateTime, 121 ) ), 1, 11) AS date

FROM
  dbo.TrackingData
WHERE
  visitDateTime >= dateadd(d, datediff(d, 0, getdate()), -30) and projectID = @projectID
  GROUP BY substring(convert( char(10), CONVERT( char(10), visitDateTime, 121 ) ), 1, 11)


) foundDate on foundDate.date = CalendarDay

order by 
CalendarDay Desc
END

This works ok, but It is not taking into account months with 31 days and I am not getting back today’s date for some reason.

  • 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-05-13T05:54:31+00:00Added an answer on May 13, 2026 at 5:54 am

    OK, I got it. Rubens gave me an idea so I modified my SQL like so:

    DECLARE  @FirstDay       SMALLDATETIME,
             @NumberOfMonths INT,
             @priorMonth     SMALLDATETIME,
             @firstDayLastMonth DateTime,
             @daysCount int
    set @firstDayLastMonth = dateadd(m, -1,      dateadd(d, -day(getDate()) + 1, getDate()))
    set @daysCount = (select datepart(dd,dateadd(dd,-1,dateadd(mm,1,cast(cast(year(@firstDayLastMonth) as varchar)+'-'+cast(month(@firstDayLastMonth) as varchar)+'-01' as datetime)))))
    SET @priorMonth = (SELECT Cast((Str(Year(Dateadd(m,-1,Getdate()))) + '/' + Str(Month(Dateadd(m,-1,Getdate()))) + '/' + Str(Day(Dateadd(m,-1,Getdate())))) AS DATETIME))
    
    Declare @before dateTime
    Set @before = Dateadd(d,-@daysCount,getdate())
    SELECT @FirstDay = @before,
           @NumberOfMonths = 1;
    
    WITH days
         AS (SELECT @FirstDay AS calendarday
             UNION ALL
             SELECT Dateadd(d,1,calendarday) AS calendarday
             FROM   days
             WHERE  Dateadd(d,1,calendarday) <= Dateadd(m,@NumberOfMonths,@FirstDay))
    
     SELECT   Substring(Convert(CHAR(10),Convert(CHAR(10),calendarday,101)),
                                                 1,11) ,
             founddate.totalbydate,
             founddate.DATE
    FROM     days  
     LEFT OUTER JOIN (SELECT   Count(Left(visitdatetime,11)) AS totalbydate,
                                       Substring(Convert(CHAR(10),Convert(CHAR(10),visitdatetime,101)),
                                                 1,11) AS DATE
                              FROM     dbo.trackingdata
                              WHERE    visitdatetime >= Dateadd(d,Datediff(d,0,Getdate()),-29)
                                       AND projectid = 131
                              GROUP BY Substring(Convert(CHAR(10),Convert(CHAR(10),visitdatetime,101)),
                                                 1,11)) founddate
               ON founddate.DATE = Substring(Convert(CHAR(10),Convert(CHAR(10),calendarday,101)),
                                                 1,11)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 298k
  • Answers 298k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I'd use self-types for dependency-management: This trait requires another trait… May 13, 2026 at 7:28 pm
  • Editorial Team
    Editorial Team added an answer assuming you are using jQuery something like this might work.… May 13, 2026 at 7:28 pm
  • Editorial Team
    Editorial Team added an answer What you need is MEF (which will be included in… May 13, 2026 at 7:28 pm

Related Questions

can anyone help me with construction of an IF in a stored procedure in
I'm struggling with a T-SQL procedure and I am hoping you can help. I
I looked at the SQLTeam website but now am having a new problem cause
I am currently developing on an advertising system, which have been running just fine
I'm securing the DB by only allowing interaction with the DB through a series

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.