4 quotes always give different result in SQL Select statement - sql

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

Related

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

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

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.

Oracle sql make strings inside single qoutes

What I wanted is to place every string inside single qoutes even if it is delimited by a dot something like this:
Input: Hi.Hello.World
Output: 'Hi'.'Hello'.'World'
Note: Inputs can be 2 or more words delimited by a dot
You could try this:
SELECT '''' || REPLACE(string, '.', '''.''') || ''''
FROM yourTable
Demo
The idea here is we replace every dot . with dot in single quotes '.'. This covers all internal dots/quotes. Then, to handle the outside single quotes, we can concatenate them on both sides.

Using RTRIM or REGEXP_REPLACE to replace a comma with a comma space and single quote

I'm attempting to learn Oracle regexp_replace well enough to take a value stored in a table as a comma-separated string and change the comma character with a single quote followed by a comma followed by a space, followed by a single quote.
For instance, the field (CourseListT) contains course codes that look like this:
PEOE100,H003,H102,L001,L100,L110,M005,M020,M130
I want it to look like this:
'PEOE100', 'H003', 'H102', 'L001', 'L100', 'L110', 'M005', 'M020', 'M130'
I started with baby steps and found article #25997057 here that showed me how to insert spaces. So I have this working:
SELECT
regexp_replace(gr.CourseListT,'([a-zA-Z0-9_]+)(,?)',' \1\2')
FROM gradreq gr
WHERE gr.gradreqsetid = 326
AND gr.SubjectArea = 'Electives'
But nothing I do will allow me to insert those silly single quote marks.
Would it be better to learn RTRIM replace? Could somebody please help me learn how to accomplish this?
Thank you
Schelly
You can simply do it with replace. Use double single-quotes to escape a single-quote.
select '''' || replace(CourseListT, ',', ''', ''') || ''''
from gradreq

How do I correctly implement quotename in SQL Server?

Background Info:
I have a stored procedure that is populating an SSRS report. The SSRS report is ran and exported as a CSV. It is then opened as textfile and ran through a 3 party vendor application.
The output of the text book should look like this:
lid, status, i_flag,Count, pDate, iDate
62558633,"Text Value","08/16",11,"08/16","08/16"
78013526,"Text Value","",,"08/16""08/16"
My results look like this:
lid, status, i_flag,Count,pDate,iDate
19007442,"'Dir,MgmtII'",'',2,'','02/16'
17343623,'Text','',0,'11/15','02/16'
Now the code that I'm using is:
SELECT
quotename(isnull(i_flag,''''), '''') as i_flag,
isnull(lid, 0) as lid,
quotename(isnull(status,''''), '''') as status,
isnull(Count, 0) as Count,
quotename(isnull(p_Date,''''), '''') as p_Date,
quotename(isnull(i_Date,''''), '''') as i_Date
FROM
#Table
Any ideas on how I can fix this. Been stumped on this for a bit. Thanks.
If I'm understanding your question correctly (which I'm quite possibly not), I think you want:
SELECT
QUOTENAME(ISNULL(i_flag,''), '"') AS i_flag,
ISNULL(lid, 0) AS lid,
QUOTENAME(ISNULL([status],''), '"') AS [status],
ISNULL([Count], 0) AS [Count],
QUOTENAME(ISNULL(p_Date,''), '"') AS p_Date,
QUOTENAME(ISNULL(i_Date,''), '"') AS i_Date
FROM
#Table
It sounds like you have some values in fields which you wish to wrap in double quotes " for the purpose of exporting to CSV, plus in some cases the values in these fields might be NULL.
My suggestion above handles this by first using ISNULL to replace any NULL values with an empty string and then using QUOTENAME to wrap the resultant value in double quotes.
The crucial differences to your posted code are:
When using ISNULL I replace the NULL with an empty string '' instead of a string containing a single quote character '''' (two consecutive single quotes within a string represent an escaped literal single quote character)
When using QUOTENAME to wrap the values in double quotes, I specify a string containing a double quote '"' in the second parameter, instead of a string containing a single quote character ''''.
I hope that helps you - if you're still having problems, perhaps you could provide some sample rows from your #Table temp table and the output you're expecting from the query so people can help you further.
As an aside, it's not good practice to use SQL reserved keywords like status or count as column names or aliases, but if you must, I'd recommend enclosing them in brackets (i.e. [status]) for readability (especially in SSMS or any IDE with SQL syntac highlighting or colour-coding) as I have done above.