Search for comma values inside query? - sql-server

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%'

Related

How to convert to UTF-8 in SQL Server database?

i'm working on mssql server 2017.
i got table with 'name' nvarchar column with values that look like '\u039b \u041e \u0422 \u0422 \u0410'
(sequence of '\u0[number][number][number or letter]')
how do i convert them to the correct characters?
This is actually the same as JSON escaping.
You can use JSON_VALUE to get the value out, by creating a JSON array
SELECT JSON_VALUE('["' + t.name + '"]', '$[0]')
FROM YourTable t;
db<>fiddle

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%'

Search unicode-characters in 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%'

On SQL Server, how to find the MAX or MIN value out of a couple of variables

I just found out that the MAX() function in SQL only works on columns.
Is there a similar function I can use to find the max value out of e.g. these four variables?
SET #return = MAX(#alpha1, #alpha2, #alpha3, #alpha4)
Or do I have to put them in a column first (and thus create a table first...;-( )?
Regards
Lumpi
There is no built-in function in T-SQL for this but you can use following
SELECT #result = MAX(alpha)
FROM (SELECT #alpha1
UNION ALL
SELECT #alpha2
UNION ALL
SELECT #alpha3) T(alpha);
or (SQL Server 2008+)
SELECT #result = MAX(alpha)
FROM (VALUES(#alpha1),
(#alpha2),
(#alpha3)) T(alpha);

Resources