Hàm Split tách ký tự thành 1 bảng trong SQL Server
--Nhatthm
-- Example: select * from dbo.Split('1,3,4,5' , ',')
CREATE function [dbo].[Split] (
@StringToSplit varchar(1000),
@Separator varchar(1))
returns table as return
with indices as
(
select 0 S, 1 E
union all
select E, charindex(@Separator, @StringToSplit, E) + len(@Separator)
from indices
where E > S
)
select substring(@StringToSplit,S,
case when E > len(@Separator) then e-s-len(@Separator) else len(@StringToSplit) - s + 1 end) String
,S StartIndex
from indices where S >0
Ứng dụng:
Select * From tbNhanVien Where MaNV In (select String from dbo.Split(@NAME , ',')
Tham số NAME gán giá trị là
A,B,C