--
用PARSENAME函数拆分字符串
DECLARE @T TABLE
(
COL VARCHAR ( 80 )
)
INSERT INTO @T
SELECT ' 123_12_124 ' UNION ALL
SELECT ' 1234_125_1243 '
SELECT COL, COL1 = PARSENAME ( REPLACE (COL, ' _ ' , ' . ' ), 3 ), -- --替换一下 '.' 因为 parsename 只认 '.'
COL2 = PARSENAME ( REPLACE (COL, ' _ ' , ' . ' ), 2 ),
COL3 = PARSENAME ( REPLACE (COL, ' _ ' , ' . ' ), 1 )
FROM @T
COL COL1 COL2 COL3
-- -------- ---------- ---------- ----------
123_12_ 124 123 12 124
1234_125_ 1243 1234 125 1243
DECLARE @T TABLE
(
COL VARCHAR ( 80 )
)
INSERT INTO @T
SELECT ' 123_12_124 ' UNION ALL
SELECT ' 1234_125_1243 '
SELECT COL, COL1 = PARSENAME ( REPLACE (COL, ' _ ' , ' . ' ), 3 ), -- --替换一下 '.' 因为 parsename 只认 '.'
COL2 = PARSENAME ( REPLACE (COL, ' _ ' , ' . ' ), 2 ),
COL3 = PARSENAME ( REPLACE (COL, ' _ ' , ' . ' ), 1 )
FROM @T
COL COL1 COL2 COL3
-- -------- ---------- ---------- ----------
123_12_ 124 123 12 124
1234_125_ 1243 1234 125 1243
(所影响的行数为 2 行)
本文转自曾祥展博客园博客,原文链接:http://www.cnblogs.com/zengxiangzhan/archive/2010/01/03/1638183.html,如需转载请自行联系原作者