How to update a table column data, containing ( ' ) symble in data [duplicate] - sql

This question already has answers here:
How do I escape a single quote in SQL Server?
(14 answers)
Closed 27 days ago.
I am getting error in microsoft sql server because the data I want to update is contains (') symbol in the data.
update Employee_table
set
brokername='Angie's Broker',
where ID=7788;
Please someome resolve the problem.

For single quotes used inside a string literal, you put another single quote before it. This is called "escaping" the single quote, and different sorts of escaping are quite usual in programming:
update Employee_table
set
brokername='Angie''s Broker',
where ID=7788;

UPDATE Employee_table SET brokername = 'Angie''s Broker' WHERE id = 7788;
To update a table column that contains the single quote symbol ('), you can use the escape sequence ('') to represent the single quote in the data.

Another way to escape the single quote is to use the unicode char like below :
update Employee_table
set brokername='Angie' + CHAR(39) + 's Broker',
where ID=7788;

Related

SQL Update table rows where the is a specific value

I have the following table in SQL server.
It is called ScenarioData and I am using the data specifically the fieldValue column to append this data into a form. I have achieved this functionality but unfortunately, the form requires a start date that has to be the present-day or up to 30 days in the future. Because I am storing data in the database as soon as the date passes these values are redundant. I have a stored procedure that selects all values from this table based on the scenarioDataId.
I was thinking to ensure that the date is always viable I could add to this stored procedure to update the relevant rows (coverStartDateDay, coverStartDateMonth, coverStartDateYear) with the current date so that the value will always be accepted.
I have proceeded the following SQL
UPDATE dbo.ScenarioData
SET ScenarioData.FieldValue = DAY(CURRENT_TIMESTAMP)
WHERE ScenarioData.FieldName = "CoverStartDateDay";
This I had hoped would append the current day to the rows in feilfValue wherein the column fieldName it equals the value coverStartDateDay. Unfortunately, I get an error saying CoverStartDateDay is not a column. Firstly where am I going wrong and secondly how can I achieve the functionality I desire?
Thanks in advance
Use single quote :
WHERE ScenarioData.FieldName = 'CoverStartDateDay';
In SQL Server double quotes are considers as column name. However, Double quotes have different usage depending on the setting QUOTED_IDENTIFIER.
By default QUOTED_IDENTIFIER is enabled (ON) & you can't use it to enclose literal strings.
For instance, if you want to go with double quotes then you need to disable QUOTED_IDENTIFIER.
SET QUOTED_IDENTIFIER OFF
Try this as varchar or string values should be passed in the single quote, not in the double quote.
UPDATE dbo.ScenarioData
SET ScenarioData.FieldValue = DAY(CURRENT_TIMESTAMP)
WHERE ScenarioData.FieldName = 'CoverStartDateDay'
Double quotes are for identifiers. Somehow if you want to pass values which contain a single quote, then you can use '' i.e., two times a single quote not the double quote.

SQL Searching for results with apostrophes

I am trying to automate a problem at work and have an SQL question. I am getting a list from one of our vendors strips all of the apostrophes out of the info.
So when I search for "oneil" and my database has "o'neil"
select * from db where name = "oneil"
I know how to fix that by hand, but how would I make it work when I don't know where the apostrophe is at?
I hope I explained that right.
You could first strip the apostrophes from the name before doing the comparison:
SELECT *
FROM db
WHERE REPLACE(name, '''', '') = 'oneil';
Demo
Note that in most versions of SQL, a literal apostrophe is represented inside a string literal using two apostrophes doubled-up ''.
For SQL Server, use a temp table with apostrophes replaced:
select *,
derived_name = REPLACE(t.name,'''','')
into #temp
from table t
Then you can do: select * from #temp where derived_name='oneil'
Note that the apostrophe is also the escape character in sql server.

Postgresql escape single quote in where clause [duplicate]

This question already has answers here:
Insert text with single quotes in PostgreSQL
(8 answers)
Closed 3 years ago.
so I am trying to run a script like this one:
select id
from owner
where owner.name = "john's"
and I am getting this error: ERROR: column "john's" does not exist.
Also I tried like this: where owner.name = 'john\'s', but it dit not work
Anyone knows how I can run a query like this one?
You can escape single quotes when you double them.
For example:
= 'john''s'
Try this
select id
from owner
where owner.name = (E'john\'s')::text
Update:
we can escape most of the characters using this statement
select id
from owner
where owner.name = (E'john\character you want to escape's')::text

SQL: Update column value conditioned on same column value

On a SQLite database something I thought was very simple doesn't work at least under my conditions.
I have one column with names and some name contains apostrophe ('), which I want to remove. I know all names which contains an apostrophe, so I am not trying to query for apostrophes. I am doing something much simpler:
UPDATE table SET column_name="name surname1 surname2" WHERE column_name="name surname1'surname2";
which doesn't return what I expect. It doesn't produce an error but it doesn't modify any record.
SQL doesn't like reflexivity?
There should be no issue with querying the current value of a column to update the same column. Try escaping the single-quote by doubling it e.g. ''.
See: http://www.sqlite.org/lang_expr.html which reads:
A string constant is formed by enclosing the string in single quotes
('). A single quote within the string can be encoded by putting two
single quotes in a row - as in Pascal.
Therefore, your update should be:
UPDATE table SET column_name='name surname1 surname2' WHERE column_name='name surname1''surname2'
Update: Added explanation of escape mechanism.

Ways to escape single quotes in SQL 'LIKE' command

What are the various ways to ESCAPE single quotes(') in the SQL LIKE command?
One way is to put two single quotes whenever you have to escape a single quote.
Can you people suggest something?
Databases: SQL Server 2005 and Oracle 10g
You already have the answer. You have to use two single quotes:
select * from table where field like '%''something''%'
Two single quotes is the best solution.
Alternatively, you can use a CHAR(39) to represent a single quote character.
UPDATE Employee SET LastName = 'O' + CHAR(39) + 'Brien'
WHERE ID=1;
The best way is to bind the parameter with ADO or ADO.NET.
Like (example in C# with ADO.NET):
SqlCommand x = new SqlCommand(sqlConnection, #"select * from table where col like '%'+#para1+'%'");
x.parameters.add(new SqlParameter("#para1",sqltype.varchar,100)).value = "this is a' test";
In SQL Server 2005 you escape a single quote (') with a double single quote ('') if you do not want to bind:
select * from table where col like '%this is a'' test%'
If you want to search the value Allen O'Dea following is the query.
SELECT * FROM [TABLE] WHERE [COLUMN] LIKE '%Allen O''Dea%'
This query works perfectly.
There is also the "Q-quote" method:
select * from mytable where text like q'#%Here's some text%#';
This is available since Oracle 10.2.
I used a '#' character as the quote delimiter, but you can use pretty much any character that won't appear in the string (there are a few exceptions, such as the space character).
In a simple example like that above I probably wouldn't do this. I'd just double up the single quotes, but it does come in handy when building large dynamic SQL statements that include lots of string literals.