写了一个大概的,对于除不尽的,直接取小数后的第一位,你试试: select left(v,charindex(':',v)-1)+ cast(cast(substring(v,charindex(':',v)+1,LEN(v))/60.0 as numeric(2,1)) as varchar) as '转换后的值' from ( select '7:30' v union all select '7:20' union all select '7:13' )t /* 转换后的值 70.5 70.3 70.2 */
上面有点问题,修改一下:select left(v,charindex(':',v)-1)+ cast(substring(v,charindex(':',v)+1,LEN(v))/60.0 as numeric(2,1)) as '转换后的值' from ( select '7:30' v union all select '7:20' union all select '7:13' )t /* 转换后的值 7.5 7.3 7.2 */
是SQL啊,那这样: declare @t time = '07:20' select CAST(DATEPART(hour,@t)+DATEPART(minute,@t)/60.0 as decimal(10,1))
create table wy (x varchar(10),y varchar(10))insert into wy(x) select '7:30' union all select '7:20' update wy set y=substring(x,1,charindex(':',x,1)-1)+'.' +rtrim(cast(substring(x,charindex(':',x,1)+1,10) as int)/6) select * from wy/* x y ---------- ---------- 7:30 7.5 7:20 7.3(2 row(s) affected) */
select left(v,charindex(':',v)-1)+
cast(cast(substring(v,charindex(':',v)+1,LEN(v))/60.0 as numeric(2,1)) as varchar) as '转换后的值'
from
(
select '7:30' v union all
select '7:20' union all
select '7:13'
)t
/*
转换后的值
70.5
70.3
70.2
*/
cast(substring(v,charindex(':',v)+1,LEN(v))/60.0 as numeric(2,1)) as '转换后的值'
from
(
select '7:30' v union all
select '7:20' union all
select '7:13'
)t
/*
转换后的值
7.5
7.3
7.2
*/
这个方法应该还行的吧:
string str = Math.Round(TimeSpan.Parse("7:30").TotalHours, 1).ToString();
declare @t time = '07:20'
select CAST(DATEPART(hour,@t)+DATEPART(minute,@t)/60.0 as decimal(10,1))
create table wy
(x varchar(10),y varchar(10))insert into wy(x)
select '7:30' union all
select '7:20'
update wy
set y=substring(x,1,charindex(':',x,1)-1)+'.'
+rtrim(cast(substring(x,charindex(':',x,1)+1,10) as int)/6)
select * from wy/*
x y
---------- ----------
7:30 7.5
7:20 7.3(2 row(s) affected)
*/