Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL Between Stored Proc

SQL Between Stored Proc

Scheduled Pinned Locked Moved Database
databasequestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Illegal Operation
    wrote on last edited by
    #1

    Hi! Can someone please tell me how would I go about creating a Stored Proc to select records between dates? I have 2 variables, date1 and date2 I want to create a stored proc as follows: select * from table between date1 and date2 where the dates are variables in the stored proc.

    Illegal Operation

    N A 2 Replies Last reply
    0
    • I Illegal Operation

      Hi! Can someone please tell me how would I go about creating a Stored Proc to select records between dates? I have 2 variables, date1 and date2 I want to create a stored proc as follows: select * from table between date1 and date2 where the dates are variables in the stored proc.

      Illegal Operation

      N Offline
      N Offline
      Niladri_Biswas
      wrote on last edited by
      #2

      select * from table where datecolumn between date1 and date2 e.g.

      declare @t table(activity varchar(10),datecol datetime)
      insert into @t
      select 'activity1', '2009-12-01' union all
      select 'activity2','2009-12-02' union all
      select 'activity3','2009-12-15' union all
      select 'activity4','2010-01-02' union all
      select 'activity5','2009-12-31'

      Declare 2 date variables

      declare @date1 datetime,@date2 datetime
      set @date1 = '2009-12-01'
      set @date2 = '2009-12-15'

      I want to select records between 1st Dec 2009 to 15th Dec 2009. Query:

      select * from @t
      where datecol between @date1 and @date2

      Output: activity datecol

      activity1 2009-12-01 00:00:00.000
      activity2 2009-12-02 00:00:00.000
      activity3 2009-12-15 00:00:00.000

      Niladri Biswas

      1 Reply Last reply
      0
      • I Illegal Operation

        Hi! Can someone please tell me how would I go about creating a Stored Proc to select records between dates? I have 2 variables, date1 and date2 I want to create a stored proc as follows: select * from table between date1 and date2 where the dates are variables in the stored proc.

        Illegal Operation

        A Offline
        A Offline
        Andy_L_J
        wrote on last edited by
        #3

        CREATE PROCEDURE usp_GetDataFromDateRange(
        @Start DATETIME,
        @End DATETIME
        )
        AS
        BEGIN
        SET NOCOUNT ON;

        SELECT * FROM MyTable
        WHERE DateCol BETWEEN @Start AND @End

        RETURN

        END

        I don't speak Idiot - please talk slowly and clearly 'This space for rent' Driven to the arms of Heineken by the wife

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups