• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 478
  • Last Modified:

SQL LIKE problem

Hi.

I am trying to get some data from SQL database with this string:

strDate = CDate(mydate)
sql = "select NoRcpt from RcptMain where Dates LIKE '%@" & strDate & "%' order by NoRcpt DESC"

However nothing returns.

NoRcpt is Integer column (used as Identity)
Dates is Datetime column



There is data in that table, and number returned should be 1 but i am receving nothing so I guess the problem is in  Dates LIKE '%@" & strDate & "%'.

Please assist.

P.S. In string strDate looks like "14.11.2010" in database in Dates column that data is 14.11.2010. 0:00:00 that's why i am using LIKE. I could format database input to ignore hh:mm:ss but I will use that eventualy.
0
Grogel
Asked:
Grogel
2 Solutions
 
Jini Jose.Net Team LeadCommented:
it is not good to use the like for date fields.
if you want to use like for date fields use the below.

select * from rcptmain where cast(dates as varchar)
like '%'+ cast(year(getdate()) as varchar)+'%'
and cast(dates as varchar) like '%'+ cast(month(getdate()) as varchar)+'%'
and cast(dates as varchar) like '%'+ cast(day(getdate()) as varchar)+'%'

othewise you can use the between stmnt

select * from rcptmain where dates between '2010-01-01 00:00' and '2010-01-30 23:59'
0
 
cyberkiwiCommented:
You should be using parameters, which will take care of formatting the date for SQL.
But since you are using a string literal, I think you want anything that is on that day regardless of time right?

strDate = CDate(mydate)
sql = "select NoRcpt from RcptMain where DateDiff(d, Dates, '" & Year(strDate) & "-" & Month(strDate) & "-" & Day(strDate) & "') = 0 order by NoRcpt DESC"
0
 
GrogelAuthor Commented:
Excelent, fast and smart guys.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

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