Solved

SQL LIKE problem

Posted on 2010-11-14
3
472 Views
Last Modified: 2012-05-10
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
Comment
Question by:Grogel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 10

Accepted Solution

by:
Jini Jose earned 250 total points
ID: 34131767
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
 
LVL 58

Assisted Solution

by:cyberkiwi
cyberkiwi earned 250 total points
ID: 34131786
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
 

Author Closing Comment

by:Grogel
ID: 34131956
Excelent, fast and smart guys.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question