sql left join 字符串


要實現join字符串

select * FROM table1 as t1
right join (select '1,2,3,4,5'  as t) as tt on t1.Id=tt.t

則需要分割字符串為數組,以下為實現的分割字符串函數split

 

 split函數及使用示例: 


select * FROM table1 as t1
right join (select * from split('1,3,5',',')) as tt on t1.Id=tt.F1

split函數:

/*
獲取字符串數組的 Table www.cnblogs.com/xqhppt/p/4377757.html
*/

if exists (select 1 from sysobjects where id = object_id('split' ))
drop Function split
go
CREATE function split(
@SourceSql varchar (max),
@StrSeprate varchar (10)
)
returns @temp table( F1 varchar (100))
as
begin
declare @i int
set @SourceSql =rtrim( ltrim(@SourceSql ))
set @i =charindex( @StrSeprate,@SourceSql )
while @i >=1
begin
insert @temp values(left( @SourceSql,@i -1))
set @SourceSql =substring( @SourceSql,@i +1, len(@SourceSql )-@i)
set @i =charindex( @StrSeprate,@SourceSql )
end

if @SourceSql <>''
insert @temp values( @SourceSql)

return
end
GO

 

FromUrl:www.cnblogs.com/xuejianxiyang/p/7025929.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM