34,592
社区成员
发帖
与我相关
我的任务
分享
create table tb
(Id int,userIds nvarchar(400), scanTime datetime)
insert tb
select 1 ,'jsh','2000-1-1' union all
select 2 ,'jsh,jsh4','2000-1-1'
select tb.id,
case when number is null then userIds else substring(','+userIds+',',number+1,charindex(',',substring(','+userIds+',',number+1,len(userIds)))-1) end as userIds, scanTime from tb
left join master.dbo.spt_values on type='p' and charindex(',',userIds)>0 and number between 1 and len(','+userIds+',')
and substring(','+userIds,number,1)=','
create table test070301
(
Id int ,userIds nvarchar(200), scanTime datetime
)
insert into test070301
select
1 ,'jsh', '2000-1-1'
union select
2 ,'jsh,jsh4' ,'2000-1-1'
select a.id,SUBSTRING(a.userIds+',',number+1,CHARINDEX(',',a.userIds)-1) userIds ,
CONVERT(varchar,a.scanTime,23) scanTime from test070301 a
inner join master..spt_values b
on b.type='p'
and LEN(SUBSTRING(a.userIds,number+1,CHARINDEX(',',a.userIds)))>0
and CHARINDEX(',',SUBSTRING(a.userIds+',',number+1,CHARINDEX(',',a.userIds)-1))=0
and SUBSTRING(a.userIds,number+1,1)<>'' and SUBSTRING(a.userIds,number+1,1)<>','
id userIds scanTime
----------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------
2 jsh 2000-01-01
2 jsh 2000-01-01
2 sh4 2000-01-01
(3 行受影响)
CREATE TABLE TB(Id INT, userIds VARCHAR(50), scanTime VARCHAR(20))
INSERT dbo.TB
SELECT 1, 'jsh', '2000-1-1' UNION ALL
SELECT 2, 'jsh,jsh4', '2000-1-1'
SELECT a.Id ,
SUBSTRING(a.userIds + ',', b.number,
CHARINDEX(',', a.userIds + ',', b.number) - b.number) AS userIds ,
a.scanTime
FROM dbo.TB a ,
master.dbo.spt_values b
WHERE b.type = 'p'
AND SUBSTRING(',' + a.userIds, b.number, 1) = ','
DROP TABLE dbo.TB
/*
Id userIds scanTime
----------- --------------------------------------------------- --------------------
1 jsh 2000-1-1
2 jsh 2000-1-1
2 jsh4 2000-1-1
(3 行受影响)
*/
select b.id, a.userid, b.scanTime
from t_user a, t_上面那个表 b
where charindex(',' + a.userid + ',', ',' + b.userids + ',') > 0