Solved

Select records based on character count to the left and right of a given character

Posted on 2013-11-01
4
336 Views
Last Modified: 2013-11-01
I need to return material_id and material_descrip from a table called mmaster based on selection criteria of data in  a column called user_1.  

I need to select records where the vale in user_1 contains a ‘-‘  AND the count of characters to the left of the ‘-‘ = 5 AND the count of the characters to the right of ‘-‘ =2.  

Thanks in advance!
0
Comment
Question by:hbrady
  • 2
4 Comments
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 250 total points
ID: 39616838
Give this a whirl in SSMS..
Declare @str varchar(10) = '12345-67'

IF CHARINDEX ('-', @str,  1) = 6
	SELECT 'There are five characters before the dash'

IF LEN(@str) - CHARINDEX ('-', @str,  1)= 2 
	SELECT 'There are two characters after the dash'

IF CHARINDEX ('-', @str,  1) > 0
	begin
	SELECT LEFT(@str, CHARINDEX ('-', @str,  1) - 1) as characters_left_of_the_dash
	SELECT RIGHT(@str, LEN(@str) - CHARINDEX ('-', @str,  1)) as characters_right_of_the_dash
	end

Open in new window

0
 
LVL 15

Accepted Solution

by:
gplana earned 250 total points
ID: 39616936
I think what you want is just this:
SELECT material_id, material_descrip
FROM mmaster
WHERE user_1 LIKE '_____-__';

Open in new window


The LIKE operator returns rows that matches the string with some special chars that represents special meaning. The underscore ( _ ) char means "a single character goes here, but it could be any character". Also the percentage character ( % ) means any set of characters, but you don't need this.

So you want any record that user_1 has a value of _ _ _ _ _ - _ _ which means any-character any_character any-character any-character any-character, a hyphen, any-character, any-character. So exactly 5 characters of any kind, then a hyphen character, and then two more characters of any kind.

Try it and let me know ;)
0
 
LVL 15

Expert Comment

by:gplana
ID: 39616939
Please look at this link from Microsoft if you want to know more about LIKE operator:
http://technet.microsoft.com/en-us/library/ms174473.aspx
0
 
LVL 1

Author Closing Comment

by:hbrady
ID: 39617156
Two good solutions, one simple, one a bit more complex.  I actually ran the simple one and it gave me what I needed; however, it picked up a few records which contained multiple hyphens.  Easy enough to omit but were this a large data set I would have gone with the more complex solution.  Thank you both!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

867 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

15 Experts available now in Live!

Get 1:1 Help Now