Home > database >  Find the first '-' and '-' the second between the words
Find the first '-' and '-' the second between the words

Time:09-16

Raw material plastic _XEFW _ - 900 bb juniper


Query the plastic

CodePudding user response:

This no one will be?

CodePudding user response:

 - 1. Add a string table value segmentation function 
GO
IF OBJECT_ID (' [dbo]. [Fun_Split] ') IS NOT NULL
The DROP FUNCTION [dbo] Fun_Split
GO
The CREATE FUNCTION [dbo] Fun_Split (@ STR NVARCHAR (MAX), @ the split NVARCHAR (10))
RETURNS the @ table table (rowNum INT IDENTITY (1, 1) PRIMARY KEY, [item] NVARCHAR (Max))
AS
The BEGIN
IF LEN (@ the split)=0
The BEGIN
The SET @ the split=N ', '
END

DECLARE @ XML XML;
SET @=the CONVERT XML (XML, '& lt; x> ')

INSERT INTO @ table ([items])
SELECT the item
The FROM (SELECT c. alue (' text () [1] 'and' nvarchar (4000) ') (item)
The FROM @ XML nodes ('/' x) t (c)) t
WHERE the item IS NOT NULL

RETURN
END
GO

- 2. To create a test table variables and test data
DECLARE @ t TABLE (
V NVARCHAR (MAX)
)
INSERT INTO @ t VALUES (' _ _XEFW plastic raw materials - 900 bb juniper ');

Query - 3.
SELECT *, SELECT the item FROM dbo. Fun_Split (v, '_') WHERE rowNum=2) AS r
The FROM @ t
/*
V r
Juniper plastic raw material plastic _XEFW _ - 900 bb
*/

CodePudding user response:

 select PARSENAME (REPLACE (ltrim (rtrim (' _ plastic raw materials bb juniper _XEFW - 900 ')), '_', '. '), 2) 
  • Related