Import fixed width text to SQL

前端 未结 5 1196
梦谈多话
梦谈多话 2020-12-15 14:50

We have records in this format:

99 0882300 25 YATES ANTHONY V MAY 01 12 04 123456 12345678

The width is fixed and we need to import it int

5条回答
  •  情歌与酒
    2020-12-15 15:38

    When you feel more at home with SQL than importing tools, you could bulk import the file into a single VARCHAR(255) column in a staging table. Then process all the records with SQL and transform them to your destination table:

    CREATE TABLE #DaTable(MyString VARCHAR(255)) 
    INSERT INTO #DaTable(MyString) VALUES ('99 0882300 25 YATES ANTHONY V MAY 01 12 04 123456 12345678')
    
    INSERT INTO FInalTable(Col1, Col2, Col3, Name)
    SELECT CAST(SUBSTRINg(MyString, 1, 3) AS INT) as Col1,
        CAST(SUBSTRING(MyString, 4, 7) AS INT) as Col2,
        CAST(SUBSTRING(MyString, 12, 3) AS INT) as Col3,
        SUBSTRING(MyString, 15, 6) as Name
    FROM #DaTable
    
    result: 99  882300  25  YATES 
    

提交回复
热议问题