Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 sytax error

Author  Topic 

smitha
Posting Yak Master

100 Posts

Posted - 2009-12-02 : 06:04:42
Hi,
I am using select statement in VBA and I am getting the data from sql database. My code goes like this

sCrit = "where DATE1 = #" & Format$(Date, "DD/MM/yyyy") & "#"
Set rsRepQry = New ADODB.Recordset
rsRepQry.Open "Select * from E1_SHIFTA " & sCrit, conDB, adOpenStatic, adLockReadOnly

but when I run this, I am getting the error INCORRECT SYNTAX NEAR #

can anyone help this

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2009-12-02 : 07:17:20
I don't think you need all that. SQL will recognize '20091231' as well as 12/31/2009. Also, SQL uses "'" for dates, not the # like Access does.


sCrit = "where DATE1 = '" Date & "'"



Everyday I learn something that somebody else already knew
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-12-02 : 07:23:21
1 It is better you use stored procedure with parameters
2 Dont format the date in ambigious dd/mm/yyyy format. Better use dd-MMM-yyyy or YYYYMMDD

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

smitha
Posting Yak Master

100 Posts

Posted - 2009-12-04 : 00:26:10
quote:
Originally posted by jimf

I don't think you need all that. SQL will recognize '20091231' as well as 12/31/2009. Also, SQL uses "'" for dates, not the # like Access does.


sCrit = "where DATE1 = '" Date & "'"



Everyday I learn something that somebody else already knew



Thanks I got the result but only with single quotes for Date

Smitha
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-12-04 : 02:18:33
quote:
Originally posted by smitha

quote:
Originally posted by jimf

I don't think you need all that. SQL will recognize '20091231' as well as 12/31/2009. Also, SQL uses "'" for dates, not the # like Access does.


sCrit = "where DATE1 = '" Date & "'"



Everyday I learn something that somebody else already knew



Thanks I got the result but only with single quotes for Date

Smitha


It means you didn't consider my points
You will get errors for the dates desc 19, nov 20, etc

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

smitha
Posting Yak Master

100 Posts

Posted - 2010-01-06 : 06:46:02
I want to carry this forward. I am not getting the correct result for this statement.

sCrit = "where DATE1 ='Date&'"


Smitha
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-06 : 06:58:36
sCrit = "where DATE1 = '" & Format$(Date, "yyyy-MM-DD") & "'"
Go to Top of Page
   

- Advertisement -