求个简单的语句 比如: 5分20秒 7分 40秒 10分5秒如何求其和? ~ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 --创建测试数据create table tb(col varchar(20))insert into tb values('5分20秒')insert into tb values('7分40秒')insert into tb values('10分5秒')select cast(left(col , charindex('分',col) - 1) as int) * 60 + cast(substring(col , charindex('分',col) + 1 , charindex('秒',col) - charindex('分',col) - 1) as int) as 秒from tbdrop table tb/*秒 ----------- 320460605*/ --创建测试数据create table tb(col varchar(20))insert into tb values('5分20秒')insert into tb values('7分40秒')insert into tb values('10分5秒')select sum(cast(left(col , charindex('分',col) - 1) as int) * 60 + cast(substring(col , charindex('分',col) + 1 , charindex('秒',col) - charindex('分',col) - 1) as int)) as 秒的总数from tbdrop table tb/*秒的总数 ----------- 1385(所影响的行数为 1 行)*/ --创建测试数据create table tb(col varchar(20))insert into tb values('5分20秒')insert into tb values('7分40秒')insert into tb values('10分5秒')go--sql语句select col , cast(left(col , charindex('分',col) - 1) as int) * 60 + cast(substring(col , charindex('分',col) + 1 , charindex('秒',col) - charindex('分',col) - 1) as int) 秒 from tbwhere charindex('分',col) > 0 and charindex('秒',col) > 0 and charindex('分',col) < charindex('秒',col)union allselect col = '合计', sum(cast(left(col , charindex('分',col) - 1) as int) * 60 + cast(substring(col , charindex('分',col) + 1 , charindex('秒',col) - charindex('分',col) - 1) as int)) as 秒 from tbwhere charindex('分',col) > 0 and charindex('秒',col) > 0 and charindex('分',col) < charindex('秒',col)--删除表drop table tb--结果/*col 秒 -------------------- ----------- 5分20秒 3207分40秒 46010分5秒 605合计 1385(所影响的行数为 4 行)*/ 关于多用户同时取数据的问题 变量名在查询批次或存储过程内部必须唯一 求一条SELECT语句! 请教:除了使用全文索引,有什么办法可以优化 title like '%keys%' 这种查询 更新多条记录问题? 请问我想生成一个四位的随机数应该如何做? SQL Sever批量Insert sql查询连续时间问题 急!在SQL SERVER 7 中,默认是否允许触发器嵌套? 在sql查询不到数据库 (讨论)分页存储过程示例 大家帮帮忙!提示:当没有用 EXISTS 引入子查询时,在选择列表中只能指定一个表达式
create table tb(col varchar(20))
insert into tb values('5分20秒')
insert into tb values('7分40秒')
insert into tb values('10分5秒')
select cast(left(col , charindex('分',col) - 1) as int) * 60 +
cast(substring(col , charindex('分',col) + 1 , charindex('秒',col) - charindex('分',col) - 1) as int) as 秒
from tb
drop table tb/*
秒
-----------
320
460
605
*/
create table tb(col varchar(20))
insert into tb values('5分20秒')
insert into tb values('7分40秒')
insert into tb values('10分5秒')
select sum(cast(left(col , charindex('分',col) - 1) as int) * 60 +
cast(substring(col , charindex('分',col) + 1 , charindex('秒',col) - charindex('分',col) - 1) as int)) as 秒的总数
from tb
drop table tb/*
秒的总数
-----------
1385
(所影响的行数为 1 行)
*/
create table tb(col varchar(20))
insert into tb values('5分20秒')
insert into tb values('7分40秒')
insert into tb values('10分5秒')
go
--sql语句
select col , cast(left(col , charindex('分',col) - 1) as int) * 60 +
cast(substring(col , charindex('分',col) + 1 , charindex('秒',col) - charindex('分',col) - 1) as int) 秒
from tb
where charindex('分',col) > 0 and charindex('秒',col) > 0 and charindex('分',col) < charindex('秒',col)
union all
select col = '合计', sum(cast(left(col , charindex('分',col) - 1) as int) * 60 +
cast(substring(col , charindex('分',col) + 1 , charindex('秒',col) - charindex('分',col) - 1) as int)) as 秒
from tb
where charindex('分',col) > 0 and charindex('秒',col) > 0 and charindex('分',col) < charindex('秒',col)
--删除表
drop table tb--结果
/*
col 秒
-------------------- -----------
5分20秒 320
7分40秒 460
10分5秒 605
合计 1385
(所影响的行数为 4 行)
*/