0

I want to have a default value for my SQL Server 2008 stored proc as below:

Create Process sp_Transactions1
     @earliestDate varchar(12)= CAST(DATEPART(YEAR,DATEADD(m,-3, getdate())) AS VARCHAR(4))  + RIGHT('00' + CAST(DATEPART(mm, DATEADD(m,-2, getdate())) AS varchar(2)), 2)+ '01'
As

But it won't compile

Is this not possible?

1

1 Answer 1

1

As Kritner said above, the default value of parameters can be only a constant. However, if the NULL value does not have another meaning in this context, you can use something like this:

CREATE PROCEDURE sp_Transactions1
     @earliestDate date=NULL
AS
IF @earliestDate IS NULL 
    SET @earliestDate=CONVERT(DATE,
        STR(YEAR(DATEADD(m,-3, getdate())),4)
        +RIGHT('0'+CONVERT(VARCHAR(2),MONTH(DATEADD(m,-2, getdate()))),2)
        +'01'
    )
--...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.