Use & in SQL Server UPDATE - sql

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

Related

SQL Server 2016 how to update the json column which contains dot in the tag

How to modify the SQL Json Tag that contains dot(.) ?
Suppose my tag name is 'S.No', I cannot read it like this:
UPDATE tbl
SET col1 = JSON_MODIFY(col1, '$.S.No', 'S#')
You should surround the name with double quotes like "S.No".
UPDATE tbl SET col1 = JSON_MODIFY(col1, '$."S.No"', 'S#')

Update table to add a literal value to a column

I am trying to update/replace column values in the table with hard coded value.
value = "c:\temp\"
This:
COLUMN
file.txt
file1.txt
Should become this:
FINAL COLUMN
c:\temp\file.txt
c:\temp\file1.txt
Attempted solution:
SELECT REPLACE(t.column, t.column, 'c:\temp't.column)
FROM TABLE t
Is this correct logic? Do we have another function I can use?
Assuming Oracle:
If you want to change the values in the table permanently you can just run an update query:
update your_table
set your_column = 'C:\temp\' || your_column;
Sample SQL Fiddle
If you're using MS SQL you can do concatenation like this:
MS SQL (all versions?):
update your_table
set your_column = 'C:\temp\' + your_column;
MS SQL 2012 and later:
update your_table
set your_column = concat('C:\temp\',your_column);
Do a UPDATE statement like below in case you want to change it permanently
update table1 set [column] = 'c:\temp\' + [column];
Else, if you just want to display it that way then SELECT query should be
select 'c:\temp\' + [column] as new_col
from table1
NOTE: above code syntax is for SQL Server. Not sure since you tagged as tsql

How to include double quotes in select statement?

i have a table like
id name
10 bob
20 bill
i want to select only name column in output with double quotes
like select '"'||name||'"' from table
it is giving me the correct output but is there any other way without using concatenation ...
Thank you..
Using this you can get result with double quotes
' " ' + columnName + ' " '
Example
Query
SELECT '"'+Name+'"' , Age
FROM customer
Result
"Viranja" | 27
Create a virtual column that adds the quotes:
CREATE TABLE
....
quoted_name VARCHAR2 GENERATED ALWAYS AS ('"' || name || '"') VIRTUAL,
...
See here for more information:
http://www.oracle-base.com/articles/11g/virtual-columns-11gr1.php
this will check for any name with at least one double quote
select * from table
where name like '%"%'
If your intention is to be able to "export" the result into space or comma-delimited text file, use a view to "format" your data. You will need to this for your date columns as well.
There are two scenarios you would want to use double quotes in sql in my opinion.
Updating a string column which contains single multiple quotes in it. (you have to escape it)
updating blog contents in columns which you cant edit in "edit top 200 rows"
so, if you want to use double quotes follow this.
SET QUOTED_IDENTIFIER OFF
BEGIN
DECLARE #YourSqlStmt AS VarChar(5000) -- Declare a variable.
SET #YourSqlStmt = "ok"
PRINT #YourSqlStmt -- do your operations here
END
This saves time and you need not to escape single quotes in a existing string content of the column.

How to escape single quotes in Sybase

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

special character in sql for h2 database

I use the query below:
update ACCOUNT_EXTERNAL_IDS
set EXTERNAL_ID = 'username:vietnt'
where ACCOUNT_ID='1000000'
and EMAIL_ADDRESS='NULL'
It shows the error:
ERROR: Column "USERNAME" not found; SQL statement:
update ACCOUNT_EXTERNAL_IDS set EXTERNAL_ID=username:vietnt where ACCOUNT_ID=1000000 and EMAIL_ADDRESS=NULL [42122-147]
I use back slash \, then, the query become:
update ACCOUNT_EXTERNAL_IDS
set EXTERNAL_ID = 'username\:vietnt'
where ACCOUNT_ID = '1000000'
and EMAIL_ADDRESS='NULL'
The error is the same.
Resolved!
The escape character '\' resolve the problem.
The issue I encounter is I use ssh connect. So, it eliminates the character '\'