If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ASP > Incorrect syntax near '#'

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-25-03, 03:43
fabs fabs is offline
Registered User
 
Join Date: Feb 2003
Posts: 1
Incorrect syntax near '#'

In VB, I can write an SQL query that goes like this:

“SELECT * FROM Staff WHERE HiredDate BETWEEN #10/23/2002# AND #10/29/2002#”

and it works perfectly. However, if I run that same query from an ASP page, I get the following error message:

Microsoft OLE DB Provider for SQL Server (0x80040E14)
Line 1: Incorrect syntax near '#'.

If I type

“SELECT * FROM Staff WHERE Name = ‘John’”

it works perfectly. The error is obviously related to the date literal. Do you know how I could create an SQL expression that will return records between two specified dates?
Reply With Quote
  #2 (permalink)  
Old 02-25-03, 04:10
rahulqa rahulqa is offline
Registered User
 
Join Date: Feb 2003
Posts: 18
If you are using sql server database then try removing # sign.

SELECT * FROM Staff WHERE HiredDate BETWEEN '10/23/2002' AND '10/29/2002'
Reply With Quote
  #3 (permalink)  
Old 02-25-03, 09:44
GadgetGuru GadgetGuru is offline
Registered User
 
Join Date: Feb 2003
Location: Other side of the ocean
Posts: 6
escape syntax

or use the escape syntax which will work against any DB:

“SELECT * FROM Staff WHERE {d'2002-10-23'} < HiredDate < {d'2002-10-29'}”

Reply With Quote
  #4 (permalink)  
Old 07-27-10, 05:10
desertrose desertrose is offline
Registered User
 
Join Date: Jul 2010
Posts: 4
hello ,,, i am totally new to sqlsever and i am suffering from z same problem
incorrect syntax near #
i removed it and enclosed may between date var between single quotes but still not working
any help please
it drives me crazy for more than a month now
Reply With Quote
  #5 (permalink)  
Old 07-27-10, 05:23
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Can you Response.Write your full query string please?
__________________
George
Twitter | Blog
Reply With Quote
  #6 (permalink)  
Old 07-27-10, 06:54
desertrose desertrose is offline
Registered User
 
Join Date: Jul 2010
Posts: 4
okay ...thanx for rplay
re8.Open("select distinct(Dist_Win_Name) from Distributers ,Daily_transaction where Distributers.Dist_Win_Name not in (select Dist_Name from Daily_transaction where Daily_transaction.Agent_No ='" + ComboBox1.SelectedItem + "'and Daily_sts='" + str1 + "' and Tran_Date between " + DateTimePicker1.Value + " and " + DateTimePicker1.Value + " and Distributers.Agent_No=Daily_transaction.Agent_No and Distributers.Agent_Name= Daily_transaction.Agent_Name and Daily_transaction.Agent_Name ='" + ComboBox1.SelectedItem + "'and Daily_sts='" + str1 + "'and Tran_Date between " + DateTimePicker1.Value + " and " + DateTimePicker1.Value + "", DB, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic)
For i = 0 To re8.RecordCount - 1
dataa = re8("Dist_Win_Name").Value
RE18.Open("select * from Daily_transaction Where Agent_No = '" + ComboBox1.SelectedItem + "' and Agent_Name = '" + TextBox2.Text + "' and Edition_serial='" + TextBox4.Text + "' and Tran_Date=#" + DateTimePicker1.Value + "#", DB, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic)
RE18.AddNew()
RE18("Agent_No").Value = ComboBox1.SelectedItem
RE18("Agent_Name").Value = TextBox2.Text
RE18("Dist_Win_No").Value = TextBox3.Text
RE18("Dist_Name").Value = dataa
RE18("Dist_Add").Value = ""
RE18("Tran_Date").Value = DateTimePicker1.Value
RE18("Rec_Copies").Value = 0
RE18("Rec_Sold").Value = 0
RE18("Rec_Returend").Value = 0
RE18("Dist_percent").Value = 0
RE18("Note").Value = 0
RE18("Daily_sts").Value = "P"
RE18("Edition_serial").Value = TextBox4.Text
RE18("Quantity").Value = 0
RE18("Estmait").Value = 0
RE18.Update()
're2.MoveLast()
'DB.Close()
RE18.Close()
Next
End If
End Sub

and error messege apears in the select statment
Reply With Quote
  #7 (permalink)  
Old 07-27-10, 07:40
desertrose desertrose is offline
Registered User
 
Join Date: Jul 2010
Posts: 4
will it seems you get nothing to understand
my problem is here
RE18.Open("select * from Daily_transaction Where Agent_No = '" + ComboBox1.SelectedItem + "' and Agent_Name = '" + TextBox2.Text + "' and Edition_serial='" + TextBox4.Text + "' and Tran_Date=#" + DateTimePicker1.Value + "#")

where i pass dalte value from my application
Reply With Quote
  #8 (permalink)  
Old 07-27-10, 10:31
Teddy Teddy is offline
Purveyor of Discontent
 
Join Date: Mar 2003
Location: The Bottom of The Barrel
Posts: 6,071
I'm guessing you're used to Access, given the hashes. Surround your datetime values in single quotes as opposed to #'s. You've got one date surrounded by #'s and several more that aren't surrounded by ANYTHING. Look close.


Also, sanitize your inputs using either stored procedures or parameterized/prepared statements. If you put this page up anywhere on the actual internet, I could find it and completely destroy your database, among other things.

I'm not kidding. Google around for "sql injection".

Also, gvee wanted to know what the final string looks like AFTER it has been constructed. Something along these lines:

Response.Write("select distinct(Dist_Win_Name) from Distributers ,Daily_transaction where Distributers.Dist_Win_Name not in (select Dist_Name from Daily_transaction where Daily_transaction.Agent_No ='" + ComboBox1.SelectedItem + "'and Daily_sts='" + str1 + "' and Tran_Date between " + DateTimePicker1.Value + " and " + DateTimePicker1.Value + " and Distributers.Agent_No=Daily_transaction.Agent_No and Distributers.Agent_Name= Daily_transaction.Agent_Name and Daily_transaction.Agent_Name ='" + ComboBox1.SelectedItem + "'and Daily_sts='" + str1 + "'and Tran_Date between " + DateTimePicker1.Value + " and " + DateTimePicker1.Value + "")
__________________
oh yeah... documentation... I have heard of that.

*** What Do You Want In The MS Access Forum? ***

Last edited by Teddy; 07-27-10 at 10:36.
Reply With Quote
  #9 (permalink)  
Old 07-27-10, 10:45
desertrose desertrose is offline
Registered User
 
Join Date: Jul 2010
Posts: 4
Red face

well as i told you am prand new for using sql
okay okay ..i just want ti select datetime value from my database as selected by end user(my application)
'select * from table where date='"+datetimepicker.value +"''
and datetimepicker.value holds the
value '01'07'2010 12:23:11'
and my date column in db is datetime type

*** actually i tried this
where date =Convert( datetime ,'" + (DateTimePicker1.Value) + "', 102)
but it returns incorrect syntax near '01'07'2010'

what can i do ?? please help mee
Reply With Quote
  #10 (permalink)  
Old 08-11-10, 07:59
scarley4 scarley4 is offline
Registered User
 
Join Date: Aug 2010
Posts: 1
If you are using sql server database then try removing # sign.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On