重复从48个字段中删除引号

时间:2012-02-23 13:05:18

标签: sql sql-server-2005 quotes

下面这个脚本逐个字段地工作,并删除每个条目开头和结尾的引号,但如果你有超过3或4列,复制,粘贴更改为每个新列名称需要一个大量的时间。任何想法如何让这可能遍历每一列?

--Removing quotes back and front from fields where datetime_updated is not blank
UPDATE [Majestic].[dbo].hdiyouth_school_2
SET datetime_updated=left(right(cast(datetime_updated as nVarchar), 
    LEN(cast(datetime_updated as nVarchar))-1),
    LEN(cast(datetime_updated as nVarchar))-2)
WHERE datetime_updated IS NOT NULL AND datetime_updated LIKE '"%"'

1 个答案:

答案 0 :(得分:1)

您可以使用与此类似的方式为每列自动生成查询:

select 
'UPDATE [Majestic].[dbo].hdiyouth_school_2
SET ['+column_name+']=left(right(cast(datetime_updated as nVarchar), 
    LEN(cast(datetime_updated as nVarchar))-1),
    LEN(cast(datetime_updated as nVarchar))-2)
WHERE datetime_updated IS NOT NULL AND datetime_updated LIKE ''"%"'';'
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME='Employee' and TABLE_SCHEMA='HumanResources'