How to escape single quotes in Sybase - sql

I come from MySQL and the below query doesn't work in Sybase. How should I escape single quotes?
UPDATE Animals SET NAME = 'Dog\'s friends' WHERE uid = 12

If working with Sybase, having got used to MySQL which more database users have experience you may soon discover you are unable to escape single quotes with backslash in.
So how do you escape quotes in Sybase? In fact, in Sybase SQL the single quote acts as the escape character.
See below for an example UPDATE statement in both “languages”:
MySQL
UPDATE Animals SET NAME = 'Dog\'s friends' WHERE uid = 12
Sybase
UPDATE Animals SET NAME = 'Dog''s friends' WHERE uid = 12
I’m not entirely sure this makes sense to me (especially as it looks like a double quote) but there you go!

You can create a custom function to escape quotes :
CREATE FUNCTION "ESCAPE_QUOTES"(in a_string long varchar)
returns long varchar
begin
return replace(a_string, '''', '''''');
end

Related

insert Single Quotation Marks in sql

I want to insert " exec e_Report.dbo.FTX_FA_Aging_Report_sp 'FACloedAgingReport' " in AlertSQL columns in my existing table.
How will be my update script?
My script for update is as below:
update dbo.F_ALERT
set AlertSQL= 'exec e_Report.dbo.FTX_FA_Aging_Report_sp '''FACloedAgingReport''
where AlertID=9330
Some how its not giving me expected result.
Thanks in Advance !!
Check your quote positioning. You need a pair of single quotes at either end of the quoted string, as well as the quotes for the whole query.
'exec ee_Report.dbo.FTX_FA_Aging_Report_sp ''FACloedAgingReportSee'''
Currently you have three before and two after, it should be the other way around as per Gordon's answer.
See https://support.microsoft.com/en-us/kb/178070
You need more quotes and things:
update dbo.F_ALERT
set AlertSQL= 'exec e_Report.dbo.FTX_FA_Aging_Report_sp ''FACloedAgingReport'''
where AlertID = 9330 ;
It is a bit confusing, but '''' is a string with a single quote. Two single quotes together are a single single quote. Hence:
'exec e_Report.dbo.FTX_FA_Aging_Report_sp ''FACloedAgingReport'''
^ starts a string
------------------------------------------^ two quotes are single quote in the string
--------------------------------------------------------------^ two quotes are single quote in the string and then one more to end the string itself

How do I remove single quotes from a table in postgresql?

I searched around quite a bit, it would be great if someone could link me to a solution or answer my query.
The thing is I have a postgresql table that contains a lot of single quotes and I cant figure out how to get rid of them, because obviously this
update tablename set fieldname= NULL where fieldname=' ;
wont work.
Better use replace() for this:
UPDATE tbl SET col = replace(col, '''', '');
Much faster than regexp_replace() and it replaces "globally" - all occurrences of the search string. The previously accepted answer by #beny23 was wrong in this respect. It replaced first occurrences only, would have to be:
UPDATE tbl SET col = regexp_replace(col, '''', '', 'g');
Note the additional parameter 'g' for "globally". Read about string functions in the manual.
Aside: the canonical (and SQL standard) way to escape single quotes (') in string literals is to double them (''). Using Posix style escape sequences works, too, of course. Details:
Insert text with single quotes in PostgreSQL
update tablename set fieldname= NULL where fieldname='''' ;
or
update tablename set fieldname= NULL where fieldname=E'\'' ;
insert into table1(data) values ($$it's a string, it's got some single quotes$$)
Use $$ before and after the string. It will insert data.

Find reserved keywords in TSQL

I am not sure if there is any built-in function in sql server 2008 that will tell whether it is reserved keyword or not.
The reason I wanted to do this is because I find sometimes the column names are using the same name as the reserved keywords, for example, a column called 'desc', 'user', 'state', etc, which then we have to wrap them with square brackets ([desc], [user], [state]) to be able to query the columns correctly.
If such a built-in function does exist, then we probably can do
if isReservedKeyword (#name) = true
set #column = REPLACE(#column, #name, '[' + #name+ ']')
else
set #column = #name
Reserved words are documented here:
http://msdn.microsoft.com/en-us/library/ms189822.aspx
That list is exhaustive, but it's not so long that you couldn't just re-enter those into your own database table to check against.
There is a built in function that will take care of this, and also 'unusual' characters: QUOTENAME
:
Returns a Unicode string with the
delimiters added to make the input
string a valid SQL Server delimited
identifier.
The following example takes the
character string abc[]def and uses the
[ and ] characters to create a valid
SQL Server delimited identifier.
SELECT QUOTENAME('abc[]def')
Here is the result set.
[abc[]]def]
(1 row(s) affected)
Notice that the right bracket in the
string abc[]def is doubled to indicate
an escape character.
Just put brackets around every column. That way you ensure that even reserved words are taken care of.

Use & in SQL Server UPDATE

I am trying to run a query like
UPDATE myTable SET Name='B&L' WHERE ID = 1;
The problem is I am getting the following error:
Unclosed quotation mark after the character string 'B'.
My column is of type varChar and as you can see I am escaping the string with 'quotes'. How can I get the & symbol to insert?
Thanks.
For MSSQL try escaping the & by enclosing it in square brackets so it would become:
UPDATE myTable SET Name='B[&]L' WHERE ID = 1;
Or you could use the ESCAPE statment like so:
UPDATE myTable SET Name='B\&L' WHERE ID = 1 ESCAPE '\';
It seems like your statement should work in SQL Server. I saw some instances of Oracle that had issues with ampersands. In those cases, you could use something like this:
UPDATE myTable SET Name='B&' + 'L' WHERE ID = 1

select * from table_name where column like '&nbsp'

I need to find records containing html code such as '&nbsp' But when I try to run the select * from table_name where column like '&nbsp%'
I got prompt asking for the value of nbsp. I guess the database thinks that nbsp is a parameter. I am wondering if the is an escape character so that I can tell the database that "&" is part of my query string. I tryed '\&nbsp' but didn't work.
My environment is Oracle 9i with sqlplus client.
Thanks.
Have a look at this:
SQL Plus FAQ
e.g.
SET ESCAPE '\'
SELECT '\&abc' FROM dual;
Easier way:
SET DEFINE OFF
See:
SET DEFINE
The backslash should work, but I think you need to start your query with
SET ESCAPE ON
In PL/SQL, you would use:
BEGIN select <Column> from <Table_name> into <Variable> where <Column> LIKE '\&nbsp\%' ESCAPE '\'; END
/
Resources:
Wilcards in SQL on PSOUG.org
LIKE Condition in Oracle® Database SQL Language Reference 11g Release 2 (11.2)