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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:34:13+00:00 2026-06-03T02:34:13+00:00

I trying to rewrite a stored procedure and my SQL is not very good.

  • 0

I trying to rewrite a stored procedure and my SQL is not very good. What i’m hoping to do is write it so that if ModuleID is 555 then select a custom date range (eg. 2012-01-01 2012-12-31). The Current SP is below.

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO


/*** EventsGetByRange ***/

ALTER PROCEDURE [dbo].[EventsGetByRange]
(
 @Filter nvarchar(500),
 @BeginDate datetime,
 @EndDate datetime
)
AS
SET DATEFORMAT mdy
Declare @sql nvarchar(4000)
Select @sql = 'SELECT E.PortalID, E.EventID, E.RecurMasterID, E.ModuleID, E.EventDateBegin, E.EventDateEnd, '
 + 'E.EventTimeBegin, E.Duration, E.EventName, E.EventDesc, '
 + 'E.Importance, E.CreatedDate,  '
 + 'CreatedBy = U.DisplayName, '
 + 'CreatorID =  E.CreatedBy, '
 + 'E.Every, '
 + 'E.Period, '
 + 'E.RepeatType, '
 + 'E.Notify, '
 + 'E.approved, '
 + 'E.Signups, '
 + 'E.MaxEnrollment, '
 + '(Select count(*) from dbo.EventsSignups WHERE EventID = E.EventID and E.Signups = 1) as Enrolled, '
 + 'E.EnrollRoleID, '
 + 'E.EnrollFee, '
 + 'E.EnrollType, '
 + 'E.PayPalAccount, '
 + 'E.PayPalPassword, '
 + 'E.Cancelled, '
 + 'E.DetailPage, '
 + 'E.DetailNewWin, '
 + 'E.DetailURL, '
 + 'E.ImageURL, '
 + 'E.ImageType, '
 + 'E.ImageWidth, '
 + 'E.ImageHeight, '
 + 'E.ImageDisplay, '
 + 'E.Location, '
 + 'c.LocationName, '
 + 'c.MapURL, '
 + 'E.Category, '
 + 'b.CategoryName, '
 + 'b.Color, '
 + 'b.FontColor, '
 + 'E.Reminder, '
 + 'E.TimezoneOffset, '
 + 'E.SendReminder, '
 + 'E.ReminderTime, '
 + 'E.ReminderTimeMeasurement, '
 + 'E.ReminderFrom, '
 + 'E.SearchSubmitted, '
 + 'E.CustomField1, '
 + 'E.CustomField2, '
 + 'E.EnrollListView, '
 + 'E.DisplayEndDate, '
 + 'E.AllDayEvent, '
 + 'E.OwnerID, '
 + 'OwnerName = O.DisplayName, '
 + 'E.LastUpdatedAt, '
 + 'LastUpdatedBy = L.DisplayName, '
 + 'E.LastUpdatedID, '
 + '(Select ModuleTitle from dbo.Modules WHERE ModuleID = E.ModuleID) as ModuleTitle, '
 + 'RMOwnerID = r.OwnerID, '
 + 'r.RRULE, '
 + 'E.OriginalDateBegin, '
 + 'E.NewEventEmailSent '
 + 'FROM dbo.Events E '
 + 'inner join dbo.EventsRecurMaster AS r on E.RecurMasterID = r.RecurMasterID '
 + 'left outer join dbo.Users U on E.CreatedBy = U.UserID '
 + 'left outer join dbo.Users O on E.OwnerID = O.UserID '
 + 'left outer join dbo.Users L on E.LastUpdatedID = L.UserID '
 + 'left join dbo.EventsCategory b on E.Category = b.Category '
 + 'left join dbo.EventsLocation c on E.Location = c.Location '
 + 'WHERE (E.ModuleID = 555 AND E.EventTimeBegin BETWEEN 2012-01-01 AND 2012-12-31) OR ((E.EventTimeBegin <= DATEADD(DAY,1,''' + convert(varchar, @EndDate) + ''') AND DATEADD(minute,E.Duration,E.EventTimeBegin) >= ''' + convert(varchar, @BeginDate) + ''') OR '
 + '  (E.EventTimeBegin BETWEEN ''' + convert(varchar, @BeginDate) + ''' AND DATEADD(DAY,1,''' + convert(varchar, @EndDate) + ''')))'
 + '  AND E.Approved = 1'
 + '  AND E.Cancelled = 0'
 + ' ' + @Filter + ' '
 + ' ORDER BY E.EventDateBegin, E.EventTimeBegin, E.EventDateEnd'
EXEC (@sql)

UPDATE: I used the where statemnnt that Diego recommended but that is not having the desired result. It does not act as and If\Else scanrio (which makes sense when I think about it).

I need to first identify if the module ID is 555 and if so only pull the dates from in the hard coded range otherwise execute it as written. Please let me know if more detail is required.

  • 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-03T02:34:14+00:00Added an answer on June 3, 2026 at 2:34 am
    • is the proc failing?
    • did you try adding E.ModuleID = 555 on the where clause?
    • Do you really want to hard code the value 555? how about passing it
      on a parameter?
    • And most important question: why adding the sql statement to a
      variable and execute it? why not just run the SQL? Is it because of
      the @Filter variable?
    • also, sql server 2005 or 2008?
    • why nvarchar and not varchar on your variables (“n” occupies double
      of space)

    EDIT:

    ok, you have a OR in there so it may be tricky. Do you want everything from code 555 despite the date range value, or everything within the date range and code 555?
    I assume option 2 would make more sense so just add

    E.ModuleID = 555
    

    before the

     + '  AND E.Approved = 1'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have the following simple Stored Procedure that runs as an overnight SQL server
I'm trying to rewrite some old SQL into LINQ to SQL. I have a
I am trying to rewrite mysite.com/broadcasts to mysite.com/feed so that it will show up
I have some mutable scala code that I am trying to rewrite in a
Im trying to rewrite something ive written in Visual Basic that uses an XMLHTTP
I'm basically trying to rewrite math.pow, I have the following obviously I'm not getting
i am trying to rewrite any URL that match this pattern: ~/Ahmed ~/Name to
I'm trying to rewrite the following java method that return a list of objects(hibenrate
I am trying to rewrite a URL with .htaccess and had a question that
I'm trying rewrite game guess word from C# console to C# windows form, and

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.