dbTalk Databases Forums  

Search for specific length string in column

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Search for specific length string in column in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
mike@mcarlson.net
 
Posts: n/a

Default Search for specific length string in column - 09-14-2007 , 09:19 AM






SQL 2000.

I need a query that will search a field in the table that is 14
characters long and begins with a number.

I have a client that was allowing people to store credit card numbers,
plain text, in an application. I need to rip through the table and
replace every instance of credit card numbers with "x" and the last 4
digits. I got the replace bit going, but I am stuck on how to search
for a string in a field of a specific length.

Any ideas?

Thanks,
--Mike


Reply With Quote
  #2  
Old   
Jack Vamvas
 
Posts: n/a

Default Re: Search for specific length string in column - 09-14-2007 , 12:58 PM






select * from MyTable where len(reference) = 2 and
SUBSTRING (reference ,1 ,1 ) IN ('0','1','2','3','4','5','6','7','8','9')

you could expand and use the ISNUMERIC()


--

Jack Vamvas
___________________________________
Need an IT job? http://www.ITjobfeed.com




<mike (AT) mcarlson (DOT) net> wrote

Quote:
SQL 2000.

I need a query that will search a field in the table that is 14
characters long and begins with a number.

I have a client that was allowing people to store credit card numbers,
plain text, in an application. I need to rip through the table and
replace every instance of credit card numbers with "x" and the last 4
digits. I got the replace bit going, but I am stuck on how to search
for a string in a field of a specific length.

Any ideas?

Thanks,
--Mike




Reply With Quote
  #3  
Old   
Hugo Kornelis
 
Posts: n/a

Default Re: Search for specific length string in column - 09-14-2007 , 01:54 PM



On Fri, 14 Sep 2007 07:19:25 -0700, mike (AT) mcarlson (DOT) net wrote:

Quote:
SQL 2000.

I need a query that will search a field in the table that is 14
characters long and begins with a number.
Hi Mike,

WHERE LEN(YourColumn) = 14
AND LEFT(YourColumn, 1) LIKE '[0-9]'

Quote:
I have a client that was allowing people to store credit card numbers,
plain text, in an application. I need to rip through the table and
replace every instance of credit card numbers with "x" and the last 4
digits. I got the replace bit going, but I am stuck on how to search
for a string in a field of a specific length.
Are you sure you need to check only the first character for numeric? All
my credit cards have only numbers. To test for length 14 and only
numbers, you can change the above to

WHERE LEN(YourColumn) = 14
AND YourColumn NOT LIKE '%[^0-9]%'

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis


Reply With Quote
  #4  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Search for specific length string in column - 09-14-2007 , 04:38 PM



Hugo Kornelis (hugo (AT) perFact (DOT) REMOVETHIS.info.INVALID) writes:
Quote:
Are you sure you need to check only the first character for numeric? All
my credit cards have only numbers. To test for length 14 and only
numbers, you can change the above to

WHERE LEN(YourColumn) = 14
AND YourColumn NOT LIKE '%[^0-9]%'
And my two credit-cards have 16-digit numbers...


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
  #5  
Old   
Hugo Kornelis
 
Posts: n/a

Default Re: Search for specific length string in column - 09-15-2007 , 03:25 AM



On Fri, 14 Sep 2007 21:38:51 +0000 (UTC), Erland Sommarskog wrote:

Quote:
Hugo Kornelis (hugo (AT) perFact (DOT) REMOVETHIS.info.INVALID) writes:
Are you sure you need to check only the first character for numeric? All
my credit cards have only numbers. To test for length 14 and only
numbers, you can change the above to

WHERE LEN(YourColumn) = 14
AND YourColumn NOT LIKE '%[^0-9]%'

And my two credit-cards have 16-digit numbers...
Heh! So has mine - I didn't even think of that while posting my reply.

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis


Reply With Quote
  #6  
Old   
--CELKO--
 
Posts: n/a

Default Re: Search for specific length string in column - 09-16-2007 , 12:20 AM



Quote:
I have a client that was allowing people to store credit card numbers in plain text, in an application. I need to rip through the table and replace every instance of credit card numbers with "x" and the last 4 digits.
You might want to learn the difference between a field and column
before you write anymore SQL -- like what a constraint is. I assume
that youmeant 16 digits, broken into groups of 4 digits.

UPDATE Foobar
SET creditcard_nbr
= 'xxxx-xxxx-xxxx-" + SUBSTRING( creditcard_nbr,13, 16);

Having done this, put all of this in the DDL. Mop the floor,but fix
the leak!!



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.