0

我想在列中分离我的数据,我的数据是

column1
1.1.1 Eastern express highway
1.1.2 weater way
2.1 water
2.1.2 land
2.2 sand

我的输出应该是

column1      column2       column3
1.1            1           Eastern express highway
1.1            2            weater way
2.1            0            water
2.1            2            land
2.2            0            sand
4

3 回答 3

0
SELECT
  substring(column1,1,3) as column1,
  CASE WHEN PATINDEX('%[0-9]%',RTRIM(SUBSTRING(column1,4,7)))=0 THEN '0'
  ELSE
     SUBSTRING(column1,5,1)
  END as column2,
    --RTRIM(SUBSTRING(column1,7,43))as column3
  CASE WHEN PATINDEX('%[0-9]%',RTRIM(SUBSTRING(column1,4,7)))!=0 
  THEN RTRIM(SUBSTRING(column1,7,43))
  ELSE
     RTRIM(SUBSTRING(column1,5,43))
  END as column3
FROM MyTable
于 2015-01-20T10:34:17.017 回答
0
SELECT
    SUBSTRING(ProductDescription, 1,
        CHARINDEX(' - ', ProductDescription) - 1
    ) AS Field1,
    SUBSTRING(ProductDescription,
        CHARINDEX(' - ', ProductDescription) + 3,
        CHARINDEX(' - ', ProductDescription,
            CHARINDEX(' - ', ProductDescription) + 3
        ) - (CHARINDEX(' - ', ProductDescription) + 3)
    ) AS Field2,
    SUBSTRING(ProductDescription,
        CHARINDEX(' - ', ProductDescription,
            CHARINDEX(' - ', ProductDescription) + 3) + 3,
        LEN(ProductDescription)
    ) AS Field3,
    ProductDescription
FROM your_table
于 2012-09-15T12:00:02.920 回答
0

我不知道您为什么要这样做,但这适用于您的示例。尝试

SELECT
  substring(section,1,3) as column1,
  CASE WHEN LEN(section)<5 THEN '0'
  ELSE
     SUBSTRING(section,5,1)
  END as column2
FROM MyTable
于 2012-09-15T12:12:49.683 回答