Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
SQL
declare @fromdate varchar(50)
declare @todate varchar(50)  
set @fromdate='2015/09/01'
set @todate='2015/09/30'
exec(@fromdate)
exec(@todate)



am getting error for this kindly tell me the solution..


Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '2015'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '2015'.
Posted
Updated 12-Oct-15 23:54pm
v2
Comments
Maciej Los 13-Oct-15 10:54am    
What? Date is a date, not string!

Please, read my comment to the question.

Date is a date and nothing else!

If you want to add date, use proper variable type:

SQL
DECLARE @fromdate DATE = '2015/10/01'


If MS SQL Server can't recognize such of format, use: SET DATEFORMAT[^] command.

SQL
SET DATEFORMAT ymd;
DECLARE @fromdate DATE = '2015/10/01'


For further information, please see:
CAST and CONVERT (Transact-SQL)[^]
Date and Time Data Types and Functions (Transact-SQL)[^]
 
Share this answer
 
Hi,

Check this...


SQL
declare @fromdate varchar(50)
declare @todate varchar(50)
set @fromdate='2015/09/01'
set @todate='2015/09/30'

SELECT @fromdate
SELECT @todate


Hope this will help you.

Cheers
 
Share this answer
 
Comments
Maciej Los 13-Oct-15 10:59am    
Wrong approach! Please, see my answer.
Magic Wonder 14-Oct-15 1:59am    
I just removed the error. On first see, i found use of exec() is wrong. so just improved that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900