Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Update Query

Author  Topic 

spowell
Starting Member

23 Posts

Posted - 2009-11-09 : 13:34:21
Hi All

I'm sure this is a really simple query but I need some help with it

I have a table that has " at the start and end of all text in each and every column.

eg

"Steve"
"Mark"
"" if blank
"24"

I need to remove all of the " in the entire table

Thanks

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-11-09 : 13:42:56
UPDATE t1 SET c1 = REPLACE(c1, '"', '')

This code will remove all " from the table and not just the ones at the start or at the end. Let us know if you don't want the "middle" ones to be removed.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

spowell
Starting Member

23 Posts

Posted - 2009-11-09 : 14:12:52
Thanks for your help - that's just what I needed
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-11-09 : 14:26:12
You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-11-09 : 14:58:59
Here

Go nuts


CREATE TABLE #myTemp99 (n int IDENTITY(1,1), TABLE_NAME varchar(256), COLUMN_NAME varchar(256))
GO

INSERT INTO #myTemp99(TABLE_NAME, COLUMN_NAME)
SELECT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.Columns
WHERE DATA_TYPE IN ('char','varchar', 'nchar','nvarchar')
ORDER BY TABLE_NAME, COLUMN_NAME
GO

SELECT * FROM #myTemp99
GO

SELECT SQL FROM (
SELECT 'UPDATE ' + TABLE_NAME + CHAR(13) + CHAR(10)
+ ' SET ' + COLUMN_NAME + ' = REPLACE('
+COLUMN_NAME+','+''''+'"'+''''+','+''''+''''+')' AS SQL
, TABLE_NAME, 1 AS SQLGroup, n
FROM #myTemp99 o
WHERE EXISTS (SELECT TABLE_NAME
FROM #myTemp99 i
WHERE i.TABLE_NAME = o.TABLE_NAME
GROUP BY i.TABLE_NAME
HAVING MIN(i.n) = o.n)
UNION ALL
SELECT ' , ' + COLUMN_NAME + ' = REPLACE('
+COLUMN_NAME+','+''''+'"'+''''+','+''''+''''+')' AS SQL
, TABLE_NAME, 2 AS SQLGroup, n
FROM #myTemp99 o
WHERE EXISTS (SELECT TABLE_NAME
FROM #myTemp99 i
WHERE i.TABLE_NAME = o.TABLE_NAME
GROUP BY i.TABLE_NAME
HAVING MIN(i.n) <> o.n)
UNION ALL
SELECT 'GO' AS SQL
, TABLE_NAME, 3 AS SQLGroup, 9999999 AS n
FROM #myTemp99 o
WHERE EXISTS (SELECT TABLE_NAME
FROM #myTemp99 i
WHERE i.TABLE_NAME = o.TABLE_NAME
GROUP BY i.TABLE_NAME
HAVING MIN(i.n) = o.n)
) AS XXX
ORDER BY TABLE_NAME, SQLGroup, n


DROP TABLE #myTemp99
GO





Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page
   

- Advertisement -