Single Quote Error - sql

When I insert single quote in search box and press search button it gives error like:
[Microsoft][SQL Server Native Client
10.0][SQL Server]Unclosed quotation mark after the character string ' '.

You should be using parameterized queries instead of constructing your SQL by concatenation.
This will avoid SQL Injection attacks as well as resolve any single quote issues.
The quick fix it to escape the ' by doubling it (''), but this would just be a temporary workaround and your code will still be vulnerable.

Parameterize your SQL queries. There are more serious issues than this called SQL Injection.

You need to escape single quotes, like \' as you're using single quotes to surround where-statements, like where i = 'foo', then you need to write where i = '\'' to match a single quote, or lie where i = 'it\'s a good day today'

Related

AS400 - Token "!" not valid

I'm running a SQL query using RUNSQL into a CL program. This query is a basic SELECT statement and uses the exclamation mark to concatenate strings.
For years until yesterday, it worked fine. Now, out of nowhere, I've got a SQL0104 message displaying Token '!' not valid every time I run the program.
If I run the query manually using STRSQL, it works.
Did this occur to someone ?
Best regards.
DB2's operator for string concatenation is actually the double pipe ||.
The documentation says:
Use the concatenation operator (||) to join two values of an expression into a single string. In some non-English, single-byte character sets, the || can display as !! (exclamation marks) or other special characters.
So your issue may be caused by a change in the character set of your client. Just use the standard operator, and your code will work regardless.

Using SQL escape characters in Powerbuilder inline SQL

I am trying to replace single quote characters in an inline SQL statement.
In SSMS:
SELECT REPLACE('test''test', '''', '')
Result: testtest
In Powerbuilder:
SELECT REPLACE(fieldname, '''', '')
INTO :ls_string
FROM tablename;
Result: 'Incorrect syntax near ''.
Basically, Powerbuilder complains about syntax because it doesn't know what to do with SQL's escape character. If I use Powerbuilder's escape character instead ('~'' versus '''' as the second argument for REPLACE), SQL Server complains about open quotes.
Any suggestions are appreciated.
You probably will have to either put the statement in a datawindow object (using the SQL quote escape method) which you use in the application as a datawindow or datastore OR do the SQL in a function or stored proc. If you go the function route you could call it in an imbedded SQL statement within Powerscript like your examples.
Something like
SELECT ufReplaceTwoQuotes INTO :ls FROM myTable USING SQLCA;
A third option would be to retrieve your data then do the replace within Powerscript (but then you have to wrestle with escaping the single quote within PowerBuilder).

SQL Injection Attack against escaping single quotes

I have been told that the method of escaping single quotes is easy to bypass in a sql injection attack. For example, if I were to have the line:
username='admin' and password='$password'
where the user types in "$password", and any single quote they type gets replaced by a double quote, could you give me an example command that would break this? I know the backslash character ( \ ) is used to escape a character, but I'm not sure how it would work out still.

Special character in varchar in SQL

I am inserting text from a file into a table, few of the lines have words like "you'll" or "don't". When I insert these lines as varchar in my table, I get an error saying - near "ll": syntax error. How do I overcome this?
Your single quote is being considered as the end of your string. Escape the quote that exists within your string to avoid this problem.
You need to escape your SQL statement. If you are using SQL Server, then you can use QUOTENAME to resolve this.
Use two apostrophes within apostrophe-quoted strings to insert the apostrophe:
insert into footable (foo) values('you''ll')
Thank you all for responses, since I was using sqlite3, there are inbuilt string formating functions available with the library, so I was able to use sqlite3_mprintf with %q instead of %s and it took care of single quotes.

How can you place a string into your query without using any quotation marks?

You have found a SQL injection vulnerability in a numeric field, but you need to use a string value in one of your attack payloads.How can you place a string into your query without using any quotation marks?
Use a parametrized query instead of concatenation strings. Guide to SQL Injection
Escape ' with ''
INSERT INTO Foo(A)VALUES('GEORGE''S CAT')