Problem with "like" statement

Please support our MS SQL advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2009
Posts: 1
Reputation: hdmnp is an unknown quantity at this point 
Solved Threads: 0
hdmnp hdmnp is offline Offline
Newbie Poster

Problem with "like" statement

 
0
  #1
Jan 23rd, 2009
Hello all.

When I run the statement:

select regdate,description,machine_id,machine_nm,status from iris_log.dbo.tb_log_info where regdate like '%23%'

I get the result:
2009-01-23 08:05:06.000
Administration Software is Closed. Operator's Name:admin

But if I do:

select regdate,description,machine_id,machine_nm,status from iris_log.dbo.tb_log_info where regdate like '%-23%'

the result is null...

why?!?!

TIA,
-hdmnp
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 276
Reputation: kanaku is on a distinguished road 
Solved Threads: 15
kanaku's Avatar
kanaku kanaku is offline Offline
Posting Whiz in Training

Re: Problem with "like" statement

 
0
  #2
Jan 23rd, 2009
Probably because the dates stored in your actual database are just that. Dates. (ie January 4, 2008; December 16, 1908; etc)

The dashes(-) you see when you retrieve results (ie SELECT date FROM tablename) are just 'formatting' or presentational characters. The same way you see pipes (|) in between columns. They aren't really part of the 'data' that gets stored in the column.

So a wildcard search for '%-%' wouldn't return anything significant. The same way searching for a pipe '%|%' in a column wouldn't return all rows.

(That's just a guess. I'm 90% sure about it)
If you know ASP, you can save other daniweb members from idiots like me by helping out in this forum.

Visit this thread
if your username starts with one of the following letters: B D F H J L N P R T X Y Z.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Problem with "like" statement

 
0
  #3
Jan 23rd, 2009
If you need to use wildcard with date you need to cast it first to string, and then play with it.
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3
Reputation: msi is an unknown quantity at this point 
Solved Threads: 0
msi msi is offline Offline
Newbie Poster

Re: Problem with "like" statement

 
0
  #4
Feb 15th, 2009
You can't work with datetime data type in the same manner as with character data types.
If you need what have asked, try this:
select regdate,description,machine_id,machine_nm,status from iris_log.dbo.tb_log_info where convert(varchar(10), regdate, 120) like '%-23%'

Functions Transact-SQL handling datetime data types
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 149
Reputation: Geek-Master is an unknown quantity at this point 
Solved Threads: 6
Geek-Master's Avatar
Geek-Master Geek-Master is offline Offline
Junior Poster

Re: Problem with "like" statement

 
0
  #5
Mar 5th, 2009
Ramy's and MSI's solutions work perfect if you have to use the LIKE clause, but DATETIME data types need to be handled differently. I would recommend not using the LIKE clause and going with the SQL function DATEPART(). If you need to retrieve records that occured on the 23rd, you can use this WHERE clause

  1. SELECT * FROM iris_log.dbo.tb_log_info
  2. WHERE DATEPART(Dd, regdate) = 23

But either methods work fine.
Last edited by peter_budo; Mar 6th, 2009 at 8:58 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
If in doubt, reach into the trash can and remove the user guide.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC