0

在我的表中,我有经验字段,例如

经验

  • 5年0个月

  • 2年0个月

在这里,我想转换为秒,然后将年份和月份添加到单个列中。

经验 - [一些价值]

所以我创建了一个如下查询,

select top(10)'insert into candidates(experience)values('+

CAST(SUBSTRING(CAST(o.Experience AS VARCHAR(50)), 0, PATINDEX('%Years%', o.Experience))      * 31536000 AS VARCHAR(50))

           +','+CAST(SUBSTRING(CAST(o.Experience AS VARCHAR(50)), PATINDEX('%Years%', o.Experience) + 5

        ,patindex('%Months%', o.Experience) - PATINDEX('%Years%', o.Experience) - 5) * 
2678400 AS VARCHAR(50))+')'

            from candidatedetails as o

从上面的代码我得到的结果是,

经验

insert into candidates(experience)values(157680000,0)
insert into candidates(experience)values(31536000,26784000)

预期结果

 insert into candidates(experience)values(157680000)
 insert into candidates(experience)values(58320000)//add(31536000+26784000)

如何在我的查询中做到这一点?有人帮我吗?

4

2 回答 2

1

你想要的是变成这样的东西(当前生成)

insert into candidates(experience)values(157680000,0)
insert into candidates(experience)values(31536000,26784000)

为此,这将总结他们

insert into candidates(experience) select 157680000+0;
insert into candidates(experience) select 31536000+26784000;

这看起来像更改您的原始代码类似:

select top(10) 'insert into candidates(experience) select '+
        CAST(SUBSTRING(CAST(o.Experience AS VARCHAR(50)), 0,
             PATINDEX('%Years%', o.Experience)) * 31536000 AS VARCHAR(50))
   +'+'+CAST(SUBSTRING(CAST(o.Experience AS VARCHAR(50)),
             PATINDEX('%Years%', o.Experience) + 5,
             patindex('%Months%', o.Experience)
           - PATINDEX('%Years%', o.Experience) - 5) * 2678400 AS VARCHAR(50))+';'
from candidatedetails as o
于 2013-04-20T11:13:08.197 回答
1

我找到了答案,

SELECT TOP(10) 'INSERT INTO jobs(Experience) VALUES('+
        CAST(SUBSTRING(CAST(r.experience AS VARCHAR(50)), 0, PATINDEX('%Years%', r.experience))*31536000
            + SUBSTRING(CAST(r.experience AS VARCHAR(50)), PATINDEX('%Years%', r.experience) + 5, 
                                                       patindex('%Months%', r.Experience) - PATINDEX('%Years%', r.Experience) - 5)* 2678400 AS VARCHAR(50))+')'
    FROM  candidatedetails r
于 2013-04-20T11:18:46.727 回答