如何在SQL中使用Substring命令将字段分为10个新字段

时间:2019-07-01 22:47:33

标签: sql substring

我有一个标题字段,什么也没有。到目前为止,它没有任何价值。在我的固定宽度文件largefile的末尾是261个字符。现在,我被告知将261个字符字段分为10个单独的字段。我可以使用此新架构将其重新导入。我在该站点上找到了其他内容,在另一个站点上找到了其他内容,这很有意义,但似乎好像缺少了一些代码。我是否打算以正确的方式进行任何思考?

我尝试了以下代码,但以错误结尾。

update dbo.largefile 
set blank1 = substring(nothing,1,9)
unkn1 = substring(nothing,10,1)
unkn2 = substring(nothing,11,1)
blank2 = substring(nothing,12,35)
unkn3 = substring(nothing,47,4)
unkn4 = substring(nothing,51,1)
contact = substring(nothing,52,35)
title = substring(nothing,87,35)
contactphone = substring(nothing,122,10)
website = substring(nothing,132,204)
unkn5 = substring(nothing,203,59);

    Msg 102, Level 15, State 1, Line 3
    Incorrect syntax near 'unkn1'.

1 个答案:

答案 0 :(得分:2)

每次分配后您都缺少逗号:

update dbo.largefile 
set blank1 = substring(nothing,1,9),
unkn1 = substring(nothing,10,1),
unkn2 = substring(nothing,11,1),
blank2 = substring(nothing,12,35),
unkn3 = substring(nothing,47,4),
unkn4 = substring(nothing,51,1),
contact = substring(nothing,52,35),
title = substring(nothing,87,35),
contactphone = substring(nothing,122,10),
website = substring(nothing,132,204),
unkn5 = substring(nothing,203,59);