1

I have a dynamic stored procedure that includes the following input variables and Where condition with the column R.dateRec being formatted as datetime.

@rangeStart datetime,
@rangeEnd datetime

AND   R.dateRec BETWEEN ' + @rangeStart + ' AND ' + @rangeEnd + '

When I run the procedure without this condition than it works well but when adding this it returns the following error, maybe because it doesn't recognise the values as dates:

Conversion failed when converting date and/or time from character string.

Can someone tell me how I can prevent this from happening ? The column in question only contains valid dates (no blank or NULL values).

Many thanks in advance, Tim.

2 Answers 2

2

need to convert the date parameters to varchar first if it is a dynamic sql...

added quotes

{beginning of query...} + ' and R.dateRec between ''' + convert(varchar, @rangeStart, 111) + ''' and ''' + convert(varchar, @rangeEnd, 111) + ''' {continue query}'
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. It's a dynamic procedure so I cant keep it like that.
Thanks for the update - this works perfect ! SQL sometimes can be picky. :)
0

are you missing quotes? i.e.

   AND   R.dateRec BETWEEN ''' + @rangeStart + ''' AND ''' + @rangeEnd + '''

1 Comment

Thanks. That didn't work but the solution from krawl resolved it.

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.