Replace quote in SQL Select - sql

I have a scenario in my vb.net project where users need to select a name from a combobox (or type a new name).
Names of course can have ' as in Tina O'Hara which in the database would be stored as Tina O''Hara.
My combobox is populated from a sql select command. I have tried to use a Replace so that the names display correctly in the dropdown.
SqlStr = "SELECT Replace(ContactName, '''', ''') AS ddlText FROM tbl_Visits Where CustID = " & hd_CustID.value & " ORDER By ContactName"
PopulateCMBX(cmbx_ContactName, SqlStr, "Please Select")
PopulateCMBX... This gets a list of names from the supplied SqlStr and populates the combobox itemlist with a 'Please Select' as the first option.
SqlStr is producing an error as there is not a matching set of ' how do I fix this. Thanks

When referencing single quotes in MS-SQL, you need to escape each single quote (') with two single quotes ('').
If the name is stored in the database with two single quotes, such as (''), then when you do the replace you need to use four single quotes (''''). In addition to these, you need to enclose the string with single quotes.
What this means is that your replace should look like this:
SqlStr = "SELECT Replace(ContactName, '''''', '''') ...
The 2nd param in the replace has 6 single quotes: 4 to represent the two single quotes you are replacing, and 2 to make it a string. The 3rd param has 4 single quotes: 2 to represent a single escaped quote and two to make it a string.

Try with this, maybe not the best solution but it works (check this example):
select Replace(Replace(ContactName, '''', '$&'),'$&$&','''') from tbl_Visits
Note that $& is like a key that should not affect the values ​​in your field, for example, could be a word: &&k&& or %%key%%, etc.
Back to clarify, it is not the best solution, but sometimes i've used.

Related

Using ' in Query String

I have a query that I want to be able to match strings containing '. I currently have:
QUERY(nameGen!$A$1:$G500, "SELECT D WHERE B matches '"&$B1091&"'",0)
So it's the cell reference &$B1091& that will contain the string
Got it. Needed to wrap the cell ref in 3 lots of ". So now its:
QUERY(nameGen!$A$1:$G500, "SELECT D WHERE B matches """&$B1091&"""",0)
An alternate option is to use;
"string 1"&char(34)&"string2".
Edit; looks like Google sheets uses char(39) - mssql uses char(39) to quote strings, Excel / vba uses char(34).
For other special characters if applicable check an ASCII table...

How to remove semicolon from a column and replace it with a line feed in Access with SQL?

I want to remove semicolons and create new columns to separate the years with different columns in this table:
I tried to replace it with the following code but I had this result:
This result only deletes what comes after the first semicolon and does not replace subsequent semicolons with new columns with the years, thanks for the answers:
SELECT Province, REPLACE( Annee_Guerre, ';' , CHR(13) + CHR(10))
FROM Feuil1;
You might want to consider replacing the ";" with vbCrLf in the VBA code using a recordset and a loop. Queries usually do not respond well to Cr and Lf in the strings.

insert string with " ' " to oracle

Hey I'm using oracle DB with Iron Python and I'm having trouble with strings that contains the char " ' " like in Mc'donalds. (I know it is looking for the closing ' )
The string is accepted from the user input and I want to add it to my DB as it is, meaning without omitting or changing any character.
How can I do it?
Try using the "q" (quote) function:
INSERT INTO restaurantTable (name)
VALUES (q'[O'Reilly and Conway's Irish Pub]');
You can also double-up the single apostrophes (O''Reilly and Conway''s Irish Pub). But in your case you'd have to parse them out, so either using the quote function or query parameters would work the best.
For more information: Q-quote operator introduced in Oracle 10g
Taken from PL/SQL, how to escape single quote in a string?
You can do it either using Q quoting like
q'[insert into MY_TBL (Col) values('ER0002')]';
OR you can use two quotes to denote a single quote
'insert into MY_TBL (Col) values(''ER0002'')';

Escape sequence character in sql query parameter in windows azure

When querying a string column, if the search parameter has a " ' " in it, the execution fails due to a syntax error. I tried to use an escape sequence character (if thats how its called), but it still does not work.
SELECT * FROM OpusOne.Accounts WHERE firstName like '%jose\'%'
What would be the correct way to add a quote " ' " to a string parameter ?
Thank you
Try this:-
SELECT * FROM OpusOne.Accounts WHERE firstName like '%jose''%'
Escape using another single quote.

Remove extra spaces if more than one space through vb.net Oracle query

Just looking for another way to remove any excess spaces(>1) throughout a query. I'm taking information for my Oracle query from an Excel spreadsheet so there's bound to be some excess zeroes due to user error and whatnot so that when I'm taking the information from the excel it will have 2 sometimes 3 extra spaces after some values so we won't get any records returned from Oracle when we query it through the application. I have tried Trim(ds.Tables.Item(0).Rows.Item(k).Item(i).ToString.ToUpper) but this isn't removing the extra spaces in some of the values. Is there some SQL statement I don't know of or perhaps another reason why Trim isn't working?
Edit: Was writing replace function incorrectly.
Original: string.Replace(" ", " ")
New: string = string.replace(" ", " ")
Use String.Replace(" ", String.Empty) instead. Trim only removes leading and trailing spaces.
On the Oracle side, you can use the REGEXP_REPLACE function, but you need to apply it to each column you want to remove the extra spaces from:
SELECT
REGEXP_REPLACE(myCol1, ' {2,}', ' ') AS myCleanCol1,
REGEXP_REPLACE(myCol2, ' {2,}', ' ') AS myCleanCol2,
FROM myTable
The example here looks for all occurrences of two or more spaces (that's the second parameter) and replaces them with a single space (the third parameter).
You can use REGEXP_REPLACE
Could be something like this:
regexp_replace(your_column, '\s{2,}', '')
Here is a sqlfiddle demo