借用rogeryao
Q:可以用substring嗎
A:想用也可以XD...(參考其二)
其一方式:取左+取右=結合字串
其二方式:取不要的字串(SubString),在取代掉不要的字串
CREATE TABLE TableX
(MM001 NVARCHAR(20),MM002 NVARCHAR(60));
INSERT INTO TableX
VALUES
(1 , N'55.5X46X50cm 120個/板;18箱/板'),
(2 , N'155.5X46X50cm 20個/板;300瓶/板'),
(3 , N'255.5X46X50cm 4120個/板;1000罐/板');
SELECT *
FROM TableX;
--其一
SELECT left(MM002,CharIndex(' ',MM002))
+ Right(MM002,len(MM002)+ 1 - CharIndex(';',MM002)) MM002
from TableX
--其二
select replace(MM002,
substring(MM002,CharIndex(' ',MM002),CharIndex(';',MM002) - CharIndex(' ',MM002))
,'') MM002
from TableX
https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=f65cc3ef22fce2dc482a1489ca683d31
CREATE TABLE TableX
(MM001 NVARCHAR(20),MM002 NVARCHAR(60));
INSERT INTO TableX
VALUES
(1 , N'55.5X46X50cm 120個/板;18箱/板'),
(2 , N'155.5X46X50cm 20個/板;300瓶/板'),
(3 , N'255.5X46X50cm 4120個/板;1000罐/板');
SELECT MM001,MM002,
d.vals.value('(/TR/TD)[1]','NVARCHAR(10)') + ';' +
d.vals.value('(/TR/TD)[3]','NVARCHAR(10)') AS 'MM002NEW'
FROM (
SELECT MM001,MM002,
CAST('<TR><TD>' + REPLACE(REPLACE(MM002,'cm','cm;'), ';', '</TD><TD>') + '</TD></TR>' AS xml) vals
FROM TableX) AS d