用破折号

时间:2017-07-20 23:08:32

标签: sql sql-server sql-server-2008

只是想从大家那里得到关于此事的建议。因此,我们的许多客户都在数据库中进行大量导入。现在,在一个实例上,客户端更新了一个特定的列,其中包含大约10000行。他们试图添加字段(示例如下所示)

ABC - TEST - TN - ABC D123

现在,当他们在系统中导入所有这些时,破折号基本上变成了特殊字符,不确定是怎么发生的,因为当我再次从系统导入文件时,它会显示有趣的符号,而不仅仅是' - '。现在我想批量更新列中的所有数据。我在想像

   UPDATE ABC
   SET columnname = replace(columname,''-'',''-'')

对此问题的任何想法都将不胜感激。谢谢

3 个答案:

答案 0 :(得分:1)

select ascii('thecharacter')

查找短划线的ASCII码

然后UPDATE ABC SET columnname = replace(columname,char(???),'-')

答案 1 :(得分:1)

这应该有效:

DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=koss DB_USERNAME=root DB_PASSWORD=root

建议,在做任何事之前复制你的桌子,以防万一出错:

UPDATE table SET columnname = REPLACE(columnname, 'weirdcharacter', '-')

但是,这不会将约束复制到新表中,因此不幸的是,主键或默认值等内容将会丢失。我相信有一种方法可以复制,同时保留T-SQL,但我不确定。

答案 2 :(得分:0)

看看下面的内容......

-- Create some test data where NCHAR(6) is the "odd nut" and NCHAR(45) is the kosher value...
IF OBJECT_ID('tempdb..#TestData', 'U') IS NOT NULL 
DROP TABLE #TestData;

CREATE TABLE #TestData (
    RandomString NVARCHAR(100) NOT NULL 
    );
INSERT #TestData (RandomString) 
SELECT CONCAT(N'qaz', NCHAR(6), 'wsxcde', NCHAR(45), 'plkm') UNION ALL
SELECT CONCAT(N'5tgbnhh6y6', NCHAR(6), 'wsxe', NCHAR(6), 'pl') UNION ALL
SELECT CONCAT(N'az', NCHAR(45), 'bgtfd', NCHAR(6), 'btgfyyt') UNION ALL
SELECT CONCAT(N'kmioliuj', NCHAR(6), 'drehytj', NCHAR(45), 'gtfhtr') UNION ALL
SELECT CONCAT(N'gf', NCHAR(6), 'nyjuy', NCHAR(6), '8ils') UNION ALL
SELECT CONCAT(N'jhio', NCHAR(45), 'yy', NCHAR(45), 'yj8u7k');

--====================================================================

-- Showing the REPLACE IN the form of a SELECT...
SELECT 
    td.RandomString,
    CleanString = REPLACE(td.RandomString, NCHAR(6), NCHAR(45))
FROM
    #TestData td;

------------------------------------------------------------------

-- Showing the REPLACE IN the form of an UPDATE...
UPDATE td set
    td.RandomString = REPLACE(td.RandomString, NCHAR(6), NCHAR(45))
FROM
    #TestData td
WHERE 
    CHARINDEX(NCHAR(6), td.RandomString) > 0;

------------------------------------------------------------------

-- Prove that we no longer have any NCHAR(6)'s in the values.
SELECT 
    td.RandomString, 
    x.Position,
    CharacterVal = SUBSTRING(td.RandomString, x.Position, 1),
    UnicodeNum = UNICODE(SUBSTRING(td.RandomString, x.Position, 1))
FROM
    #TestData td
    CROSS APPLY (
                SELECT TOP (LEN(td.RandomString))
                    Position = ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
                FROM 
                    sys.all_objects ao
                ) x 
WHERE 
    UNICODE(SUBSTRING(td.RandomString, x.Position, 1)) IN (6, 45);