Please forgive me as I am a bit of an sql noob. I am trying to do an insert using the following but am having a problem with the apostrophes. I have a lots of records to insert but many have the same problem.
Is there a way of escaping them?
INSERT INTO [dbo].[tb_Chefs] ([ChefHotelID], [HotelID], [ChefID],
[Position], [GroupID])
VALUES(N'b809a86e-f7f2-45b2-a240-0049f51509d7' ,N'14481', N'624',
N'Chef d'atelier', N'331')
GO
Any help much appreciated.
'Chef d'atelier' becomes 'Chef d''atelier'
Just double them up
If a character string enclosed in
single quotation marks contains an
embedded quotation mark, represent the
embedded single quotation mark with
two single quotation marks.
You can also you the ESCAPE clause. This allows you to define your own escape character.
Use something like this instead
INSERT INTO [dbo].[tb_Chefs] ([ChefHotelID], [HotelID], [ChefID], [Position], [GroupID])
VALUES("N'b809a86e-f7f2-45b2-a240-0049f51509d7'" ,"N'14481'", "N'624'", "N'Chef d'atelier', N'331'")
OR you could store the values you want to insert in a variable first and then use the variable in the SQL statement instead of the raw value.
Related
In a Presto-compatible database (AWS Athena) I have some rows that contain values in double-quotes, mixed with values without double-quotes
e.g. column "postal code" can have "00100" and 00100.
What SQL query can I issue to remove all double-quotes found, so "00100" becomes 00100?
You can use replace():
replace(postal_code, '"', '')
This can be in an update or select.
Note: This removes all double quotes. If you have some values that could have double quotes in them, then you need more complicated expressions. In my experience, though, a column named postal_code would never have double quotes so this is safe.
I'd like to generate a query like this:
select id, '=FKERES(B2;'D:\nyunyuka\[kotyog.xlsx]Munka1'!$A:$B;2;0)' as
excel_formula from table;
I learned that I have to escape the ' character using E but it seems not working. I assume that I have to escape all the \ as well. But It's getting complicated to me.
I am not sure if I understood the question correct. Hope that I did.
You need to add extra single quotes like this:
select id, '=FKERES(B2;''D:\nyunyuka[kotyog.xlsx]Munka1''!$A:$B;2;0)' as excel_formula
from table;
So, if there is a single quote ' in a string in a select clause then you have to add another one '' to make it work.
Besides doubling the quotes you can use dollar quoting instead.
So use
select id, $Q$=FKERES(B2;'D:\nyunyuka[kotyog.xlsx]Munka1'!$A:$B;2;0)$Q$ as excel_formula
from table;
Instead of
select id, '=FKERES(B2;''D:\nyunyuka[kotyog.xlsx]Munka1''!$A:$B;2;0)' as excel_formula
from table;
Where $Q$ in the above takes the place of the opening and closing quote ('). It is generally not worth it for just a couple instances but for long strings with lots of quotes it can sure comes in handy.
I have a string with value
'MAX DATE QUERY: SELECT iso_timestamp(MAX(time_stamp)) AS MAXTIME FROM observation WHERE offering_id = 'HOBART''
But on inserting into postgresql table i am getting error:
org.postgresql.util.PSQLException: ERROR: syntax error at or near "HOBART".
This is probably because my string contains single quotes. I don't know my string value. Every time it keeps changing and may contain special characters like \ or something since I am reading from a file and saving into postgres database.
Please give a general solution to escape such characters.
As per the SQL standard, quotes are delimited by doubling them, ie:
insert into table (column) values ('I''m OK')
If you replace every single quote in your text with two single quotes, it will work.
Normally, a backslash escapes the following character, but literal backslashes are similarly escaped by using two backslashes"
insert into table (column) values ('Look in C:\\Temp')
You can use double dollar quotation to escape the special characters in your string.
The above query as mentioned insert into table (column) values ('I'm OK')
changes to insert into table (column) values ($$I'm OK$$).
To make the identifier unique so that it doesn't mix with the values, you can add any characters between 2 dollars such as
insert into table (column) values ($aesc6$I'm OK$aesc6$).
here $aesc6$ is the unique string identifier so that even if $$ is part of the value, it will be treated as a value and not a identifier.
You appear to be using Java and JDBC. Please read the JDBC tutorial, which describes how to use paramaterized queries to safely insert data without risking SQL injection problems.
Please read the prepared statements section of the JDBC tutorial and these simple examples in various languages including Java.
Since you're having issues with backslashes, not just 'single quotes', I'd say you're running PostgreSQL 9.0 or older, which default to standard_conforming_strings = off. In newer versions backslashes are only special if you use the PostgreSQL extension E'escape strings'. (This is why you always include your PostgreSQL version in questions).
You might also want to examine:
Why you should use prepared statements.
The PostgreSQL documentation on the lexical structure of SQL queries.
While it is possible to explicitly quote values, doing so is error-prone, slow and inefficient. You should use parameterized queries (prepared statements) to safely insert data.
In future, please include a code snippet that you're having a problem with and details of the language you're using, the PostgreSQL version, etc.
If you really must manually escape strings, you'll need to make sure that standard_conforming_strings is on and double quotes, eg don''t manually escape text; or use PostgreSQL-specific E'escape strings where you \'backslash escape\' quotes'. But really, use prepared statements, it's way easier.
Some possible approaches are:
use prepared statements
convert all special characters to their equivalent html entities.
use base64 encoding while storing the string, and base64 decoding while reading the string from the db table.
Approach 1 (prepared statements) can be combined with approaches 2 and 3.
Approach 3 (base64 encoding) converts all characters to hexadecimal characters without loosing any info. But you may not be able to do full-text search using this approach.
Literals in SQLServer start with N like this:
update table set stringField = N'/;l;sldl;'''mess'
How can I insert single quote character to Hsqldb table? Escape character doesn't work for the problem.
There are two ways.
Use two single quotes. For example INSERT INTO T VALUES 'escap''d'
Use a Unicode string, which can contain a Unicode escape. For example INSERT INTO T VALUES U&'escap\0027d'
Both examples insert the string escap'd into the table.
The question is old, but I found the answer by fredt but it led me astray because it doesn't actually do what the original poster requested which is insert a single quote. Instead it inserts escap'd which is not what I wanted to do.
This may have been the only way in the past (Fred would know better than me), but the easiest way in hsqldb to insert a single quote now is to just make a prepared statement in java (p_insert) and set the String:
p_insert.setString(1,"'");
p_insert.executeUpdate();
This will actually put a single quote in the database, at least in version 2.3.2. To retrieve it from the database, you will need to use double single quotes in your SELECT statement.
After I run a query and view the output, for example
select * from People
My output is as follows
First Last Email
Ray Smith raysmith#whatever.itis
How would I export this data so that it looks as follows?
"Ray","Smith","raysmith#whatever.itis"
Or is there a way to do this within SQL to modify records to contain quotes?
Because when you export, it's going to include the commas anyway, right?
If the columns you're interested in are 128 characters or less, you could use the QUOTENAME function. Be careful with this as anything over 128 characters will return NULL.
SELECT QUOTENAME(First, '"'), QUOTENAME(Last, '"'), QUOTENAME(Email, '"')
FROM People
select '"'+first+'","'+last+'","'+email+'"'
from people
This is the kind of thing best done in code however, you shouldn't query for presentation.
select concat(“\"”,first,“\"”,“\"”,Last,“\"”,“\"”,Email,“\"”) as allInOne
Modifying the records to contain quotes would be a disaster; you don't use the data only for export. Further, in theory you'd have to deal with names like:
Thomas "The Alley Cat" O'Malley
which presents some problems.
In Standard SQL, you'd use doubled-up single quotes to enclose single quotes (with no special treatment for double quotes):
'"Thomas "The Alley Cat" O''Malley"'
Some DBMS allow you to use double quotes around strings (in Standard SQL, the double quotes indicate a 'delimited identifier'; SQL Server uses square brackets for that), in which case you might write the string as:
"""Thomas ""The Alley Cat"" O'Malley"""
Normally, though, your exporter tools provide CSV output formatting and your SQL statement does not need to worry about it. Embedded quotes make anything else problematic. Indeed, you should usually not make the DBMS deal with the formatting of the data.
This worked best for me
SELECT 'UPDATE [dbo].[DirTree1] SET FLD2UPDATE=',QUOTENAME(FLD2UPDATE,'''')
+' WHERE KEYFLD='+QUOTENAME(KEYFLD,'''')
FROM [dbo].[Table1]
WHERE SUBSTRING(FLD2UPDATE,1,2) = 'MX'
order by 2
If you are using MS SQL Server, try something like:
SELECT '"'||Table.Column||'"'
FROM Table
-- Note that the first 3 characters between "SELECT" and "||" are:
' " '
-- The characters are the same after "||" at the end... that way you get a " on each side of your value.