Replacing all doubles quotes from SQL table - sql

Im trying to remove all instances of double quotes from an entire SQL table, and replace these double quotes " " with signle quotes ' '.
Is there an efficient way to do this? thanks!

If you want to change the column names, you would need to update the table:
update tablename
select column_name = replace(column_name, '"', '')
where column_name like '%"%';

To replace double quotes with single quotes, simply do
Update table
set column=replace('"','''')
where column like '%"%'

If you just want to select that column without any double quote in it:
SELECT *,REPLACE(columnname, '"','') FROM tableName
If you want to update you column by replacing all the double quote(") then Gordon provided you the right answer.
update tablename select columnname = replace(columnname, '"', '') WHERE charindex('"',columnname)>0

Related

How to add commas to a value?

Is it possible to add to the result value inside a column commas?
I mean for example I create new value:
insert into dbo.SAPID (TEST2)
Values (110)
I want the value to be with commas='110' inside the column result set.
insert into dbo.SAPID (TEST2)
Values (char(39) + '110' + char(39))
Please clarify your request. It seems like TEST2 is an Integer and you would like to return a string with single quotes surrounding it.
To do this you can cast the value when selected and append the quote:
SELECT '''' + CAST( TEST2 AS NVARCHAR(10) ) + '''' FROM SAPID
I believe you are thinking of single quotes '' like you provided in your example above:
I want the value to be with commas='110'
In that case, you could just do something like this:
--your column in the table will need to be a string instead of int
create table dbo.SAPID (TEST2 nvarchar(10));
--insert string value with single quotes
insert into dbo.SAPID(TEST2)
VALUES ('''' + '110' + '''');
--select statement
select * from dbo.SAPID
Single quote is an escape character in SQL much like other languages.Single quotes are escaped by doubling them up, just as shown in above example.
SQL Fiddle Demo

Remove extra space from column values

I have a column in Oracle database, where I had stored concatenated values. For example,
/uk/letters/default?/uk/letters/funny_letters?
/uk/letters/letters?/library/conditionalstyle?o=3&f=11/uk/letters/funny_letters?
/uk/workinglife/viewarticle_93?/library/conditionalstyle?/uk/financialcentre/car_tax_calculator?
/uk/job-hunting/default?/partners/msn/i-resignfinctr?/uk/letters/letters?/
In between the urls, there are some spaces in between. How can I remove them?
UPDATE YourTable
SET YourColumn = REPLACE(YourColumn, ' ', '')
This is too late but will help some one.
select regexp_replace(column_name, '[[:space:]]+', chr(32)) from table_name;
After verification , you can update the table as follows;
UPDATE table_name
SET column_name = REGEXP_REPLACE (column_name ,'[[:space:]]+',' ');

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.

Alter All Column Values using TRIM in SQL

I want to modify (Remove all white spaces) all the values of Column
"Name" .
How do I use TRIM and modify all column values of Name .
Example :
Before : " rishi.ranka " After : "rishi.ranka"
Thank you very much in Advance.
Use a combination of LTRIM (Left Trim) & RTRIM (Right Trim):
UPDATE [TableName]
SET [Name]=LTRIM(RTRIM([Name]))
If the datatype of name column is varchar then don't need to use rtrim function the right side spaces will be automatically trim. use only LTRIM only.
update tablename
set name = ltrim(name)
where <condition>;
Run this see the how it trims the right spaces automatically.
DECLARE #mytb table
(
name varchar(20)
);
INSERT INTO #mytb VALUES (' stackoverflow ');
SELECT len(name) from #mytb;
SELECT ltrim(name),len(ltrim(name)) from #mytb;

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.