How to include 2 single quotes in a variable in sql server - sql

Below line is returning me the value with single quote.
set #SearchStr2= upper(QUOTENAME('%''' + #SearchStr + '''%','''') )COLLATE SQL_Latin1_General_CP1_CS_AS;
Actual Output : '%'NM'%'
Expected Output : '%''NM''%' -- Need 2 single quotes before and after %

I'm not exactly sure what you are targeting. Assuming your current code's #SearchStr = 'NM'. Your current code should return your "Expected Output".
If that isn't what you're expecting, just add more single quotes till you get what you want.
Example:
SELECT UPPER(QUOTENAME('''%''' + 'NM' + '''%''','''') )COLLATE SQL_Latin1_General_CP1_CS_AS
I added more 2x single quotes before and after the %'s and that added an additional single quote on the return screen. '''%''NM''%'''

Related

4 quotes always give different result in SQL Select statement

So I've been googling for a couple of hours and I still don't understand a single thing about escaping quotes in sql.
Can somebody please explain me if '''' in sql means ' why does select '('||''''||')' give (') why not (''')?
A string in SQL must be enclosed in single quotes, e.g. 'a'
A single quote inside a SQL string is escaped by doubling it: '' - to create a string out of that, you need to enclose that in two single quotes: '''' - so the '' in the middle of that string are the same as the a in my first example.
The expression: '('||''''||')' consists of three string constants:
'(' --> (
'''' --> ' (as explained above)
')' --> )

Remove carriage return / Line feeds in string and output with quotes

I have the following SQL that outputs a string with double quotes around it:
SELECT '"'+A.DESCR+'"'
FROM MyTable
Results:
"Stackable Storage Basket"
I have found that I have carriage returns / line feeds in some of the data and want to remove any of these while retaining the quotes. I have modified the code as below, however this is adding an extra quote onto the end of the string, and not retaining the beginning quote either.
SELECT REPLACE(REPLACE(DESCR + '"'+DESCR+'"' + '''', char(10),'"'), char(13), '"')
FROM MyTable
Results:
Stackable Storage Basket"'
How do I need to modify this to get the expected behavior?

Unable to pass variable in Informix

I was able to figure out how to get connected to Avaya CMS through Informix using SQL. The below query works but when I try to replace the ''1/01/19'' with a variable, I get the following error: "EIX000: (-1205) Invalid month in date"
Code that works
select * from Openquery(CMS, 'select * FROM dagent WHERE ROW_DATE = ''1/01/19'' ');
Code that does not work
DECLARE #startDate DATETIME
SET #startDate = '2021-01-21'
select * from Openquery(CMS, 'select * FROM dagent WHERE ROW_DATE = ''+#startDate+'' ');
Does anyone have an idea what the problem could be?
The trouble is not enough single quotes.
You have:
'select * FROM dagent WHERE ROW_DATE = ''+#startDate+'' '
^^ ^^
In each case where you have two adjacent single quotes, you need a third too. The two single quotes map to one single quote, so the quoted string contains +#startDate+, not the concatenation of your variable.
You need:
'select * FROM dagent WHERE ROW_DATE = '''+#startDate+''' '
Now the first two single quotes in the triplet map to a single quote; the third terminates the string, the +#startDate+ becomes string concatenation, and then the next single quote starts a new string, the two single quotes map to one quote, and the space and single quote finish the string.
How to debug?
Assign the string you used to a variable and print it.
Assign the string I suggest to a variable and print it.

alter value in isnull

I have a stored procedure where I'm serching for part of a column if a parameter isn't null.
The problem is that the value I'm searching for is mixed up in a long string of other values. Here's an example of what a row might look like:
'...value=153139,example=xyz,param=15,morestuff=otherthings...' //ignore the actual names, it's just examples for SO
So this wont work because it would potentially select the wrong thing (value=153139would cause a match if #param = '15'):
WHERE
[column] LIKE ISNULL(#param, [column])
This wouldn't work either:
WHERE
[column] LIKE '%param=' + ISNULL(#param, [column]) + '%'
The perfect thing for me would be something like ISNULL(check_expression, replacement_if_null, replecement_if_not_null), but to my knowledge, that doesn't exist..
P.S. I know that the way the information is stored isn't optimal, but it's what the system uses and I have to work with it =/ So no comments like "You should split the column up so that it only contains one value" please
Have you try a case statement:
case when check_expression is null then replacement_if_null else replecement_if_not_null end
or
[column] LIKE '%param=' + ISNULL(#param, [column]) + ',%' -- comma added before the last %
Cast #param if it is necessary and make sure you don't have hidden space around the values (rtrim or ltrim can be used to remove them)

Replace quote in SQL Select

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.