如何将一列数据拆分为多少列

时间:2012-11-26 06:04:57

标签: sql sql-server sql-server-2008

表格-1

create table test (dname varchar(250))
insert test values('0.1 ML LIDOCAINE HYDROCHLORIDE 10 MG/ML')
select * from test

                  dname
                 -------

0.1 ML LIDOCAINE HYDROCHLORIDE 10 MG/ML

我还有一个像

这样的表格

表格-2

create table test1(
unit1 int,
measure1 varchar(20),
drugname varchar(250),
unit2 int,
measure2 varchar(40)
)

现在我必须通过使用insert into newtablename select from old table name将测试表中的数据插入到test1表中

我的预期输出是

select * from test1

unit1     measure1     drugname                    unit2      measure2
0.1       ML           LIDOCAINE HYDROCHLORIDE     10           mg/ml

请注意,此处药物名称长度,即LIDOCAINE HYDROCHLORIDE可能会增加或减少。 我在这里发现了很多不符合我要求的问题。

请提供此问题的解决方案。

不要提出任何建议,因为已经存在更简单的问题。因为我已经提到了本网站发布的问题和答案。

4 个答案:

答案 0 :(得分:3)

SQL Fiddle

MS SQL Server 2008架构设置

create table test (dname varchar(250))
insert test values('0.1 ML LIDOCAINE HYDROCHLORIDE 10 MG/ML')

查询1

select left(dname, S1.Pos - 1) as FirstWord,
       substring(dname, S1.Pos + 1, S2.Pos - S1.Pos - 1) as SecondWord,
       substring(dname, S2.Pos+1, len(dname)-S3.Pos-S2.Pos) as WordsInTheMiddle,
       left(right(dname, S3.Pos - 1), S3.Pos - S4.Pos - 1) as SecondButLastWord,
       right(dname, S4.Pos - 1) as LastWord
from test
  cross apply (select charindex(' ', dname)) as S1(Pos)
  cross apply (select charindex(' ', dname, S1.Pos+1)) as S2(Pos)
  cross apply (select charindex(' ', reverse(dname))) as S4(Pos)
  cross apply (select charindex(' ', reverse(dname), S4.Pos+1)) as S3(Pos)

<强> Results

| FIRSTWORD | SECONDWORD |        WORDSINTHEMIDDLE | SECONDBUTLASTWORD | LASTWORD |
-----------------------------------------------------------------------------------
|       0.1 |         ML | LIDOCAINE HYDROCHLORIDE |                10 |    MG/ML |

答案 1 :(得分:0)

使用此功能分割您的值

CREATE FUNCTION Splitfunction(@String varchar(8000), @Delimiter char(1))       
returns @temptable TABLE (items varchar(8000))       
as       
begin       
declare @idx int       
declare @slice varchar(8000)       

select @idx = 1       
    if len(@String)<1 or @String is null  return       

while @idx!= 0       
begin       
    set @idx = charindex(@Delimiter,@String)       
    if @idx!=0       
        set @slice = left(@String,@idx - 1)       
    else       
        set @slice = @String       

    if(len(@slice)>0)  
        insert into @temptable(Items) values(@slice)       

    set @String = right(@String,len(@String) - @idx)       
    if len(@String) = 0 break       
end   
return      

end

之后在查询中使用该功能

SELECT i.items FROM dbo.Splitfunction('0.1 ML LIDOCAINE HYDROCHLORIDE 10 MG/ML',' ') AS i

现在您将该值插入另一个表中。

愿这对你有帮助吗?

此致

Amit Vyas

答案 2 :(得分:0)

    Use DB_Name
GO
if (object_id('splitFields') is not null)
drop function splitFields;
GO
CREATE FUNCTION splitFields(
    @strIds varchar(max) 
)
RETURNS @temp table (unit1 varchar(50),     measure1 varchar(50),drugname varchar(50),unit2 varchar(50),measure2 varchar(50))
AS
BEGIN 
    declare @i int,@j int,@l int,@v bigint;
    SET @i = 0;
    SET @j = 0;
    SET @l = len(@strIds);
    declare @unit1 varchar(50), @measure1 varchar(50),@drugname varchar(50),@unit2 varchar(50),@measure2 varchar(50);
    declare @no int;
    set @no = 0
    while (@j < @l)
    BEGIN
        SET @j = charindex(' ',@strIds,@i+1);
        IF @j = 0 set @j = @l+1;
        SET @v = substring(@strIds,@i+1,@j-@i-1);
        if @no = 0 then set @unit1= @v
        if @no = 1 then set @measure1= @v
        if @no = 2 then set @drugname= @v
        if @no = 3 then set @unit2= @v
        if @no = 4 then set @measure2= @v
        SET @no = @no + 1;
        SET @i = @j;
    END
    insert into @temp(unit1, measure1,drugname,unit2,measure2) values(@unit1, @measure1,@drugname,@unit2,@measure2)
    RETURN ;
END
GO

我没有足够的时间做测试,请自行测试。

答案 3 :(得分:0)

试试这个:

此查询有点长,但根据需要提供 SQL Fiddle demo