Solved

Compare string to date in sql

Posted on 2011-09-21
9
184 Views
Last Modified: 2012-05-12
I have the query

select * from DateTTable
where dataField like '%j%'

this query return all results, however dataField  is only consists of date

I am I missing something

0
Comment
Question by:erikTsomik
[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
  • 4
  • 3
  • 2
9 Comments
 
LVL 8

Expert Comment

by:venk_r
ID: 36575843
Is the dateField declared as DATETIME  datatype?
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 36576065
yes does declares as datetime
0
 
LVL 8

Accepted Solution

by:
venk_r earned 500 total points
ID: 36576107
Please don't handle datetime field as string values. You will not get the desired result.The 'like %%' clause are string related operators.
When you want query date field please use either 'between' clause or  else '>'  and '<' operators.

Something like this if you know the ranges

select * from DateTTable
where dataField between '01/01/2011' and '02/01/2011'

0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 19

Author Comment

by:erikTsomik
ID: 36576123
well it is easy with a range what if I do not have a range and the user just start typing the I wanto find all data in the database which matches this entry
0
 
LVL 8

Expert Comment

by:venk_r
ID: 36576296
First thing they will not be able to enter character in a datetime field.The reason you re getting the result is because it does  ascii conversion internally.
if ou really want to apply like operator then convert the datefield into a string before fetching.


select * from DateTTable
where convert(varchar(20),dataField) like '%%'

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 36577707
>>The 'like %%' clause are string related operators.<<
You may want to double check that.  See this section from SQL Server BOL:
Using Operators with Date and Time Data Types
The relational operators (<, <=, >, >=, <>), comparison operators (=, <, <=, >, >=, <>, !<, !>) and logical operators and Boolean predicates (IS NULL, IS NOT NULL, IN, BETWEEN, EXISTS, NOT EXISTS, and LIKE) are supported for all the date and time data types.


I am not saying it is appropriate to use in this case, but to make a blanket statement that LIKE only supports character data is plain wrong.
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 36577754
SO what is the conclusion I know that like can be used it just an interpretation of like statement but when i provide one later it returns records which should NOT be there
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 36577817
If you use it correctly, than it should be fine. As you can see in the following example:

DECLARE @dat date

SET @dat = GETDATE()

SELECT      @dat,
      CASE
            WHEN @dat LIKE '2011-08%' THEN 'GETDATE() LIKE 2011-08%'
            ELSE 'GETDATE() NOT LIKE 2011-08%'
      END,
      CASE
            WHEN @dat LIKE '2011-09%' THEN 'GETDATE() LIKE 2011-09%'
            ELSE 'GETDATE() NOT LIKE 2011-09%'
      END
0
 
LVL 8

Expert Comment

by:venk_r
ID: 36577859
You are right acperkins. May be I may  have misunderstood about the like operator usage.

But my point to wrikrsomik is
You have given the below statement

select * from DateTTable
where dataField like '%j%'

This should result all the month's data containing Jan,July and June.Anything apart from that should be filtered.
If that doesnt work at all may be try the below statement.It should the return all the months containing 'J' in it.

select * from DateTTable where month (dataField ) in(1,6,7)

 
0

Featured Post

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

626 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