参考: CREATE TABLE A(id INT,country VARCHAR(100)) INSERT A SELECT 1,'中国;日本;韩国' UNION ALL SELECT 2,'美国;意大利;法国' UNION ALL SELECT 3,'德国' SELECT * FROM A-- 建立一个辅助的临时表就可以了 SELECT TOP 8000 id = identity(int,1,1) INTO # FROM syscolumns a, syscolumns b SELECT A.ID, COUNTRY = SUBSTRING(A.COUNTRY, B.ID, CHARINDEX(';', A.COUNTRY + ';', B.ID) - B.ID) FROM A, # B WHERE SUBSTRING(';' + a.COUNTRY, B.id, 1) = ';' ORDER BY 1,2 GODROP TABLE A,#id country ----------- ---------------------------------------------------------------------------------------------------- 1 中国;日本;韩国 2 美国;意大利;法国 3 德国(所影响的行数为 3 行)
不是很明白,我主要是想把bevy_man组成一个字符串.
declare @a varchar(100) set @a='' select @a=@a+bevy_man from tablename select @a as output
不好意思,忘了逗号,改正如下: declare @a varchar(100) set @a='' select @a=@a+bevy_man+',' from tablename select left(@a,len(@a)-1) as output
回:vchao13() ,非常谢谢,输出时为NULL.不知怎么回事
输出时为NULL.不知怎么回事
declare @a varchar(100) set @a='' select @a=@a+isnull(bevy_man,'')+',' from tablename select left(@a,len(@a)-1) as output
如果我把 select @a=@a+isnull(bevy_man,'')+',' from tablename 修改成: select @a=@a+isnull(bevy_id,'')+',' from tablename 就出现: 将 varchar 值 ',' 转换为数据类型为 int 的列时发生语法错误
create function ss() returns varchar(1000) as begin declare @sql varchar(1000) set @sql='' select @sql=@sql+','+ name from test1 set @sql=stuff(@sql,1,1,' ') return @sql end 这是连接写的一个函数,参考下
CREATE TABLE A(id INT,country VARCHAR(100))
INSERT A
SELECT 1,'中国;日本;韩国' UNION ALL
SELECT 2,'美国;意大利;法国' UNION ALL
SELECT 3,'德国'
SELECT * FROM A-- 建立一个辅助的临时表就可以了
SELECT TOP 8000 id = identity(int,1,1)
INTO # FROM syscolumns a, syscolumns b
SELECT
A.ID,
COUNTRY = SUBSTRING(A.COUNTRY, B.ID, CHARINDEX(';', A.COUNTRY + ';', B.ID) - B.ID)
FROM A, # B
WHERE SUBSTRING(';' + a.COUNTRY, B.id, 1) = ';'
ORDER BY 1,2
GODROP TABLE A,#id country
----------- ----------------------------------------------------------------------------------------------------
1 中国;日本;韩国
2 美国;意大利;法国
3 德国(所影响的行数为 3 行)
set @a=''
select @a=@a+bevy_man from tablename
select @a as output
declare @a varchar(100)
set @a=''
select @a=@a+bevy_man+',' from tablename
select left(@a,len(@a)-1) as output
set @a=''
select @a=@a+isnull(bevy_man,'')+',' from tablename
select left(@a,len(@a)-1) as output
select @a=@a+isnull(bevy_man,'')+',' from tablename
修改成:
select @a=@a+isnull(bevy_id,'')+',' from tablename
就出现:
将 varchar 值 ',' 转换为数据类型为 int 的列时发生语法错误
returns varchar(1000)
as
begin
declare @sql varchar(1000)
set @sql=''
select @sql=@sql+','+ name from test1
set @sql=stuff(@sql,1,1,' ')
return @sql
end
这是连接写的一个函数,参考下