[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

quote marks in patindex

Posted on 2013-05-24
5
Medium Priority
?
462 Views
Last Modified: 2013-05-25
Ok gurus,

I have a string that may or may not has a space in it. If it DOES have a space, it needs to be wrapped in quotes, IE:
he lp needs to be "he lp"
now if I do
PATINDEX('%" "%','he lp')

Open in new window

 I get 0 as expected, but I need this to be 3 as it isn't wrapped in quotes. When I do
PATINDEX('%" "%','"he lp"')

Open in new window

it still returns 0
Ideas?
0
Comment
Question by:steamngn
[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
  • 2
  • 2
5 Comments
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39195506
are you looking for the position of a space, then you need not put those quote around..

you can do something like this

select PATINDEX('% %','he lp')

Open in new window


returns you three
the other one
select PATINDEX('% %','"he lp"')

Open in new window


returns you 4 as it is couning " now.
0
 
LVL 40

Expert Comment

by:lcohan
ID: 39195519
try this:

select PATINDEX('% %','he lp')

don't put the string to search or patern between any quotes when in %%...
0
 

Author Comment

by:steamngn
ID: 39196456
Ok guys,
After re-reading this, I think I confused everyone... Sorry. What I need to do is this:
I have a varchar column of data, and in that column are strings that may or may not have special characters such as a space or backslash:
DATA
---------
horse
flying monkey
"flying squirrel"
pol"ar be"ar

Open in new window

Now with the above, I need to check if the special character exists, and if it DOES then the character(s) must also be wrapped in double quotes. So in the example above:
DATA
---------
horse              /*returns good, no special characters*/
flying monkey      /*fails, has space and not wrapped in quotes*/
"flying squirrel"  /*returns good, has space and is wrapped in quotes*/
pol"ar be"ar       /*also returns good, has space and is wrapped in quotes*/

Open in new window

So what I need to do is really two parts; first check to see if the special characters exist, and then if they do check to see that there are quotes on either side of these characters. Perhaps PATINDEX to look for the characters, and then CHARINDEX to get a position for each quote mark?
0
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 2000 total points
ID: 39196542
in this case patindex will not help you entirely, there are two sure shot ways  to identify this pattern.

1) using recursive CTE
2) Using while loop

Internally SQL Server uses almost similar paln for both of them, I am using the while loop in order to get this done
you can convert the below code into a sql function in order to use it in your code....

declare @s varchar(1) -- special charecter
set @s = ' ' 
declare @input varchar(100) -- input to the function
set @input = 'flyi"ng mo"nkey'
declare @scPosition INT -- special charecter position
set @scPosition = 0
declare @Result	VARCHAR(10)
set @Result = 'False'
declare @dqPosition1 int,@dqPosition2 int
set @dqPosition1 = 0
set @dqPosition2 = 0
while (CHARINDEX(@s,@input,@scPosition+1) <> 0)
BEGIN
	SET @scPosition = CHARINDEX(@s,@input,@scPosition+1)
	SET @Result = 'False'
	WHILE (CHARINDEX('"',@input,@dqPosition1+1) <> 0)
	BEGIN
		SET @dqPosition1 = CHARINDEX('"',@input,@dqPosition1+1)
		SET @dqPosition2 = CHARINDEX('"',@input,@dqPosition1+2)
		SELECT @Result = 'TRUE' WHERE @scPosition BETWEEN @dqPosition1 AND @dqPosition2
		SELECT @dqPosition1 = CASE @dqPosition2 WHEN 0 THEN @dqPosition1 ELSE @dqPosition2 END
	END
END
SELECT @Result

Open in new window

0
 

Author Closing Comment

by:steamngn
ID: 39196970
Yup,
I was sliding in this direction as I was fiddling with it, but you beat me to it so kudos, points and all the bragging rights that go with it! :)
This is actually part of an RFC 5322 email validation function; there are other while loops needed anyway, so your solution fit nicely.
Andy
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

650 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