Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

setting a default date to an SP's parameter 1

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
0
0
GB
I seem unable to work out the syntax to set a stored procedure paramter to a default date.

I have tried
Code:
@EndDate char(10) = GETDATE

But that errors with "Conversion failed when converting date and/or time from character string."

I then tried
Code:
@EndDate char(10) = CONVERT(char(10),GETDATE(),126)

But that won't compile.

I want to set a default if no date is passed to the SP as the current date in 'YYYY-MM-DD' format.

How would I do this?

Thanks,
1DMF

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
Personally, I would make the parameter nullable then at the top of the procedure do a NULL check on it and set it to CURRENT_TIMESTAMP if it is NULL.

Code:
    ...some parameters...
    @EndDate char(10) = NULL
    ...some more parameters...

...procedure actual activity starts...
SELECT @EndDate = ISNULL(@EndDate, CURRENT_TIMESTAMP);

...procedure actual activity carries on...

If that makes any sense :)

Rhys

"Technological progress is like an axe in the hands of a pathological criminal"
"Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe"
Albert Einstein
 
Set the parameter to null. Within the stored procedure, as the first line, use:

Set @EndDate = coalesce(@EndDate, Convert(char(10), GetDate(), 121))


-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Thanks George,

I was close I found this on stack overflow
Code:
Set @currentDate = Coalesce(@currentDate , GetDate())
and was about to have a play!

Your help as always is much appreciated.

1DMF.

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top