Solved

Like command in SQL

Posted on 2010-11-30
16
429 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
  • 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now