Solved

Like command in SQL

Posted on 2010-11-30
16
465 Views
Last Modified: 2012-05-10
I want to conduct a search in SQL table using like command.
With the content is 250.xx, it is easy, like:
select count(number) from table1 where value1 like '250%'

With (410.xx-414.xx), I can use:
select count(number) from table1 where value1 like '41[0-4]%'

But I have problem conduct it on these below range:
range1 (491,491.0-491.9,492,492.0,492.8, or 496);
range2 ( 398.91,402.01,401.11,402.91,428.xx);
range3 (401.00, 401.1, or 401.9 and without range2)

Could you please help me in these ones.

Thank you very much!

0
Comment
Question by:YANKAUSKAS
[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
  • 8
  • 5
  • 3
16 Comments
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 34242578
kindly elaborate, can't really get your question.
Are these ranges in table columns, if yes then what are you trying to search for???
0
 

Author Comment

by:YANKAUSKAS
ID: 34242623
No, this range is the value inside each cell of table. Please read the like here:
http://www.sql-tutorial.net/SQL-LIKE.asp
0
 

Author Comment

by:YANKAUSKAS
ID: 34242635
I want to count all the cell (according the id) which has value range inside these value: 491,491.0-491.9,492,492.0,492.8, or 496, for example.
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 20

Expert Comment

by:BuggyCoder
ID: 34242639
could you please share some of your data with the specified ranges.
0
 

Author Comment

by:YANKAUSKAS
ID: 34242651
If it is in a consecutive range from 491 to 497 I can use  49[1-7], but its value is omitted at some point. So how can I change the like command.
Do you get my question?
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 34242679
see if it is integer or numeric data why are you not going for between clause.
like should be applied if the data is varchar. if you can do comparisons by coverting the data in numeric(signed/unsigned), one should use between and other arithmetic operators
0
 

Author Comment

by:YANKAUSKAS
ID: 34242684
Whenever is Ok, whenever the value inside these range, I will count it.
 491
491.0-491.9,
492
492.0
492.8
496

Outside, do not count, for example 492.0 and 492.8 count but 492.6 492.7 does not count.

I think that I explain my question enough to understand.
0
 

Author Comment

by:YANKAUSKAS
ID: 34242701
Yes, I have done with consecutive value, but with discrete value, I don't know how to deal with. And because the value may have some form like 492.8. AFAIK, between is only for integer, right?
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 34242704
select * from sometable where cast(float,value)=491 or cast(float,value) between 491.0 and 491.9

and so on
0
 

Author Comment

by:YANKAUSKAS
ID: 34242759
Sorry but your query didn't work. I do not want to risk y count all the value inside a range while some value may different. I still need some solution using "like" or specify the exact value instead.
Thanks
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 34242760
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34242840
first range:
select count(number) from table1
where value1 like '491' or value1 like '418.[0-9]%'
   or value1 like '492' or value1 like '492.[0-8]%'
   or value1 like '496'
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34242866
Is that what you are after?

> range1 (491,491.0-491.9,492,492.0,492.8, or 496);

It is not immediately obvious whether you wanted to include
491.02,
491.0 (exactly)
491 (exactly)
491.xx (will there be alpha?)
491.93 (491.93 is outside of 491.0 and 491.9 as a numeric range, but does 491.9 mean 491.9-anything?)
etc

You need to be very clear with your question
0
 

Author Comment

by:YANKAUSKAS
ID: 34243001
Yes, cyberwiki, you seem correct. I have done with this so far, just a little:
I just need to specify the value and connect it using or, that's all I need, it's not so complicate I think.

range1 (491,491.0-491.9,492,492.0,492.8, or 496);
select sum(something) as sumtot from table1 where diag1 = '491' or diag1 like '491.[0-9]' or diag1 = '492' or diag1 = '492.0' or diag1 = '492.8' or diag1 = '496'

range2 ( 398.91,402.01,401.11,402.91,428.xx);
select sum(something) as sumtot from table1 where diag1 = '398.91' or diag1 = '402.01' or diag1 = '401.11' or diag1 = '402.91' or diag1 like '428%'



range3 (401.00, 401.1, or 401.9 and without range2) with range2 is ( 398.91,402.01,401.11,402.91,428.xx);

This query below does not works, please help me fix it cyberwiki:


select count(distinct something) from table1 where diag1 = '401.00' or diag1 = '401.1' or diag1 = '401.9' and diag1 <> '398.91' and diag1 <> '402.01' and diag1 <> '401.11' and diag1 <> '402.91' and diag1 not like '428%'

Open in new window

0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 34243046
For range3, there is no overlap with range2, so why even include range 2? Unless range2:401.11 is included in range3:401.1.  Again, since you have removed % from most of your conditions, I don't think range3:401.1 is supposed to mean like '401.1%' so there is no overlap.

So range3 should just be
where diag1 in ('401.00', '401.1', '401.9')

You can use IN when using exact values and don't need LIKE.
0
 

Author Closing Comment

by:YANKAUSKAS
ID: 34243073
Thank you, I have done it.
0

Featured Post

Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

752 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