繁体   English   中英

在SQL中将单列值分离为多列

[英]Separate Single column Value into multiple columns in SQL

我有以下格式的表格,

MENUACTION         VALUE

ReceivedDetails    ACCOUNTNO v="3275402GBP"
ReceivedDetails    AGR1 
ReceivedDetails    AGR2 

我需要以下格式的表格

MENUACTION         VALUE                       CONTROLID      DATAVALUE      

ReceivedDetails    ACCOUNTNO v="3275402GBP"    ACCOUNTNO      3275402GBP
ReceivedDetails    AGR1                        AGR1
ReceivedDetails    AGR2                        AGR2

注意:“值列”中的记录是动态的,我们需要将“值列”中的标记分隔为两个不同的列(ControlID,DataValue)

你能帮上忙吗 提前致谢。

步骤1:更改表以添加DATAVALUE列步骤2:运行此

update table [tablename]
set Value = left(
              -- The Right below gets 3275402GBP"
              Right(VALUE, len(Value) - charindex('v=', Value) - 2),
              -- and the left gets it again to get it's length - 1
              -- leaving 3275402GBP
              Len(Right(VALUE, len(Value) - charindex('v=', Value) - 2) - 1
                 )
-- exclude any without a 'v=' to avoid invalid substrings
where charindex('v=', Value) > 0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM