SQL查询数据拆分

时间:2015-10-23 13:16:04

标签: sql sql-server sql-server-2008

我需要帮助处理列到输出,如下面sql中所示。我有一个名为Description的列,其中包含以下文本。

User InformationName:jweller 
Company:CamTel 
Business Phone: 1234567890 
Business Email: mobiledevsupport@Demo.com 
Device Model: SGH-I547C 
OS version: 4.1.2 
Application Version: 1.0.59.2 
Product Name: Demo Mobile 
Android App Device language: English (Canada) Device 
Time Zone: Mountain Standard Time 
Data connection:Cellular network (3G) 
Signal Strength: -81dBM  
Error Message Displayed to the U

我想要在行,列和类似的东西中转换此文本。我使用的是sql server 2008.

任何建议都会有很大帮助。

User InformationName | Company | Business Phone  |Email                        |  Device Model |  OS version | Application Version | Product Name
jweller                CamTel    1234567890      mobiledevsupport@Demo.com   SGH-I547C         4.1.2      1.0.59.2              Demo Mobile Android App

2 个答案:

答案 0 :(得分:1)

要获得此结果,您可以尝试这样的事情:

SELECT SUBSTRING(description, (CHARINDEX('User InformationName:', description) + LEN('User InformationName:')), CHARINDEX('Company:', description) - (CHARINDEX('User InformationName:', description) + LEN('User InformationName:'))) AS [User InformationName],
       SUBSTRING(description, (CHARINDEX('Company:', description) + LEN('Company:')), CHARINDEX('Business Phone:', description) - (CHARINDEX('Company:', description) + LEN('Company:'))) AS [Company],
       SUBSTRING(description, (CHARINDEX('Business Phone:', description) + LEN('Business Phone:')), CHARINDEX('Business Email:', description) - (CHARINDEX('Business Phone:', description) + LEN('Business Phone:'))) AS [Business Phone],
       SUBSTRING(description, (CHARINDEX('Business Email:', description) + LEN('Business Email:')), CHARINDEX('Device Model:', description) - (CHARINDEX('Business Email:', description) + LEN('Business Email:'))) AS [Business Email],
       SUBSTRING(description, (CHARINDEX('Device Model:', description) + LEN('Device Model:')), CHARINDEX('OS version:', description) - (CHARINDEX('Device Model:', description) + LEN('Device Model:'))) AS [Device Model],
       SUBSTRING(description, (CHARINDEX('OS version:', description) + LEN('OS version:')), CHARINDEX('Application Version:', description) - (CHARINDEX('OS version:', description) + LEN('OS version:'))) AS [OS version],
       SUBSTRING(description, (CHARINDEX('Application Version:', description) + LEN('Application Version:')), CHARINDEX('Product Name:', description) - (CHARINDEX('Application Version:', description) + LEN('Application Version:'))) AS [Application Version],
       SUBSTRING(description, (CHARINDEX('Product Name:', description) + LEN('Product Name:')), CHARINDEX('Device language:', description) - (CHARINDEX('Product Name:', description) + LEN('Product Name:'))) AS [Product Name]
FROM myTable

以下是SQL Fiddle,因此您可以查看这是您要找的内容。

这里我使用简单的SUBSTRING函数解释here并进行计算从哪个字符串索引开始和长度字符串我使用CHARINDEX和LEN函数,这不是那么复杂你可以找到解释在网上很容易......

GL!

P.S。我想我不需要告诉你,这不是存储信息的好方法,我认为这显然是为什么......

答案 1 :(得分:1)

您也可以使用字符串拆分器来执行此操作,以便首先按换行将数据拆分为行,然后使用以下内容将:之前和之后的值分开:

select * From (
  select ltrim(left(s.Value, P.P - 1)) as Type, 
         ltrim(substring(s.Value, P.P+1, 9999)) as Value
  from table1 t
  cross apply cte_split_core (t.description, char(10)) s
  cross apply (select charindex(':', s.Value) as P) P
  where P.P > 0
) s pivot (
  max(Value) FOR Type IN ([User InformationName],[Company],[Business Phone],[Business Email],[Device Model],[OS version],[Application Version],[Product Name])
) Pvt

CTE功能来自Erland Sommarskog的article,但您也可以使用其他功能。

SQL Fiddle

中的示例