Search unicode-characters in sql server - sql-server

I have to search all records, where the attribute BodyTxt contains the unicode \uDE8F
How can I use the character \uDE8F in a Like-Statement?

Use charindex
select * from tablename where charindex('\uDE8F',columnname)>0
OR use like
select * from tablename where columnname like '%\uDE8F%'

Related

NVARCHAR SQL type problem with Persian char

I have a problem in SQL with NVARCHAR type and Persian char 'ی' and 'ک', I have some records in a Table like:
+----- Name ------+
+----- علی ------+
When I want to select from this Table like:
select * from [Table] when Name like 'علی'
select * from [Table] when Name='علی'
select * from [Table] when Name like 'علي'
select * from [Table] when Name='علي'
It returns NULL! I found that when I use N before strings it is solved but I need to use N before parameter in SP and try this:
declare #name nvarchar(max)='علی'
select * from [Table] when Name like N''+#name
But unfortunately it is not working and I found when I assign 'علی' to the nvarchar, automatically 'ی' converted to 'ي'!!!
How can I fix that?
It seems like you're using MS SQL Server. In that case the N prefix denotes a unicode string.
Thus, you should use N'*' whenever you set a unicode string. Therefore, you should change your stored procedure like so:
declare #name nvarchar(max)=N'علی'
select * from [Table] when Name like #name
1- Search and find the best Collation and Unicode for your database. It can be different in each case.
2- If you are using a programming language, you can handle it there. When you want to save a record, you should change ی ک ه and unify them. It means you should always use the same char. In save, update and select(in where clause).
3- You can make a trigger for Insert and unify characters.
You don't need to use N before a parameter that is Nvarchar.
This will work :
declare #name nvarchar(max)='علی' -- or maybe you want '%علی%'
select * from [Table] when Name like #name

How to access data from SQL Server S.P.M.A But i searched word is spma

I have database value like S.P.M.A But I entered in text box is spma how to get value from database?
I used like operator but I did not get value
select *
from tablename
where name like '%spma%'
I used another method soundex
SELECT SOUNDEX ('spma'), SOUNDEX ('s.p.m.a')
You could try comparing the name with the dots removed:
select *
from tablename
where replace(name, '.', '') like '%spma%'

Replacing a specific Unicode Character in MS SQL Server

I'm using MS SQL Server Express 2012.
I'm having trouble removing the unicode character U+02CC (Decimal : 716) in the grid results. The original text is 'λeˌβár'.
I tried it like this, it doesn't work:
SELECT ColumnTextWithUnicode, REPLACE(ColumnTextWithUnicode , 'ˌ','')
FROM TableName
The column has Latin1_General_CI_AS collation and datatype is nvarchar. I tried changing the collation to something binary, but no success as well:
SELECT ColumnTextWithUnicode, REPLACE(ColumnTextWithUnicode collate Latin1_General_BIN, 'ˌ' collate Latin1_General_BIN,'')
FROM TableName
Or even using the NChar() function like:
SELECT ColumnTextWithUnicode, REPLACE(ColumnTextWithUnicode , NCHAR(716),'')
FROM TableName
The results are 'λeˌβár' for all three.
But if I cast the column to varchar like:
SELECT ColumnTextWithUnicode, REPLACE(CAST(ColumnTextWithUnicode as varchar(100)), 'ˌ','')
FROM TableName
the result becomes 'eßár', removing both the first character and 'ˌ'.
Any ideas to remove just the 'ˌ'?
you just need to put N before string pattern too (if you want look for unicode char):
SELECT REPLACE (N'λeˌβár' COLLATE Latin1_General_BIN, N'ˌ', '')
It is working fine by following select query as we are getting U+FFFD � REPLACEMENT CHARACTER when we bulk inserting address filled from txt to sql.
select Address, REPLACE(Address COLLATE Latin1_General_BIN,N'�',' ') from #Temp

FTS Contains string concatenate

im working on FTS and has been stuck in a problem.With respect to . How do you concatenate strings inside of a CONTAINS in SQL Server 2008?
im trying to use Contains function of FTS
and i need to pass search string either as concatenated string or return string from a scalar function. but both options are not available in FTS.
it does not allow to concatenate search string inside "Contains" Clause
select top 10 * from dbo.staging_table with (nolock)
where contains(text,N'"pakistan"'+'" Lahore"')
select top 10 * from dbo.staging_table with (nolock)
where contains(text,(select from dbo.getcityList()))
second option is again unavailable as it is expecting a string argument.
Im using this query in a view so i cant declare any variable :(
Please help me in resolving this issue
You can use table-valued function instead of a view.
CREATE FUNCTION dbo.fTestFTS (#str NVARCHAR(4000))
RETURNS TABLE
RETURN (
select top 10 *
from dbo.staging_table
where contains(*, #str)
)
SELECT * FROM dbo.fTestFTS(N'"pakistan"' + ' &' + '" Lahore"')
Note the ampersand in search string.

Search for comma values inside query?

How can I search for a value say
23,000
on a VARBINARY(MAX) filestream column in SQL Server 2008 R2 ? This won't work
SELECT * FROM dbo.tbl_Files WHERE CONTAINS(SystemFile, '%[23,000]%');
I think its just you have % and full text search uses *
select
*
from tbl_Files
Where contains(SystemFile, '"*23,000*"')
I have a full text index with phone numbers in it and this works too
select
*
from tbl_Files
Where contains(SystemFile, '0116')
SELECT * FROM dbo.tbl_Files WHERE CAST(SystemFile AS NVARCHAR) LIKE '%23,000%'
Please try this one :
select * from dbo.tbl_Files where CAST(SystemFile as int) like '%23000%'
If you have a VARBINARY datatype for a column then you should have to CAST that value , because it is stored as bianry value in table.
try this one :
SELECT * FROM dbo.tbl_Files WHERE cast(SystemFile as varchar) like '%23,000%'

Resources