Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Server Compact Edition Question using PATINDEX

Posted on 2013-06-14
4
Medium Priority
?
382 Views
Last Modified: 2013-07-22
I am using the SQL Server Compact Edition and want to know how I can check for invalid characters in a table column.  Since SQL Server Compact Edition does not support character classes I cannot use this expression.

SELECT *
FROM [Addresses.txt]
WHERE PatIndex('%[^a-z0-9]%', [Addresses.txt].[PROD_CD]) > 2

Is there another way to do this using PatIndex or perhaps some other command that is supported by SQL Server Compact Edition?  The objective is to identify invalid characters in a table column.  For example;

ProductDescription
---------------------------
Black Drill Handle*      
White Phone% HeadSet
Blue Computer Case
R^ed IPhone Case& Pen

This query would need list the records that contain an invalid character so based on the data above the following would be listed becuase they contain invalid characters *,%^&

Black Drill Handle*      
White Phone% HeadSet
R^ed IPhone Case& Pen
0
Comment
Question by:hojohappy
  • 2
  • 2
4 Comments
 
LVL 11

Accepted Solution

by:
Louis01 earned 2000 total points
ID: 39247864
Only alternative I can think of (in SQL) is to find strings with non-illegal characters.

select *
from theTable
where LOWER(theColumn) not like '%a%'
or LOWER(theColumn) not like '%b%'
or LOWER(theColumn) not like '%c%'
...

Not very effective...

Perhaps a javascript would work for you?

<script language="JavaScript">
var temp = new String('¿This is a testing... of St???¿ra What.@#..');  
document.write(temp + '<br>');  
temp = temp.replace(/[^a-zA-Z 0-9]+/g,'');  
document.write(temp + '<br>');  
</script>  

Open in new window

0
 
LVL 2

Expert Comment

by:vivekkumarSharma
ID: 39247895
Add check constraint to that column.


ALTER TABLE tblName ADD CONSTRAINT CK_ConstraintName
    CHECK
(
theColumn not like '%a%'
or theColumn not like '%b%'
or theColumn not like '%c%'
)
0
 
LVL 11

Expert Comment

by:Louis01
ID: 39247984
:-o ... correction:

or LOWER(theColumn) not like '%b%'
...

should be

and LOWER(theColumn) not like '%b%'
...
0
 
LVL 2

Expert Comment

by:vivekkumarSharma
ID: 39270944
thanks for correction Louis01.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

916 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