SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Detecting Specific Characters in a Varchar Field


Detecting Specific Characters in a Varchar Field

Author
Message
Grinja
Grinja
Right there with Babe
Right there with Babe (751 reputation)Right there with Babe (751 reputation)Right there with Babe (751 reputation)Right there with Babe (751 reputation)Right there with Babe (751 reputation)Right there with Babe (751 reputation)Right there with Babe (751 reputation)Right there with Babe (751 reputation)

Group: General Forum Members
Points: 751 Visits: 618
Hi,
I would like to return the results from Varchar field where there are spaces in the field.
Is there a way to limit results to where there are empty spaces or characters such as '_ , *, .)

Any links to string manipulation would also be appreciated.

Thanks
Andras Belokosztolszki
Andras Belokosztolszki
SSCertifiable
SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)

Group: General Forum Members
Points: 5693 Visits: 1585
You can get the location of the characters you are looking for by using patindex
e.g.

declare @a varchar(10)
set @a = 'abcd a_b'

select patindex('%[ _]%', @a)


If you need to remove things, you can use replace, substring, ...

You can read on string manipulation functions that are available in T-SQL on http://msdn.microsoft.com/en-us/library/ms181984.aspx

However, if you need something more complicated, you may way to look at CLR functions.

Regards,
Andras



Andras Belokosztolszki, MCPD, PhD
GoldenGate Software
Grinja
Grinja
Right there with Babe
Right there with Babe (751 reputation)Right there with Babe (751 reputation)Right there with Babe (751 reputation)Right there with Babe (751 reputation)Right there with Babe (751 reputation)Right there with Babe (751 reputation)Right there with Babe (751 reputation)Right there with Babe (751 reputation)

Group: General Forum Members
Points: 751 Visits: 618
Thank you very much for this, this is exactly what i'm trying to accomplish.
Hehe
$QLdb@
$QLdb@
Valued Member
Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)

Group: General Forum Members
Points: 64 Visits: 228
this is really great
terrykzncs
terrykzncs
Right there with Babe
Right there with Babe (772 reputation)Right there with Babe (772 reputation)Right there with Babe (772 reputation)Right there with Babe (772 reputation)Right there with Babe (772 reputation)Right there with Babe (772 reputation)Right there with Babe (772 reputation)Right there with Babe (772 reputation)

Group: General Forum Members
Points: 772 Visits: 134

declare @a varchar(10)
set @a = 'abcd a_b'

select patindex('%[ _]%', @a)




An alternative would be to use


declare @a varchar(10)
set @a = 'abcd a_b'
select charindex('_', @a)



I don't see a difference between the 2 functions. I posted this code as this is what I would have gone for first. Maybe someone can provide more info?
David Burrows
David Burrows
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17054 Visits: 10137
The patindex is checking for two chars _ and space

charindex('_', @a) will return position of first _ char
patindex('%[ _]%', @a) will return position of EITHER _ or space

charindex('_', @a) and patindex('%[_]%', @a) would give the same result (ie no space)

if checking for presence of _ and space then
CHARINDEX('_', @a) > 0 AND CHARINDEX(' ', @a) > 0
would be the same as
PATINDEX('%[ _]%', @a) > 0

and it would get worse if you wanted to check for the letters a to f as you would have to OR 6 CHARINDEX
or use PATINDEX('%[a-f]%', @a)


Far away is close at hand in the images of elsewhere.

Anon.


terrykzncs
terrykzncs
Right there with Babe
Right there with Babe (772 reputation)Right there with Babe (772 reputation)Right there with Babe (772 reputation)Right there with Babe (772 reputation)Right there with Babe (772 reputation)Right there with Babe (772 reputation)Right there with Babe (772 reputation)Right there with Babe (772 reputation)

Group: General Forum Members
Points: 772 Visits: 134
Ahem.....

Just did some investigating....Blink



declare @a varchar(10)
set @a = 'abcd a_b'
select charindex('cd', @a) -- matches a pattern - 'cd' is found in @a
select charindex('ce', @a) -- matches a pattern - 'ce' not found in @a
select patindex('%[zce]%', @a) -- treats each of the letters as seperate and tries to find any
--1 of them in @a
select patindex('%[ce]%', @a) -- same outcome as above



Still let me know if there is any other difference between the 2. It may help someone else reading this post.
terrykzncs
terrykzncs
Right there with Babe
Right there with Babe (772 reputation)Right there with Babe (772 reputation)Right there with Babe (772 reputation)Right there with Babe (772 reputation)Right there with Babe (772 reputation)Right there with Babe (772 reputation)Right there with Babe (772 reputation)Right there with Babe (772 reputation)

Group: General Forum Members
Points: 772 Visits: 134
Thanks David.

Didnt see your reply there as I was typing out mine. The guys here on SQL ServerCentral reply really quickSmooooth
David Burrows
David Burrows
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17054 Visits: 10137
You're welcome :-)


Far away is close at hand in the images of elsewhere.

Anon.


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search