Incorrect syntax near '12'. at DateTime.Today.Date

doramail05
doramail05 used Ask the Experts™
on
i have this incorrect syntax
'Incorrect syntax near '12'.'

where the so_order_date is 7/17/2009 12:00:00 AM in SQL Server 2005
string sqlcmddual2 = "USE " + txtProntoDB.Text + "; Select * FROM sales_order WHERE so_bo_suffix = '' AND LEFT(so_order_no, 3)  = 356 AND so_order_date =" + DateTime.Today.Date;

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
have you try to print out the text you sent to sql server?

i guess you need to add ' (single quote) arround the datetime.today.date for sql syntax

i.e.
string sqlcmddual2 = "USE " + txtProntoDB.Text + "; Select * FROM sales_order WHERE so_bo_suffix = '' AND LEFT(so_order_no, 3)  = 356 AND so_order_date ='" + DateTime.Today.Date +"'";

Open in new window

Guy Hengel [angelIII / a3]Billing Engineer
Most Valuable Expert 2014
Top Expert 2009

Commented:
note: this syntax is not recommended.
1) using dynamic sql, calling for sql injection
2) not using bind paramters and at the same time no explicit data type conversion (check up CONVERT() function ....)
3) date = date ... I presume the so_order_date might have some time portion stored?
  if so, that sql will never return anything. you must use so_order_date >= {date} and so_order_date < dateadd(day, 1, {date} )

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial