如何将临时表上的符号数据字段拆分为5列? [英] How to split symbol data field on temp table to 5 columns?

查看:66
本文介绍了如何将临时表上的符号数据字段拆分为5列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理SQL Server 2012查询,但遇到一个问题:我无法将临时表字段符号数据拆分为5列.

该怎么做?

 创建表#TEMP(id INT IDENTITY(1,1),SymbolData NVARCHAR(50))插入#TEMP(SymbolData)值('0Hz〜4.5kHz'),('0Hz | 9kHz'),('0V-4.5vl'),('0Hz.4.5kHz')选择*从#TEMP 

如何将列 Symbol Data 分为5列

 值,单位前,符号前,值后,单位后----------------------------------------------------0赫兹〜4.5赫兹0赫兹|9 kHz0 V-4.5 vl0赫兹4.5 kHz的 

临时表列 SymbolData 中的每一行都有一个符号或字符,例如 | 或-等.

我需要将该列 SymbolData 分为5个部分,每个部分代表该列:

  • 在列上的符号之前获取值,作为列之前的值
  • 将列上的符号之前的单位作为之前的单位
  • 将列上的符号获取为符号
  • 将列上符号后的值作为之后的值
  • 将列上符号后的单位作为之后的单位

解决方案

解决方案很简单:

  1. 通过 \ s (空格)保留元素的顺序来拆分值
  2. 透视结果
  3. 仅提取特定列中的数字或字母

为了分割值,您可以使用 XML ,例如

您也可以查看答案,以在您的环境中获得此类功能.

对于您而言,使用XML拆分数据并替换以成形结果将更加容易和安全.

I work on a SQL Server 2012 query, an I face an issue: I can't split temp table field symbol data to 5 columns.

How to do that please?

CREATE TABLE #TEMP
(
    id INT IDENTITY(1,1),
    SymbolData  NVARCHAR(50)
)

INSERT INTO #TEMP (SymbolData)
VALUES ('0Hz ~ 4.5kHz'), ('0Hz | 9kHz'),
       ('0V - 4.5vl'), ('0Hz . 4.5kHz')

SELECT * FROM #TEMP

How to divide column Symbol Data into 5 columns to be

valuebefore unitbefore symbole valueafter unitafter
----------------------------------------------------
0            Hz           ~    4.5         Hz 
0            Hz           |     9          kHz 
0            V            -    4.5         vl
0            Hz           .    4.5         kHz 

Every row in the temp table column SymbolData has a symbol or character like | or - etc..

I need to split that column SymbolData into 5 parts every part represent column:

  • get value before symbol on column as value before
  • get unit before symbol on column as unit before
  • get symbol on column as symbol
  • get value after symbol on column as value after
  • get unit after symbol on column as unit after

解决方案

The solution is easy:

  1. split the values by \s(space) preserving order of elements
  2. pivot the result
  3. extract only numbers or only letters from the specific column

In order to split the values you can use XML like this. In order to extract only numbers you can perform a a chain of REPLACEs removing all units. In order to remove the numbers and leave the text, you can use REPLACEs again.

In my environment, I am using a lot of SQL CLR functions and the solution looks like this:

SELECT PVT.id
      ,PVT.symbolData
      ,dbo.fn_Utils_RegexReplace ([0], '[^\d+]', '') AS [valuebefore]
      ,dbo.fn_Utils_RegexReplace ([0], '\d+', '') AS [unitbefore]
      ,[1] AS [symbole]
      ,dbo.fn_Utils_RegexReplace ([2], '[^\d+\.]', '') AS [valueafter]
      ,dbo.fn_Utils_RegexReplace ([2], '[\d+\.]', '') AS [unitafter]
FROM #TEMP
CROSS APPLY dbo.fn_Utils_RegexSplitWithOrder (SymbolData, '\s') RS
PIVOT
(
    MAX([value]) FOR [index] IN ([0], [1], [2])
) PVT
ORDER BY PVT.id;

You can check this answer to get such functions in your environment, too.

In your case, it will be easier and safer to use XML to split the data and replace to shape the results.

这篇关于如何将临时表上的符号数据字段拆分为5列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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