Получить только строку в T-SQL - PullRequest
0 голосов
/ 31 мая 2019

Учитывая приведенные ниже строки,

DECLARE @Text1 = 'This is the 1st time I found the Car# CAR:8 #NumberIncluded#'

Хотелось бы узнать, есть ли способ получить символ "#"

Ответы [ 2 ]

3 голосов
/ 31 мая 2019

Это один из вариантов получения. Сначала он удаляет все что угодно до CAR:, а затем удаляет все после любых цифр в оставшейся строке.

SELECT SUBSTRING( x.TruncString, 0, PATINDEX( '%[^0-9]%', x.TruncString))
FROM (VALUES('This is the 1st time I found the Car# CAR:8 #NumberIncluded#'),
            ('This is the 1st time I found the Car# CAR:8 #NumberIncluded# with no ID tied to it, the prices is 588USD for  CAR:8 #NumberIncluded##NumberIncluded#')
     )SampleData(String)
CROSS APPLY( SELECT STUFF( String, 1, CHARINDEX( 'CAR:', String)+3, ''))x(TruncString)
1 голос
/ 31 мая 2019

Я хотел бы предложить это:

DECLARE @Text1 NVARCHAR(MAX) = 'This is the 1st time I found the Car# CAR:8 #NumberIncluded#'

DECLARE @Text2 NVARCHAR(MAX) = 'This is the 1st time I found the Car# CAR:8 #NumberIncluded# with no ID tied to it, the prices is 588USD for  CAR:7 #NumberIncluded##NumberIncluded#'

DECLARE @Text3 NVARCHAR(MAX) = 'My CAR:A5, CAR:9 AND CAR:10'

SELECT SUBSTRING(@Text1, PATINDEX('%CAR:[0-9]%', @Text1) + 4, 1)  
SELECT SUBSTRING(@Text2, PATINDEX('%CAR:[0-9]%', @Text2) + 4, 1)
SELECT SUBSTRING(@Text3, PATINDEX('%CAR:[0-9]%', @Text3) + 4, 1)

Ожидаемые результаты : 8, 8, 9

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...