拆分sql中的数据

时间:2014-11-20 17:45:52

标签: sql sql-server tsql

我正在分割一个字符串,我用这个查询得出了正确的答案

select 

      SUBSTRING(unit1, 1, CHARINDEX(' ', unit1) - 1)Qty
      ,SUBSTRING(unit1, CHARINDEX(' ', unit1) + 1, LEN(unit1))Units

      --,unit1
      from 

      (SELECT  
      SUBSTRING(Description], CHARINDEX(': ', [Description]) + 2, LEN([Description])) unit1
  FROM Table 
  )

对于此数据,此查询将“描述”拆分为仅限于“描述”中的单位和

中的单位
Description                     Qty   Units 
Container Capacity: 5 Gallon    5     Gallon    
Container Capacity: 1 Liter     1     Liter 
Container Capacity: 1 quart     1     quart 
Container Capacity: 0.5 Liter   0.5   Liter 
Container Capacity: 20 Liter    20    Liter 
Container Capacity: 236 ml      236   ml    
Container Capacity: 250 ml      250   ml    
Container Capacity: 3.5 oz      3.5   oz    
Container Capacity: 4 Liter     4     Liter 
Container Capacity: 5 Liter     5     Liter 
Container Capacity: 55 Liter    55    Liter 
Container Capacity: 60 Liter    60    Liter 
Container Capacity: 1 pint      1     pint  

我想我的问题是有没有更好的方法来做到这一点只是接缝,我应该能够切出内部的选择语句,并从一个选择语句得到正确的形式。 非常感谢你的帮助

0 个答案:

没有答案