如何将字符串分成不同的列? [英] how to separate string into different columns?

查看:22
本文介绍了如何将字符串分成不同的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张表格,里面有这样的条目.

I've a table with entries like this.

    MachineName
-----------------------

    Ab bb zecos
    a zeng
    zeng
    empty

一个表格中有 4 行.

4 rows in a table.

如何将这 4 行分开以获得类似的输出.

How can i seperate those 4 rows to get output like.

       M1       M2       M3
-----------------------------------
       Ab       bb      zecos
       a        zeng     NULL
       zeng     NULL     NULL
       NULL     NULL     NULL

推荐答案

有一个名为 ParseName 的函数代替使用 split 函数,它返回对象的指定部分,该部分拆分由 分隔的字符串.请通过 ParseName 链接帮我写了这个查询

Instead of using split function there is a function called ParseName which returns the specified part of the object which spilts the string delimated by . Please go through the ParseName link which helped me in writing this query

Declare @Sample Table
(MachineName varchar(max))

Insert into @Sample
values 
('Ab bb zecos'),('a Zeng')


  SELECT 
  Reverse(ParseName(Replace(Reverse(MachineName), ' ', '.'), 1)) As [M1]
 , Reverse(ParseName(Replace(Reverse(MachineName), ' ', '.'), 2)) As [M2]
 , Reverse(ParseName(Replace(Reverse(MachineName), ' ', '.'), 3)) As [M3]

  FROM  (Select MachineName from @Sample
  ) As [x] 

这篇关于如何将字符串分成不同的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆