0

表格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

4 回答 4

3

SQL小提琴

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)

结果

| FIRSTWORD | SECONDWORD |        WORDSINTHEMIDDLE | SECONDBUTLASTWORD | LASTWORD |
-----------------------------------------------------------------------------------
|       0.1 |         ML | LIDOCAINE HYDROCHLORIDE |                10 |    MG/ML |
于 2012-11-26T06:40:42.400 回答
0

尝试这个:

这个查询有点长,但给了你需要的

select left(dname,charindex(' ',dname)) as unit1,
       substring(dname,charindex(' ',dname)+1,charindex(' ',dname,charindex(' ',dname)+1)-charindex(' ',dname)-1) as measure1,
       substring(dname,charindex(' ',dname,charindex(' ',dname)+1)+1,len(dname)-charindex(' ',reverse(dname),charindex(' ',reverse(dname))+1)-charindex(' ',dname,charindex(' ',dname)+1)+1) as drugname, 
       reverse(substring(reverse(dname),charindex(' ',reverse(dname),charindex(' ',reverse(dname))+1)-2,charindex(' ',reverse(dname),charindex(' ',reverse(dname))+1)-charindex(' ',reverse(dname))-1)) as unit2,
       reverse(left(reverse(dname),charindex(' ',reverse(dname))-1)) as measure2
from test

SQL Fiddle 演示

于 2012-11-26T06:35:01.920 回答
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

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

这对你有帮助吗?

问候

阿米特维亚斯

于 2012-11-26T06:25:13.577 回答
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

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

于 2012-11-26T06:25:20.413 回答