943,715 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 2186
  • MS SQL RSS
Jan 23rd, 2009
0

Problem with "like" statement

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hdmnp is offline Offline
1 posts
since Jan 2009
Jan 23rd, 2009
0

Re: Problem with "like" statement

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)
Reputation Points: 70
Solved Threads: 15
Posting Whiz
kanaku is offline Offline
378 posts
since Jan 2007
Jan 23rd, 2009
0

Re: Problem with "like" statement

If you need to use wildcard with date you need to cast it first to string, and then play with it.
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Feb 15th, 2009
0

Re: Problem with "like" statement

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
msi
Reputation Points: 10
Solved Threads: 0
Newbie Poster
msi is offline Offline
3 posts
since Nov 2007
Mar 5th, 2009
0

Re: Problem with "like" statement

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

sql Syntax (Toggle Plain Text)
  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.
Reputation Points: 12
Solved Threads: 6
Junior Poster
Geek-Master is offline Offline
156 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: SQL record count
Next Thread in MS SQL Forum Timeline: Debug stored procedure





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC