Trying to escape ! in t-sql string - tsql

how can I escape this? I get a parse error because it's reading the ! literally or something
select [text] from someDB
where contains([text], '<![CDATA[')

CONTAINS is designed to search for words. You'd be better off using a LIKE (LIKE '%<![[]CDATA[[]%') match for this.

Related

How to enter in a unicode character code in BigQuery?

Let's say I want to use the character 倀:
https://www.fileformat.info/info/unicode/char/20480/index.htm
How would I type this into a query in BigQuery with the proper escape sequence? My thought was the following, but this seems like it's not correct based on the Lexical Structure:
SELECT '倀', '\U00020480'
But obviously that's not correct as I'm getting:
How would I properly type it in with the escape code?
Consider below "hint"
SELECT '倀', CODE_POINTS_TO_STRING([00020480])
with output

How to select a word in a text SQL query?

On our PostgreSQL Database we have a field called Description. As you can guess this Description contains a lot of text and we would like to look inside this descriptions to find a certain word.
We tried contains and Charindex function but both are not working...
Any Idea how we can solve this?
Thank you very much!
Luca
You can use regular expressions with word delimiter markers:
select * from table
where description ~ ('\m' || 'yourword' || '\M');
Use ~* instead of ~ for case insensitive searches.
Note that using description LIKE '%yourword%' as #JNevill suggests will find that your word within other words as well, e.g. 'Jean-Luc Picard' LIKE '%car%' is true.

Replace characters with multi-character strings

I am trying to replace German and Dutch umlauts such as ä, ü, or ß. They should be written like ae instead of ä. So I can't simply translate one char with another.
Is there a more elegant way to do that? Actually it looks like that (not completed yet):
SELECT addr, REPLACE (REPLACE(addr, 'ü','ue'),'ß','ss') FROM search;
On my way trying different commands I got another problem:
When I searched for Ü I got this:
ERROR: invalid byte sequence for encoding "UTF8": 0xdc27
Tried it with U&'\0220', it didn't replace anything. Only by using ü (for lowercase ü) it was replaced correctly. Has to do something with unicode, but how to solve this issue?
Kind regards from Germany. :)
Your server encoding seems to be UTF8.
I suspect your client_encoding does not match, which might give you a wrong impression of what you are dealing with. Check with:
SHOW client_encoding; -- in your actual session
And read this related answers:
Can not insert German characters in Postgres
Replace unicode characters in PostgreSQL
The rest of the tool chain has to be in sync, too. When using puTTY, for instance, one has to make sure, the terminal agrees with the rest: Change settings... Window -> Translation -> Remote character set = UTF-8.
As for your first question, you already have the best solution. A couple of umlauts are best replaced with a string of replace() statements.
As you seem to know already as well, single character replacements are more efficient with (a single) translate() statement.
Related:
Replace unicode characters in PostgreSQL
Regex remove all occurrences of multiple characters in a string
Beside other reasons I decided to write the replacement in python. Like Erwin wrote before, it seems there is no better solution as combining replace- commands.
In general pretty simple, even no encoding had to benn used. My "final" solution now looks like this:
ger_UE="Ü"
ger_AE="Ä"
ger_OE="Ö"
ger_SS="ß"
dk_AA="Å"
dk_OE="Ø"
dk_AE="Æ"
cur.execute("""Select addr, REPLACE (REPLACE (REPLACE( REPLACE (REPLACE (REPLACE (REPLACE(addr, '%s','UE'),'%s','OE'),'%s','AE'),'%s','SS'),'%s','AA'),'%s','OE'),'%s','AE')
from search WHERE x = '1';"""%(ger_UE,ger_OE,ger_AE,ger_SS,dk_AA,dk_OE,dk_AE))
I am now looking forward to the speed when it hits the large table. If anyone would like to make some annotations, they are very welcome.

Sed: use found string as a variable

I'm looking for a way to replace all instances of a form:
model->variable
with
models[variable][index]
where variable can be pretty much any combination of letters and numbers, probably defined like [0-9a-Z]{4,12}.
There are hundreds of such variables in the text. I need to know exact form of found string "variable" to use it in replacement. Is there a way to "remember" the string and use it later? Or any other method / software which could help in such case?
Thanks in advance.
If you could convert "variable" to uppercase by the way, it would be awesome.
You can use things in the pattern to replace with if you enclose it in \(...\). You then use \1 to insert the thing that was captured by the first such bracket.
A naïve solution to your problem would be this:
sed 's/model->\(.*\)/models[\1][index]/' file.txt

Whats an alternative to having to use sqlite prepared statements to escape characters

I'm working on an iPhone app, which uses the SQLite database, and I'm trying to handle escape characters. I know that there is LIKE ESCAPE to handle escape characters in select statements, but in my application i have SELECT, INSERT, UPDATE actions and i really don't know how to go about handling escape characters.
if you are using sqlite3_exec() all-in-one function,
you dont need to use sqlite3_bind* functions..
just pass the string to sqlite3_mprintf() with %q token
sqlite3_mprintf("%q","it's example");
and the output string is
it''s example
Use FMDB, and then you won't have to. It has built-in parameter binding support, and that will take care of any escaping you need for you.
I believe you simply have to tell SQLite what your escape character is at the end of the SQL statement. For example:
SELECT * FROM MyTable WHERE RevenueChange LIKE '%0\%' ESCAPE '\'
The LIKE will match values such as 30%, 140%, etc. The character I used, \, could be anything.