日期:2014-05-17 浏览次数:20803 次
select ProductName,EncapsulationType from WorkOrder
CREATE TABLE t1 ( col VARCHAR(50) ) INSERT INTO t1 SELECT 'OM011 OC' UNION ALL SELECT 'GC012 UT' UNION ALL SELECT 'GC011 UT1' UNION ALL SELECT 'GC013 UT1_GC013' UNION ALL SELECT 'GC015 UT1_GC015' SELECT * FROM t1 SELECT CASE WHEN CHARINDEX('_',col)>0 THEN LEFT(col,CHARINDEX('_',col)-1) ELSE col END FROM t1 ---------------------- (无列名) OM011 OC GC012 UT GC011 UT1 GC013 UT1 GC015 UT1
------解决方案--------------------
WITH WorkOrder (ProductName,EncapsulationType) AS( SELECT 'OM011', 'OC' UNION ALL SELECT 'GC012', 'UT' UNION ALL SELECT 'GC011', 'UT1' UNION ALL SELECT 'GC013', 'UT1_GC013' UNION ALL SELECT 'GC015', 'UT1_GC015' ) select ProductName,CASE WHEN CHARINDEX('_',EncapsulationType,0)=0 THEN EncapsulationType WHEN CHARINDEX('_',EncapsulationType,0)>0 THEN SUBSTRING(EncapsulationType,0,4) END EncapsulationType from WorkOrder /* ProductName EncapsulationType ----------- ----------------- OM011 OC GC012 UT GC011 UT1 GC013 UT1 GC015 UT1 (5 行受影响) */
------解决方案--------------------