Unable to pass variable in Informix - sql

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.

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

Escaping an apostrophe in Sqldf with R

I have a dataframe mergeTest which have a column Name with apostrophes on few values,
So I'm looping through it to divide values which denominator are contained in the table nbrToDivide
test1 <- sqldf(c(paste('UPDATE mergeTest SET Value = Value/',nbrToDivide[i],
' WHERE `Year` =',nbrToDivide$`Year`[i],
' AND UPPER(Name) = \'',nbrToDivide$Name[i],sep=""),
'SELECT * from mergeTestt'))
The problem is when the value of UPPER(Name) contains an apostrophe in it, it will interprete it and return an error.
I tried to use gsub with grepl but it adds two backslashes to my names so I dunno if there is a way to deal with it or should I just suppress the apostrophe in my two datraframes ?
Double the single quote. Here is an example:
> sqldf("select 'O''Brian' Name")
Name
1 O'Brian
Replacing one single quote with two single quotes will fix this as in :
"SELECT * FROM TableName WHERE FieldName = 'QueryString''s Value'"

how to SELECT a column which has a space in between its name

I am trying to execute a SQL query through OCCI calls in my CPP program.
I want to read 2 columns out of those one column name has a space in between. I tried enclosing the column name between ' ', " ", [ ] and nothing helped. Can experts suggest me on answering this.
Below is my code:
....
string sqlStmt = "SELECT 'REJECTED COST', APPROVED_COST FROM COST_TABLE where PART_NUM= 'PN4879-1'";
stmt = conn->createStatement(sqlStmt);
ResultSet *rset = stmt->executeQuery();
double dRejCost = 0;
double dAppCost = 0;
if(rset->next())
{
dRejCost = rset->getNumber(1);
dAppCost = rset->getNumber(2);
}
stmt->closeResultSet(rset);
conn->terminateStatement(stmt);
Error/Exception:
The error I get # dRejCost = rset->getNumber(1);:
ORA-01722: invalid number
PS: The ORACLE table has the many columns where "REJECTED COST" column header is named with a space. I don't have the privilege to ask the DB team to change the DB table name though.
Thanks in advance.
Single quotes (') denote character literals. I.e., you're selecting the string 'REJECTED COST', which, obviously, cannot be cast to a number.
In order to select a column name with a space, you should use double quotes ("). Note that they need to be escaped, as you're using them inside a c++ string, which is also denoted by double quotes:
string sqlStmt = "SELECT \"REJECTED COST\", APPROVED_COST FROM COST_TABLE where PART_NUM= 'PN4879-1'";

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.

PL/SQL Use Parameters Contains Quotation Marks

I wrote a procedure that included parameters type varchar2 and i pass from my application string is like that "'O','H','Y'" and my query is below.
select * from table_name t where t.productname in (parameter)
Query does not return any data. How to handle it?
I try yo double or three quatitions marks.
Try to set the parameter like this 'O,H,Y'
Here you can see some examples
If you are trying to pass a procedure as parameter then you should get nothing or error. You can only pass a function that returns a value as we all know. You need to add your code and samples to help you more. What exactly are you passing in ()?
-- Correct syntax to pass parameters in your case --
select * from table_name t where t.productname in ('O', 'H', 'Y')
/
To pass single quotes in an sql query, use single quotes twice. Simple!
You can have:
select * from table_name t where t.productname in ('''O''', '''H''', '''Y''')
To test this, you can use:
SELECT 'Schindler''s List' AS fname FROM dual;
and see that you can easily pass single quotes to a query.
Double quotes are not much of matter here. We can directly use double quotes inside an SQL string literal, like
SELECT 'He said, "Give me my teddy bear!"' AS dilogue FROM dual;