How do I select rows where a varchar column contains any characters that SQL Server considers a symbol? The symbol can be anywhere in the string.
Is there a SQL Server equivalent of the C# char.IsSymbol()
function?
How do I select rows where a varchar column contains any characters that SQL Server considers a symbol? The symbol can be anywhere in the string.
Is there a SQL Server equivalent of the C# char.IsSymbol()
function?
Assume for the moment that your definition of "symbol" is any non-alphanumeric character.
You need to use SQL Server's LIKE
functionality.
This will find rows containing non-alphanumerics at the start, end, and anywhere, respectively.
Select * from MyTable where MyColumn like '[^a-zA-Z0-9]%' -- Starts With
or
Select * from MyTable where MyColumn like '%[^a-zA-Z0-9]' -- Ends With
or
Select * from MyTable where MyColumn like '%[^a-zA-Z0-9]%' -- contains
The performance of the last one is exceptionally bad.
References
C#
U can use Indexof()
to search for first occurence of symbol car
and compare with 0 to check whether it is present at start of column value.
U can use LastIndexof()
to search for last occurence of symbol car
and compare with inputstring.Length-3 to check wheteher car is present at end. Here inputstring is the column value.
SQL Server
Use CHARINDEX()
function to find position for occurence of symbol car
Select * from Table
where CHARINDEX('car',column_name)=1 or CHARINDEX('car',column_name) = LEN(column_name)-3
Note : The starting position returned by CHARINDEX() is 1-based, not 0-based.