Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Special Character in SQL

I have a problem with a special character inserted in a table of SQL Server 2008 R2. The point is that when i'm trying to insert a string with the character º (e.g. 3 ELBOW 90º LONG RADIUS) in the table this appears like this: 3 ELBOW 90� LONG RADIUS, and when i'm trying to select all the rows that contains the character � the result is null.

I tried to make the select with ASCII by making this: select * from itemcode where description like '%'+char(63)+'%'

and make this to know that the ASCII of that symbol is 63:

select ASCII('�')

But that doesn't work. What i must do to select all the rows that have that character and what should i do to make that SQL recognize the character º?

Thanks

like image 339
Misayel Espinoza Avatar asked Dec 04 '12 21:12

Misayel Espinoza


People also ask

What is like %% in SQL?

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: The percent sign (%) represents zero, one, or multiple characters. The underscore sign (_) represents one, single character.

How do I allow special characters in SQL Server?

Make sure that your columns are using the type nvarchar(...), rather than varchar(...). The former is Unicode, the latter is ASCII. Also, make sure that your database default collation is set to Accent Sensitive, and that your columns are stored that way.

What is the data type for special characters in SQL?

So what is varchar in SQL? As the name suggests, varchar means character data that is varying. Also known as Variable Character, it is an indeterminate length string data type. It can hold numbers, letters and special characters.


3 Answers

The degree symbol

U+00B0 ° degree sign (HTML: ° °)

is not an ASCII character and generally requires an NVARCHAR column and a N'' string literal. (except for codepages etc that support the symbol)

63 is the code of the question mark, which is the fallback for your inverse question mark in ASCII:

select UNICODE('�') => 63
select UNICODE(N'�') => 65533

where 65533 is the Unicode Replacement Character used to display characters that could not be converted or displayed.

like image 155
devio Avatar answered Sep 20 '22 23:09

devio


when I run this:

print ascii('º')

I get 186 as the ascii code value, so try:

select * from YourTable Where Description like '%'+char(186)+'%'

to see all the ascii codes run this:

;WITH AllNumbers AS
(
    SELECT 1 AS Number
    UNION ALL
    SELECT Number+1
        FROM AllNumbers
        WHERE Number<255
)
SELECT Number,CHAR(Number) FROM AllNumbers
OPTION (MAXRECURSION 255)

EDIT op stated in a comment that they are using nvarchar columns.

forger about ascii, use NCHAR (Transact-SQL) to output a degree symbol:

print '32'+NCHAR(176)+'F' --to display it

select * from YourTable 
    Where Description like '%'+NCHAR(176)+'%' --to select based on it

and use UNICODE (Transact-SQL) to get the value:

print UNICODE('°')

returns:

176
like image 29
KM. Avatar answered Sep 16 '22 23:09

KM.


select top 10 * from table_name 
where tbl_colmn like N'%'+  NCHAR(65533) + N'%'

the function NCHAR(65533) will return the character your're looking for.

like image 42
christos_g Avatar answered Sep 20 '22 23:09

christos_g