user3253756 user3253756 - 5 months ago 26
SQL Question

Msg 102, Level 15, State 1, Line 9 Incorrect syntax near '-'

I have created a stored procedure successfully. When I execute this procedure by passing parameters it throws the following exception:


Msg 102, Level 15, State 1, Line 9

Incorrect syntax near '-'


USE [master]
GO

DECLARE @return_value int

EXEC @return_value = [dbo].[spJobsGet]
@Email = N'peter_grout@outlook.com',
@Password = N'password',
@JobSheetID = 0,
@PropertyID = 0,
@DateFrom = 2014-06-20,
@DateTo = 2014-07-20

SELECT 'Return Value' = @return_value

GO


I am stuck. Please help.

Answer

You need to put quotes around your date string literals:

@DateFrom = '2014-06-20', 
@DateTo = '2014-07-20'

You should be aware that the format you are using can be ambiguous, a better option would be

@DateFrom = '20140620', 
@DateTo = '20140720'

which is always interpreted as YYYYMMDD.

https://technet.microsoft.com/en-us/library/ms180878.aspx#UnseparatedStringFormat

Comments