SQL将一部分列复制到一个新列中

时间:2014-04-30 23:51:10

标签: sql sql-server tsql

我继承了一个设置不佳的数据源。鉴于目前的数据结构:

PreAddress                                          City    County
12312 Osprey Drive NW Gig Harbor                    NULL    NULL
12312 Osprey Drive NW Gig Harbor                    NULL    NULL
3022 SW Bradford St Seattle                         NULL    NULL
3022 SW Bradford St Seattle                         NULL    NULL
4605 Prestwick Lane SE Olympia                      NULL    NULL
921 129th Street Court East Tacoma Auburn/Pierce    NULL    NULL

我需要从PreAddress列中删除City名称,并将其转储到City列中,如下所示:

PreAddress                                          City                    County
12312 Osprey Drive NW                               Gig Harbor              NULL
12312 Osprey Drive NW                               Gig Harbor              NULL
3022 SW Bradford St                                 Seattle                 NULL
3022 SW Bradford St                                 Seattle                 NULL
4605 Prestwick Lane SE                              Olympia                 NULL
921 129th Street Court East                         Tacoma Auburn/Pierce    NULL

那里的任何SQL专家都知道如何编写脚本吗?

更新

First Pass SQL:

USE [SMS]
GO

IF OBJECT_ID('tempdb..#tmpCitiesCounties') IS NOT NULL
    DROP TABLE #tmpCitiesCounties
GO

IF OBJECT_ID('tempdb..#tmpCityCleanup') IS NOT NULL
    DROP TABLE #tmpCityCleanup
GO

CREATE TABLE #tmpCitiesCounties
    ( [ccId] INT IDENTITY(1, 1)
                 PRIMARY KEY
    , [City] VARCHAR(50) NOT NULL
    , [County] VARCHAR(50) NOT NULL );

CREATE TABLE #tmpCityCleanup
    ( [Id] INT NULL
    , [Address] VARCHAR(64) NULL
    , [City] VARCHAR(50) NULL
    , [County] VARCHAR(50) NULL );

INSERT  INTO [#tmpCitiesCounties]
        ( [City], [County] )
VALUES  ( 'Battle Ground', 'Clark' ),
        ( 'Camas', 'Clark' ),
        ( 'La Center', 'Clark' ),
        ( 'Ridgefield', 'Clark' ),
        ( 'Vancouver', 'Clark' ),
        ( 'Washougal', 'Clark' ),
        ( 'Yacolt', 'Clark' ),
        ( 'Fircrest', 'Pierce' ),
        ( 'Gig Harbor', 'Pierce' ),
        ( 'Unincorporated', 'Skagit' ),
        ( 'Arlington', 'Snohomish' ),
        ( 'Bothell/Snohomish', 'Snohomish' );

INSERT  INTO [#tmpCityCleanup]
        SELECT  [SNPR].[Id]
              , REPLACE(LOWER([SNPR].[PreAddress]), LOWER([TCC].[City]), '') AS [Address After]
              , [TCC].[City]
              , [TCC].[County]
        FROM    [dbo].[SellerNetProceedsResult] AS SNPR
                LEFT JOIN [#tmpCitiesCounties] AS TCC
                    ON [TCC].[City] = RIGHT(LOWER([SNPR].[PreAddress]), LEN(LOWER([TCC].[City])))
        ORDER BY [SNPR].[Id] DESC

SELECT  [TCC1].[Id]
      , [TCC1].[Address]
      , [TCC1].[City]
      , [TCC1].[County]
FROM    [#tmpCityCleanup] AS TCC1

所以,这一块SQL正确地撕掉了东西(城市和县的临时表被截断了,因为有很多东西比我想要放在这篇文章中),但就像上面那行的情况一样它有“Tacoma Auburn / Pierce”,上面的SQL在删除“Auburn / Pierce”之后留下了“Tacoma”。

如果我然后运行以下sql代码,我在地址上的两个表之间无法识别:

SELECT  [TCC1].*
      , REPLACE([TCC1].[Address], [TCC2].City, '') AS [Address After]
      --, [TCC2].[City]
      --, RIGHT([TCC1].[Address], LEN([TCC2].[City]))
FROM    [#tmpCityCleanup] AS TCC1
        left JOIN [#tmpCitiesCounties] AS TCC2
            ON [TCC2].[City] = RIGHT([TCC1].[Address], LEN([TCC2].[City]))

相反,“Address After”列只是null。

Id      PreAddress                              City            County  Address After
151     12312 osprey drive nw Gig Harbor        Algona          King    NULL
150     12312 osprey drive nw                   Gig Harbor      Pierce  NULL

也许我错过了什么。

3 个答案:

答案 0 :(得分:2)

您需要有一个可能的表名的查找表。在我的头顶,可能是这样的:

UPDATE Address SET
  PreAddress=Replace(a.PreAddress,b.City,''),
  City=b.City
FROM Address a INNER JOIN Cities b  ON b.City=RIGHT(a.PreAddress,LEN(b.city))

答案 1 :(得分:2)

如果这是一次性更新而不是常规修复,那么对于SQL的替代方案,您可以使用批量地理编码服务来格式化地址,并且可以将它们加载回表中。你也可以这样得到坐标:)。

答案 2 :(得分:0)

可能会有效

 DECLARE @Tbl TABLE (
        Adress VARCHAR(100),
        City VARCHAR(100), 
        County VARCHAR(1)
    )

    INSERT INTO @Tbl VALUES ('12312 Osprey Drive NW Gig Harbor',NULL, NULL)
    INSERT INTO @Tbl VALUES ('12312 Osprey Drive NW Gig Harbor', NULL, NULL)
    INSERT INTO @Tbl VALUES ('3022 SW Bradford St Seattle', NULL, NULL)
    INSERT INTO @Tbl VALUES ('3022 SW Bradford St Seattle', NULL, NULL)

    --Before Update
    SELECT LEFT(Adress, NULLIF(LEN(Adress)-7,-5)), REVERSE(SUBSTRING(REVERSE(Adress),1,CHARINDEX(' ',REVERSE(Adress))-1)),County  FROM @Tbl 
    --Something like this is what you need
    UPDATE @Tbl  
    SET Adress = t.Adress,
        City = t.city,
        County = t.county
    FROM @Tbl t 
    WHERE t.Adress = Adress
    AND t.City = City