使用子字符串选择整数后的值

时间:2014-03-03 07:26:11

标签: sql sql-server

这是我的表结构

create table #t(id int identity(1,1),street varchar(100))

insert into #t values('#100 church street'),('#102 vitalFinaly street'),('#101 teriyakei street')

我需要这样的输出:(整数后6个字符)

id  (No column name)
1   church
2   vitalF
3   teriya

我试过这个select id,substring(street,6) from #t

4 个答案:

答案 0 :(得分:1)

<强> Use Charindex

select id,substring(street,charindex(' ',street)+1,6) from #t

<强> SEE DEMO

答案 1 :(得分:1)

请尝试:

select 
  id, LEFT(SUBSTRING(Street, PATINDEX('%[A-Z]%', Street), LEN(Street)), 6)
from #t

答案 2 :(得分:0)

使用一个查询

DECLARE @temp TABLE(
    id BIGINT
    ,value NVARCHAR(MAX)
)

CREATE TABLE #t(id int identity(1,1),street varchar(100))
INSERT INTO #t 
OUTPUT
     INSERTED.Id
    ,LEFT(SUBSTRING(Inserted.Street, PATINDEX('%[A-Z]%', Inserted.Street), LEN(Inserted.Street)), 6)
INTO @temp
VALUES ('#100 church street'), ('#102 vitalFinaly street'),('#101 teriyakei street')

SELECT *
FROM @temp

答案 3 :(得分:0)

select RTRIM(LTRIM(LEFT(REVERSE(LEFT(REVERSE(street), PATINDEX('%[0-9]%', REVERSE(street)) - 1)),7)))
from #t

请找到上面的答案。