Calculated default datetime parameters for stored procedure
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
add a comment |
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
You've got a good answer, but please note that@End
should be bigger than@Start
– Zohar Peled
Nov 20 at 6:47
add a comment |
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
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
sql sql-server tsql stored-procedures
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
add a comment |
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
add a comment |
4 Answers
4
active
oldest
votes
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
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
add a comment |
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
add a comment |
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)
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
add a comment |
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;
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 20 at 7:12
Ravi
942213
942213
add a comment |
add a comment |
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)
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
add a comment |
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)
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
add a comment |
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)
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)
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
add a comment |
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
add a comment |
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;
add a comment |
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;
add a comment |
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;
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;
answered Nov 20 at 7:18
Evaldas Buinauskas
8,32082757
8,32082757
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
You've got a good answer, but please note that
@End
should be bigger than@Start
– Zohar Peled
Nov 20 at 6:47