| | |
Problem with "like" statement
Please support our MS SQL advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2009
Posts: 1
Reputation:
Solved Threads: 0
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
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
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)
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.
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.
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
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
•
•
Join Date: Nov 2007
Posts: 3
Reputation:
Solved Threads: 0
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
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
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
But either methods work fine.
sql Syntax (Toggle Plain Text)
SELECT * FROM iris_log.dbo.tb_log_info 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.
![]() |
Similar Threads
- PHP Mysql "Where" statement (PHP)
- wildcards in "if then" statement? (Shell Scripting)
- Function being "skipped" in switch statement (C++)
- "use" statement inside an eval: problem? (Perl)
- dynamic "use" (Perl)
- problem with "for" statement (C++)
- Syntax error in "like" in sql statement (ASP.NET)
Other Threads in the MS SQL Forum
- Previous Thread: SQL record count
- Next Thread: Debug stored procedure
| Thread Tools | Search this Thread |






