0

I have to select in between | and length should be greater than one.

DECLARE @c varchar(100)

set @c = 'Leslie|Nicole|McCrory'

SELECT SUBSTRING(STUFF(@c, 1, CHARINDEX('|',@c), ''), 0, CHARINDEX('|', STUFF(@c, 1, CHARINDEX('|',@c), '')))

Result : Nicole

DECLARE @c varchar(100)

set @c = 'Leslie|N|McCrory'

SELECT SUBSTRING(STUFF(@c, 1, CHARINDEX('|',@c), ''), 0, CHARINDEX('|', STUFF(@c, 1, CHARINDEX('|',@c), '')))

Result : N

I need the result, which should be greater than one. (i.e) Leslie|N|McCrory => from this N should not be selected.

2
  • What's the expected resut? Commented Dec 28, 2015 at 6:42
  • Output result should be greater than one. Say "Leslie|N|McCrory" => N should not selected @FelixPamittan Commented Dec 28, 2015 at 6:45

1 Answer 1

1

Do you mean something like:

DECLARE @c varchar(100)

set @c = 'Leslie|N|McCrory'

SELECT SUBSTRING(STUFF(@c, 1, CHARINDEX('|',@c), ''), 0, CHARINDEX('|', STUFF(@c, 1, CHARINDEX('|',@c), '')))
WHERE LEN(SUBSTRING(STUFF(@c, 1, CHARINDEX('|',@c), ''), 0, CHARINDEX('|', STUFF(@c, 1, CHARINDEX('|',@c), '')))) > 1
Sign up to request clarification or add additional context in comments.

1 Comment

You're welcome. By the way, you may want to put the substring method in a subquery, so you only have to do it once

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.