Calculated default datetime parameters for stored procedure












1














I'm trying to create stored procedure which will return rows WHERE Creation date between 'start' and 'end' value. But I want to consider possibility of empty values for stored procedure, so I need default values.



CREATE PROCEDURE [dbo].[GetProjectsRatingByCreationDateInterval]
@Start DATETIME = GETDATE,
@End DATETIME = DATEADD(MONTH, -1, GETDATE())
AS
SELECT
ID, ProjectName, Rating, CreationDateTime
FROM
Projects
WHERE
CreationDateTime BETWEEN @Start AND @End


Tried to do this like that but received this error:




Incorrect syntax near '('.




It references the DATEADD function call in the @End declaration.










share|improve this question
























  • You've got a good answer, but please note that @End should be bigger than @Start
    – Zohar Peled
    Nov 20 at 6:47
















1














I'm trying to create stored procedure which will return rows WHERE Creation date between 'start' and 'end' value. But I want to consider possibility of empty values for stored procedure, so I need default values.



CREATE PROCEDURE [dbo].[GetProjectsRatingByCreationDateInterval]
@Start DATETIME = GETDATE,
@End DATETIME = DATEADD(MONTH, -1, GETDATE())
AS
SELECT
ID, ProjectName, Rating, CreationDateTime
FROM
Projects
WHERE
CreationDateTime BETWEEN @Start AND @End


Tried to do this like that but received this error:




Incorrect syntax near '('.




It references the DATEADD function call in the @End declaration.










share|improve this question
























  • You've got a good answer, but please note that @End should be bigger than @Start
    – Zohar Peled
    Nov 20 at 6:47














1












1








1







I'm trying to create stored procedure which will return rows WHERE Creation date between 'start' and 'end' value. But I want to consider possibility of empty values for stored procedure, so I need default values.



CREATE PROCEDURE [dbo].[GetProjectsRatingByCreationDateInterval]
@Start DATETIME = GETDATE,
@End DATETIME = DATEADD(MONTH, -1, GETDATE())
AS
SELECT
ID, ProjectName, Rating, CreationDateTime
FROM
Projects
WHERE
CreationDateTime BETWEEN @Start AND @End


Tried to do this like that but received this error:




Incorrect syntax near '('.




It references the DATEADD function call in the @End declaration.










share|improve this question















I'm trying to create stored procedure which will return rows WHERE Creation date between 'start' and 'end' value. But I want to consider possibility of empty values for stored procedure, so I need default values.



CREATE PROCEDURE [dbo].[GetProjectsRatingByCreationDateInterval]
@Start DATETIME = GETDATE,
@End DATETIME = DATEADD(MONTH, -1, GETDATE())
AS
SELECT
ID, ProjectName, Rating, CreationDateTime
FROM
Projects
WHERE
CreationDateTime BETWEEN @Start AND @End


Tried to do this like that but received this error:




Incorrect syntax near '('.




It references the DATEADD function call in the @End declaration.







sql sql-server tsql stored-procedures






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 9:59









Rahul Neekhra

6021627




6021627










asked Nov 20 at 5:53









Maxim Baranov

82




82












  • You've got a good answer, but please note that @End should be bigger than @Start
    – Zohar Peled
    Nov 20 at 6:47


















  • You've got a good answer, but please note that @End should be bigger than @Start
    – Zohar Peled
    Nov 20 at 6:47
















You've got a good answer, but please note that @End should be bigger than @Start
– Zohar Peled
Nov 20 at 6:47




You've got a good answer, but please note that @End should be bigger than @Start
– Zohar Peled
Nov 20 at 6:47












4 Answers
4






active

oldest

votes


















1














Please set the value as Null and set inside procedure like below



CREATE PROCEDURE [dbo].[GetProjectsRatingByCreationDateInterval]

@Start DATETIME NULL,
@End DATETIME NULL
AS

IF @Start is null
SET @Start = GETDATE()

IF @End is null
SET @End = DATEADD(MONTH, -1, GETDATE())

SELECT
ID, ProjectName, Rating, CreationDateTime
FROM
Projects
WHERE
CreationDateTime BETWEEN @Start and @End





share|improve this answer























  • Or easier - select @start = ISNULL(@Start, GetDate()), @end = ISNULL(@End, DATEADD(MONTH, 1, GETDATE())
    – Zohar Peled
    Nov 20 at 6:47










  • Correct me If i am wrong, In your query the SET operation for @ start and @ end will run for sure even if it having value. But here it will be skipped if it have value already.
    – Santhana
    Nov 20 at 6:53



















1














the problem is here



@Start DATETIME = GETDATE,


getdate is a function so write it as GETDATE()



@Start DATETIME = GETDATE(),


The Procedure look like that



    CREATE PROCEDURE [dbo].[GetProjectsRatingByCreationDateInterval]
@Start DATETIME = GETDATE(),
@End DATETIME = DATEADD(MONTH, -1, GETDATE())
AS
BEGIN
SELECT
ID, ProjectName, Rating, CreationDateTime
FROM
Projects
WHERE
CreationDateTime BETWEEN @Start AND @End
END





share|improve this answer





























    0














    You can use below code for your need.



    CREATE PROCEDURE [dbo].[GetProjectsRatingByCreationDateInterval]
    @Start DATETIME = CURRENT_TIMESTAMP,
    @End DATETIME = NULL
    AS
    SELECT
    ID, ProjectName, Rating, CreationDateTime
    FROM
    Projects
    WHERE
    CreationDateTime BETWEEN @Start AND (CASE WHEN @End IS NULL THEN DATEADD(MONTH, -1, @Start) ELSE @End END)





    share|improve this answer





















    • This is shorter, but SQL Server will find it more difficult to optimize.
      – Evaldas Buinauskas
      Nov 20 at 6:55










    • may be depending upon Projects table usages of and design. But the case sufficing his requirement with neat & clean code.
      – Rahul Neekhra
      Nov 20 at 6:57



















    0














    You also can make use of ISNULL() function to default to a value if whatever has been passed is a NULL:



    CREATE PROCEDURE dbo.GetProjectsRatingByCreationDateInterval
    @Start DATETIME NULL, @End DATETIME NULL
    AS
    BEGIN
    SET @Start = ISNULL(@Start, GETDATE());
    SET @End = ISNULL(@Start, DATEADD(MONTH, -1, GETDATE()));

    SELECT ID, ProjectName, Rating, CreationDateTime
    FROM Projects
    WHERE CreationDateTime BETWEEN @Start AND @End;
    END;





    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387012%2fcalculated-default-datetime-parameters-for-stored-procedure%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Please set the value as Null and set inside procedure like below



      CREATE PROCEDURE [dbo].[GetProjectsRatingByCreationDateInterval]

      @Start DATETIME NULL,
      @End DATETIME NULL
      AS

      IF @Start is null
      SET @Start = GETDATE()

      IF @End is null
      SET @End = DATEADD(MONTH, -1, GETDATE())

      SELECT
      ID, ProjectName, Rating, CreationDateTime
      FROM
      Projects
      WHERE
      CreationDateTime BETWEEN @Start and @End





      share|improve this answer























      • Or easier - select @start = ISNULL(@Start, GetDate()), @end = ISNULL(@End, DATEADD(MONTH, 1, GETDATE())
        – Zohar Peled
        Nov 20 at 6:47










      • Correct me If i am wrong, In your query the SET operation for @ start and @ end will run for sure even if it having value. But here it will be skipped if it have value already.
        – Santhana
        Nov 20 at 6:53
















      1














      Please set the value as Null and set inside procedure like below



      CREATE PROCEDURE [dbo].[GetProjectsRatingByCreationDateInterval]

      @Start DATETIME NULL,
      @End DATETIME NULL
      AS

      IF @Start is null
      SET @Start = GETDATE()

      IF @End is null
      SET @End = DATEADD(MONTH, -1, GETDATE())

      SELECT
      ID, ProjectName, Rating, CreationDateTime
      FROM
      Projects
      WHERE
      CreationDateTime BETWEEN @Start and @End





      share|improve this answer























      • Or easier - select @start = ISNULL(@Start, GetDate()), @end = ISNULL(@End, DATEADD(MONTH, 1, GETDATE())
        – Zohar Peled
        Nov 20 at 6:47










      • Correct me If i am wrong, In your query the SET operation for @ start and @ end will run for sure even if it having value. But here it will be skipped if it have value already.
        – Santhana
        Nov 20 at 6:53














      1












      1








      1






      Please set the value as Null and set inside procedure like below



      CREATE PROCEDURE [dbo].[GetProjectsRatingByCreationDateInterval]

      @Start DATETIME NULL,
      @End DATETIME NULL
      AS

      IF @Start is null
      SET @Start = GETDATE()

      IF @End is null
      SET @End = DATEADD(MONTH, -1, GETDATE())

      SELECT
      ID, ProjectName, Rating, CreationDateTime
      FROM
      Projects
      WHERE
      CreationDateTime BETWEEN @Start and @End





      share|improve this answer














      Please set the value as Null and set inside procedure like below



      CREATE PROCEDURE [dbo].[GetProjectsRatingByCreationDateInterval]

      @Start DATETIME NULL,
      @End DATETIME NULL
      AS

      IF @Start is null
      SET @Start = GETDATE()

      IF @End is null
      SET @End = DATEADD(MONTH, -1, GETDATE())

      SELECT
      ID, ProjectName, Rating, CreationDateTime
      FROM
      Projects
      WHERE
      CreationDateTime BETWEEN @Start and @End






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 20 at 6:42









      ravi polara

      315111




      315111










      answered Nov 20 at 6:09









      Santhana

      10110




      10110












      • Or easier - select @start = ISNULL(@Start, GetDate()), @end = ISNULL(@End, DATEADD(MONTH, 1, GETDATE())
        – Zohar Peled
        Nov 20 at 6:47










      • Correct me If i am wrong, In your query the SET operation for @ start and @ end will run for sure even if it having value. But here it will be skipped if it have value already.
        – Santhana
        Nov 20 at 6:53


















      • Or easier - select @start = ISNULL(@Start, GetDate()), @end = ISNULL(@End, DATEADD(MONTH, 1, GETDATE())
        – Zohar Peled
        Nov 20 at 6:47










      • Correct me If i am wrong, In your query the SET operation for @ start and @ end will run for sure even if it having value. But here it will be skipped if it have value already.
        – Santhana
        Nov 20 at 6:53
















      Or easier - select @start = ISNULL(@Start, GetDate()), @end = ISNULL(@End, DATEADD(MONTH, 1, GETDATE())
      – Zohar Peled
      Nov 20 at 6:47




      Or easier - select @start = ISNULL(@Start, GetDate()), @end = ISNULL(@End, DATEADD(MONTH, 1, GETDATE())
      – Zohar Peled
      Nov 20 at 6:47












      Correct me If i am wrong, In your query the SET operation for @ start and @ end will run for sure even if it having value. But here it will be skipped if it have value already.
      – Santhana
      Nov 20 at 6:53




      Correct me If i am wrong, In your query the SET operation for @ start and @ end will run for sure even if it having value. But here it will be skipped if it have value already.
      – Santhana
      Nov 20 at 6:53













      1














      the problem is here



      @Start DATETIME = GETDATE,


      getdate is a function so write it as GETDATE()



      @Start DATETIME = GETDATE(),


      The Procedure look like that



          CREATE PROCEDURE [dbo].[GetProjectsRatingByCreationDateInterval]
      @Start DATETIME = GETDATE(),
      @End DATETIME = DATEADD(MONTH, -1, GETDATE())
      AS
      BEGIN
      SELECT
      ID, ProjectName, Rating, CreationDateTime
      FROM
      Projects
      WHERE
      CreationDateTime BETWEEN @Start AND @End
      END





      share|improve this answer


























        1














        the problem is here



        @Start DATETIME = GETDATE,


        getdate is a function so write it as GETDATE()



        @Start DATETIME = GETDATE(),


        The Procedure look like that



            CREATE PROCEDURE [dbo].[GetProjectsRatingByCreationDateInterval]
        @Start DATETIME = GETDATE(),
        @End DATETIME = DATEADD(MONTH, -1, GETDATE())
        AS
        BEGIN
        SELECT
        ID, ProjectName, Rating, CreationDateTime
        FROM
        Projects
        WHERE
        CreationDateTime BETWEEN @Start AND @End
        END





        share|improve this answer
























          1












          1








          1






          the problem is here



          @Start DATETIME = GETDATE,


          getdate is a function so write it as GETDATE()



          @Start DATETIME = GETDATE(),


          The Procedure look like that



              CREATE PROCEDURE [dbo].[GetProjectsRatingByCreationDateInterval]
          @Start DATETIME = GETDATE(),
          @End DATETIME = DATEADD(MONTH, -1, GETDATE())
          AS
          BEGIN
          SELECT
          ID, ProjectName, Rating, CreationDateTime
          FROM
          Projects
          WHERE
          CreationDateTime BETWEEN @Start AND @End
          END





          share|improve this answer












          the problem is here



          @Start DATETIME = GETDATE,


          getdate is a function so write it as GETDATE()



          @Start DATETIME = GETDATE(),


          The Procedure look like that



              CREATE PROCEDURE [dbo].[GetProjectsRatingByCreationDateInterval]
          @Start DATETIME = GETDATE(),
          @End DATETIME = DATEADD(MONTH, -1, GETDATE())
          AS
          BEGIN
          SELECT
          ID, ProjectName, Rating, CreationDateTime
          FROM
          Projects
          WHERE
          CreationDateTime BETWEEN @Start AND @End
          END






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 at 7:12









          Ravi

          942213




          942213























              0














              You can use below code for your need.



              CREATE PROCEDURE [dbo].[GetProjectsRatingByCreationDateInterval]
              @Start DATETIME = CURRENT_TIMESTAMP,
              @End DATETIME = NULL
              AS
              SELECT
              ID, ProjectName, Rating, CreationDateTime
              FROM
              Projects
              WHERE
              CreationDateTime BETWEEN @Start AND (CASE WHEN @End IS NULL THEN DATEADD(MONTH, -1, @Start) ELSE @End END)





              share|improve this answer





















              • This is shorter, but SQL Server will find it more difficult to optimize.
                – Evaldas Buinauskas
                Nov 20 at 6:55










              • may be depending upon Projects table usages of and design. But the case sufficing his requirement with neat & clean code.
                – Rahul Neekhra
                Nov 20 at 6:57
















              0














              You can use below code for your need.



              CREATE PROCEDURE [dbo].[GetProjectsRatingByCreationDateInterval]
              @Start DATETIME = CURRENT_TIMESTAMP,
              @End DATETIME = NULL
              AS
              SELECT
              ID, ProjectName, Rating, CreationDateTime
              FROM
              Projects
              WHERE
              CreationDateTime BETWEEN @Start AND (CASE WHEN @End IS NULL THEN DATEADD(MONTH, -1, @Start) ELSE @End END)





              share|improve this answer





















              • This is shorter, but SQL Server will find it more difficult to optimize.
                – Evaldas Buinauskas
                Nov 20 at 6:55










              • may be depending upon Projects table usages of and design. But the case sufficing his requirement with neat & clean code.
                – Rahul Neekhra
                Nov 20 at 6:57














              0












              0








              0






              You can use below code for your need.



              CREATE PROCEDURE [dbo].[GetProjectsRatingByCreationDateInterval]
              @Start DATETIME = CURRENT_TIMESTAMP,
              @End DATETIME = NULL
              AS
              SELECT
              ID, ProjectName, Rating, CreationDateTime
              FROM
              Projects
              WHERE
              CreationDateTime BETWEEN @Start AND (CASE WHEN @End IS NULL THEN DATEADD(MONTH, -1, @Start) ELSE @End END)





              share|improve this answer












              You can use below code for your need.



              CREATE PROCEDURE [dbo].[GetProjectsRatingByCreationDateInterval]
              @Start DATETIME = CURRENT_TIMESTAMP,
              @End DATETIME = NULL
              AS
              SELECT
              ID, ProjectName, Rating, CreationDateTime
              FROM
              Projects
              WHERE
              CreationDateTime BETWEEN @Start AND (CASE WHEN @End IS NULL THEN DATEADD(MONTH, -1, @Start) ELSE @End END)






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 20 at 6:50









              Rahul Neekhra

              6021627




              6021627












              • This is shorter, but SQL Server will find it more difficult to optimize.
                – Evaldas Buinauskas
                Nov 20 at 6:55










              • may be depending upon Projects table usages of and design. But the case sufficing his requirement with neat & clean code.
                – Rahul Neekhra
                Nov 20 at 6:57


















              • This is shorter, but SQL Server will find it more difficult to optimize.
                – Evaldas Buinauskas
                Nov 20 at 6:55










              • may be depending upon Projects table usages of and design. But the case sufficing his requirement with neat & clean code.
                – Rahul Neekhra
                Nov 20 at 6:57
















              This is shorter, but SQL Server will find it more difficult to optimize.
              – Evaldas Buinauskas
              Nov 20 at 6:55




              This is shorter, but SQL Server will find it more difficult to optimize.
              – Evaldas Buinauskas
              Nov 20 at 6:55












              may be depending upon Projects table usages of and design. But the case sufficing his requirement with neat & clean code.
              – Rahul Neekhra
              Nov 20 at 6:57




              may be depending upon Projects table usages of and design. But the case sufficing his requirement with neat & clean code.
              – Rahul Neekhra
              Nov 20 at 6:57











              0














              You also can make use of ISNULL() function to default to a value if whatever has been passed is a NULL:



              CREATE PROCEDURE dbo.GetProjectsRatingByCreationDateInterval
              @Start DATETIME NULL, @End DATETIME NULL
              AS
              BEGIN
              SET @Start = ISNULL(@Start, GETDATE());
              SET @End = ISNULL(@Start, DATEADD(MONTH, -1, GETDATE()));

              SELECT ID, ProjectName, Rating, CreationDateTime
              FROM Projects
              WHERE CreationDateTime BETWEEN @Start AND @End;
              END;





              share|improve this answer


























                0














                You also can make use of ISNULL() function to default to a value if whatever has been passed is a NULL:



                CREATE PROCEDURE dbo.GetProjectsRatingByCreationDateInterval
                @Start DATETIME NULL, @End DATETIME NULL
                AS
                BEGIN
                SET @Start = ISNULL(@Start, GETDATE());
                SET @End = ISNULL(@Start, DATEADD(MONTH, -1, GETDATE()));

                SELECT ID, ProjectName, Rating, CreationDateTime
                FROM Projects
                WHERE CreationDateTime BETWEEN @Start AND @End;
                END;





                share|improve this answer
























                  0












                  0








                  0






                  You also can make use of ISNULL() function to default to a value if whatever has been passed is a NULL:



                  CREATE PROCEDURE dbo.GetProjectsRatingByCreationDateInterval
                  @Start DATETIME NULL, @End DATETIME NULL
                  AS
                  BEGIN
                  SET @Start = ISNULL(@Start, GETDATE());
                  SET @End = ISNULL(@Start, DATEADD(MONTH, -1, GETDATE()));

                  SELECT ID, ProjectName, Rating, CreationDateTime
                  FROM Projects
                  WHERE CreationDateTime BETWEEN @Start AND @End;
                  END;





                  share|improve this answer












                  You also can make use of ISNULL() function to default to a value if whatever has been passed is a NULL:



                  CREATE PROCEDURE dbo.GetProjectsRatingByCreationDateInterval
                  @Start DATETIME NULL, @End DATETIME NULL
                  AS
                  BEGIN
                  SET @Start = ISNULL(@Start, GETDATE());
                  SET @End = ISNULL(@Start, DATEADD(MONTH, -1, GETDATE()));

                  SELECT ID, ProjectName, Rating, CreationDateTime
                  FROM Projects
                  WHERE CreationDateTime BETWEEN @Start AND @End;
                  END;






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 at 7:18









                  Evaldas Buinauskas

                  8,32082757




                  8,32082757






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387012%2fcalculated-default-datetime-parameters-for-stored-procedure%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                      Alcedinidae

                      Origin of the phrase “under your belt”?