[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 285
  • Last Modified:

sql query with like statement not working

Im tyring the below code:

cmd.CommandText = "Select Count(*) From nousersonline where PageUrl =%@forumname% and (ActivityDate BETWEEN DATEADD('n', -5, Now()) and Now())"
 cmd.Parameters.AddWithValue("@forumname", forumname)

Open in new window


but i get the error:

Syntax error in query expression 'PageUrl =%@forumname% and (ActivityDate BETWEEN DATEADD('n', -5, Now()) and Now())'.
0
runnerjp2005
Asked:
runnerjp2005
2 Solutions
 
CodeCruiserCommented:
Change this

PageUrl =%@forumname%

to

PageUrl Like @forumname

and then supply the % in the parameter value of forumname
0
 
wdosanjosCommented:
Please try the following:

cmd.CommandText = "Select Count(*) From nousersonline where PageUrl like @forumname and (ActivityDate BETWEEN DATEADD('n', -5, Now()) and Now())"
cmd.Parameters.AddWithValue("@forumname", "%" & forumname & "%")

Open in new window

0
 
Imran Javed ZiaCommented:
if you are not interested in like operator then please try following

cmd.CommandText = "Select Count(*) From nousersonline where PageUrl = @forumname and (ActivityDate BETWEEN DATEADD('n', -5, Now()) and Now())"  
 cmd.Parameters.AddWithValue("@forumname", forumname)
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now