steamngn
asked on
quote marks in patindex
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
Ideas?
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')
I get 0 as expected, but I need this to be 3 as it isn't wrapped in quotes. When I doPATINDEX('%" "%','"he lp"')
it still returns 0Ideas?
try this:
select PATINDEX('% %','he lp')
don't put the string to search or patern between any quotes when in %%...
select PATINDEX('% %','he lp')
don't put the string to search or patern between any quotes when in %%...
ASKER
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:
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
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*/
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
you can do something like this
Open in new window
returns you three
the other one
Open in new window
returns you 4 as it is couning " now.