Is there a more efficient way to handle these replace calls - sql

I'm querying across two dbs separated by a legacy application. When the app encounters characters like, 'ü', '’', 'ó' they are replaced by a '?'.
So to match messages, I've been using a bunch of 'replace' calls like so:
(replace(replace(replace(replace(replace(replace(lower(substring([Content],1,153)) , '’', '?'),'ü','?'),'ó','?'), 'é','?'),'á','?'), 'ñ','?'))
Over a couple thousand records, this can (as you expect) is very slow. There is probably a better way to do this. Thanks for telling me what it is.

One thing you can do is implement a RegEx Replace function as a SQL assembly and call is as a user-defined function on your column instead of the Replace() calls. Could be faster. You also want to probably to the same RegEx Replace on your passed in query values.
TSQL Regular Expression

You could create a persisted computed column on the same table where the [Content] column is.
Alternatively, you can probably speed up the replace by creating a user defined function in C# using a StringBuilder. And you can even combine both of these solutions.
[SqlFunction(IsDeterministic = true, IsPrecise = true)]
public static SqlString LegacyReplace(SqlString value)
{
if(value.IsNull) return value;
string s = value.Value;
int l = Math.Min(s.Length, 153);
var sb = new StringBuilder(s, 0, l, l);
sb.Replace('’', '?');
sb.Replace('ü', '?');
// etc...
return new SqlString(sb.ToString());
}

Why not first do the same replace (chars to "?") on the string you are searching for in the app side using regular expressions? E.g. your SQL server query that was passed a raw string to search for and used these nested replace() calls will instead be passed a search string already containing "?"s by your app code.

Could you convert the strings to varbinary before comparing? Something like the below:
declare
#Test varbinary (100)
,#Test2 varbinary (100)
select
#Test = convert(varbinary(100),'abcu')
,#Test2 = convert(varbinary(100),'abcü')
select
case
when #Test <> #Test2 then 'NO MATCH'
else 'MATCH'
end

Related

WHERE clause returning no results

I have the following query:
SELECT *
FROM public."Matches"
WHERE 'Matches.Id' = '24e81894-2f1e-4654-bf50-b75e584ed3eb'
I'm certain there is an existing match with this Id (tried it on other Ids as well), but it returns 0 rows. I'm new to querying with PgAdmin so it's probably just a simple error, but I've read the docs up and down and can't seem to find why this is returning nothing.
Single quotes are only used for strings in SQL. So 'Matches.Id' is a string constant and obviously not the same as '24e81894-2f1e-4654-bf50-b75e584ed3eb' thus the WHERE condition is always false (it's like writing where 1 = 0)
You need to use double quotes for identifiers, the same way you did in the FROM clause.
WHERE "Matches"."Id" = ...
In general the use of quoted identifiers is strongly discouraged.

How to split a string using sybase query function

String str = 'ce765e1bc7:abc879:53:7011:2'
How to split the string using sybase query function to value 7011
I have working stored proc for this. But wanted to know if sybase provides any inbuilt function to do so.
If you know the position of the first character and length of the required pattern, you can use 'substring'
Syntax - substring(expression, start, length)
select substring('ce765e1bc7:abc879:53:7011:2',22,4)
If you only have string and the pattern to find but not sure about the length, you can additionally use 'charindex' and 'char_length' as shown in the example below:
BEGIN
DECLARE #stpos INT, #stlen INT
SELECT #stpos = charindex('7011', 'ce765e1bc7:abc879:53:7011:2')
SELECT #stlen = char_length('7011')
SELECT substring('ce765e1bc7:abc879:53:7011:2',#stpos, #stlen)
END

access 2007 change the first letter in a field of a table

I have a table called documents one of the fields is called location which shows the file path for the document. I need to change it from D:\........ to H:\.....
How can I do this using update in sql as the file paths vary in length and there are lots of records
You can use string helper function to achieve the same. Something like below
UPDATE documents SET location = 'H:' + Mid(location, 2, Len(location) - 2)
WHERE Left(location, 1) = 'D'
Here, Len() function returns the length of the string literal
Left() function returns 1 character from the left of the string literal
Mid() function give you substring from a string (starting at any position)
See MS Access: Functions for more information on the same.

How to Search substring in Multiple columns using Hibernate

following is my code to call the database to find the substring "ello".
String queryString = "from ContentItem where singerName = '%"+searchString+"%' OR songName = '%"+searchString+"%'";
System.out.println(queryString);
Query query = session.createQuery(queryString);
return query.list();
the string output is
from ContentItem where singerName = '%ello%' OR songName = '%ello%'
it says Unexpected Token for %. How to make this possible to search substring inside those columns?
I'm working Hibernate inside Tapestry.
the equals (=) operator does not work with wildcards. You need to use like (or ilike).
eg
SELECT * FROM table WHERE column like '%abc%';
see this document for more info.

Compare strings by their written representation - is it possible?

For example I have a column that contains string (in English - "A", "B" ) "AB1234" and I'd like to compare it to the string "AB1234" (in Russian "A", "B" ), for Example.
Is there any built-in function to achieve this?
The best way I found is to use Translate func where i enumerate all needed symbols.
You are looking for a function LOOKS LIKE.
Unfortunately there is no such function in SQL.
Instead, you can create a function-based index that casts every string to a common denominator using TRANSLATE, and search for the string:
CREATE INDEX ix_mytable_transliterate ON (TRANSLATE(UPPER(str), 'АВЕКМНОРСТУХ', 'AВEKMHOPCTYX'))
SELECT *
FROM mytable
WHERE TRANSLATE(UPPER(str), 'АВЕКМНОРСТУХ', 'AВEKMHOPCTYX') = TRANSLATE(UPPER('весна на танке'), 'АВЕКМНОРСТУХ', 'AВEKMHOPCTYX')