Solved

T SQL search instring

Posted on 2013-01-19
2
350 Views
Last Modified: 2013-01-19
I have a field containing a coded string, part of which appears like this (there is other string data in the field either side of the below examples)...

foo: 0,0,3,0;
bar: 0,0,2,0;
foo: 0,2,1,0;
bar: 0,0,0,0;
foo: 1,2,3,1;
bar: 0,1,8,0;

What I need to do is select all rows that contain a value greater than 2 in the 3rd of the 4 comma-separated numbers. In other words, to return only the following rows from the above example...

foo: 0,0,3,0;
bar: 0,0,2,0;
foo: 1,2,3,1;
bar: 0,1,8,0;

How would the query be constructed to achieve this?

I'm thinking along the lines of...

select * from mytable where
(myfield like '%foo: [0-10],[0-10],[3-10],[0-10];%') OR
(myfield like '%bar: [0-10],[0-10],[3-10],[0-10];%')


...however this is not returning the desired results i.e. it returns values that contain a 0 in the 3rd part of the comma separated number string.
0
Comment
Question by:drl1
2 Comments
 
LVL 20

Accepted Solution

by:
TheAvenger earned 500 total points
ID: 38796297
The groups for numbers should be like this:

[0-9]

This means all digits from 0 to 9. So your query would be:

select * from mytable where
(myfield like '%foo: [0-9],[0-9],[3-9],[0-9];%') OR
(myfield like '%bar: [0-9],[0-9],[3-9],[0-9];%')

If you have 10 as a number, you need to add more conditions where 10 is specified explicitly
0
 

Author Comment

by:drl1
ID: 38796322
Thanks, I got it working in that manner just before you posted. So any values above 9 have to be coded explicitly and a range can't simply adopt the syntax [0-20] or [0-100]....good to know!

Thanks
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help  needed 3 34
get column names from table in vb.net 8 47
Need help with a Stored Proc on Sql Server 2012 4 28
SQL - format decimal in a string 5 38
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

789 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