时间转换打嗝 [英] time conversion hiccup

查看:60
本文介绍了时间转换打嗝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




ddl& dml

项目varchar(10)start char(5)stop char(5)

------------------ ------- ----- -----

嘿现在21:00 19:25

新测试20:25 20:30

t 10 21:00 NULL

t 11 21:10 21:35

t 12 21:30 22:40

t 12 7:05 11:10

test me 08:00 14:25

test me 17:00 17:55


我想要的是用小时计算持续时间(h.1decimal)例如

1.2:

我现在使用以下查询:

选择项目,开始,停止,

CASE WHEN(datediff(n,start,stop)< 0)THEN -1

WHEN(datediff(n,start) ,停止)< 1)然后(CAST(datediff(n,开始,停止)

为十进制(1)))

ELSE转换(十进制(1), (datediff(n,start,stop)/ 60))END as

total_hours
来自testTBl的

按项目分组,开始,停止

输出:

项目开始停止total_hours

---- --------------------- ----- ----- -----------

嘿现在21:00 19:25 -1

新测试20:25 20:30 0

t 10 21:00 NULL NULL

t 11 21:10 21:35 0
$ b $ 12 12:30 22:40 1
$ b $ 12 12:05 11:10 4

测试我08 :00 14:25 6

测试我17:00 17:55 0


如果计算结果正确我想删除开始和停止列,

所以,它只会返回项目和小时数,包括少于每小时一小时的小数字。


谢谢。

解决方案

嗨!


我能通过快速查看阅读是两个错误或错误。


1)变量或类型为decimal(1)的结果的定义,可以存储在

的最左边和右边的总数位数
小数点,所以你永远不会得到任何超过一个

位数的结果,即使结果应该是10或更多,在这种情况下你是

应该出现溢出错误。


2)除以整数60后,操作将成为一个

整数除法,你可以通过执行这个声明轻松看到:

select datediff(n,'''08:00'',''14:25'')/ 60,


convert(decimal(1),datediff(n,'''08:00'',''14:25'')/ 60),


datediff(n ,''08:00'',''14:25'')/ 60.0,


转换(decimal(1),datediff(n,'''08:00'' ,''14:25'')/ 60.0)


希望这会有所帮助,


Palli


<请******* @ gmail.com写信息

新闻:11 ********************** @ g4g2000hsf.googlegro ups.com ...





ddl& dml

项目varchar(10)start char(5)stop char(5)

------------------ ------- ----- -----

嘿现在21:00 19:25

新测试20:25 20:30

t 10 21:00 NULL

t 11 21:10 21:35

t 12 21:30 22:40

t 12 7:05 11:10

test me 08:00 14:25

test me 17:00 17:55


我想要的是用小时计算持续时间(h.1decimal)例如

1.2:

我现在使用以下查询:

选择项目,开始,停止,

CASE WHEN(datediff(n,start,stop)< 0)THEN -1

WHEN(datediff(n,start) ,停止)< 1)然后(CAST(datediff(n,开始,停止)

为十进制(1)))

ELSE转换(十进制(1), (datediff(n,start,stop)/ 60))END as

total_hours
来自testTBl的


按项目分组,开始,停止


输出:

项目开始停止total_hours

----- -------------------- ----- ----- -----------

嘿现在21:00 19:25 -1

新测试20:25 20:30 0

t 10 21:00 NULL NULL

t 11 21 :10 21:35 0

t 12 21:30 22:40 1
$ b $ 12 12:05 11:10 4

测试我08: 00 14:25 6

测试我17:00 17:55 0


如果计算结果正确我想删除开始和停止列,

所以,它只会返回项目和小时数,包括少于每小时一小时的小时数。


谢谢。



很漂亮,谢谢。


9月18日,9:上午43点,Pall Bjornsson < pa ... @ kvos.iswrote:


嗨!


我通过快速阅读看到的是两个错误或错误。


1)变量的定义或类型为decimal(1)的结果,可以存储在

最多的一个总数位数在

小数点的左侧和右侧,所以你永远不会得到任何超过一个

位数的结果,即使结果应该是10或更多,在这种情况下你

应该会出现溢出错误。


2)整数60的除法强制操作是一个

整数除法,你可以通过执行这个语句很容易看到:

select datediff(n,''08:00'',''14:25'' )/ 60,


转换(decimal(1),datediff(n,'''08:00'',''14:25'')/ 60),

datediff(n,''08:00'',''14:25'')/ 60.0,


convert(decimal(1) ,DATEDIFF(N '' 08 :00'',''14:25'')/ 60.0)


希望这会有所帮助,


Palli


< DonLi2 ... @ gmail.com写信息


新闻:11 **************** ******@g4g2000hsf.googlegro ups.com ...





OP省略

- 显示引用文本 -



ahe,我说得有点太新了,新概率。

数据集:

开始停止

19:30 02:15(第二天早上)

26: 15(无效hh:mm时间范围)


CASE WHEN(datediff(n,start,stop)< 0)那么0结束


以上stmt不好,现在怎么办?我得去吃饭,你能帮帮我吗?b $ b想想哦,你可能会问,我也可以为你吃饭吗? :)谢谢一个

亿...

9月18日上午10点58分,DonLi2 ... @ gmail.com写道:


漂亮,谢谢。


9月18日上午9:43,Pall Bjornsson < pa ... @ kvos.iswrote:


嗨!


我通过快速阅读看到的是两个错误或错误。


1)变量的定义或者decimal(1)类型的结果,可以存储在

最多的一个

小数点左侧和右侧的位数,所以你永远不会得到任何超过单个

位数的结果,即使结果应该是10或更多,在这种情况下你应该得到溢出错误


2)除以整数60的除法强制操作为

整数除法,因为你很容易通过执行此声明来看:

select datediff(n,'''08:00'',''14:25'')/ 60,


convert(decimal(1),datediff(n,'''08:00'',''14:25'')/ 60),


datediff(n,''08:00'',''14:25'')/ 60.0,


convert(decimal(1),datediff(n,'''08:00'',''14:25'')/ 60.0)


希望这会有所帮助,


Palli


< DonLi2 ... @ gmail.comwrote in message


news:11 ***** *****************@g4g2000hsf.googlegro ups.com ...





OP省略

- 显示引用文字 - 隐藏引用文字 -



- 显示引用的文字 -



Hi,

ddl & dml
project varchar(10) start char(5) stop char(5)
------------------------- ----- -----
hey now 21:00 19:25
new test 20:25 20:30
t 10 21:00 NULL
t 11 21:10 21:35
t 12 21:30 22:40
t 12 7:05 11:10
test me 08:00 14:25
test me 17:00 17:55

what I want is to calculate time duration using hour (h.1decimal) e.g.
1.2 :
what I have now using the following query:
select project, start, stop,
CASE WHEN (datediff(n,start,stop) < 0) THEN -1
WHEN (datediff(n,start,stop) < 1) THEN (CAST(datediff(n,start,stop)
as decimal(1)))
ELSE Convert(decimal(1),(datediff(n,start,stop)/60)) END as
total_hours
from testTBl
group by project, start, stop

output:
project start stop total_hours
------------------------- ----- ----- -----------
hey now 21:00 19:25 -1
new test 20:25 20:30 0
t 10 21:00 NULL NULL
t 11 21:10 21:35 0
t 12 21:30 22:40 1
t 12 7:05 11:10 4
test me 08:00 14:25 6
test me 17:00 17:55 0

If the calcuate is right I''d like to remove start and stop columns,
so, it would just return project and the sum of hours including less
than an hour in decimal for each.

Thank you.

解决方案

Hi !

What I can see via quick read are two errors or mistakes.

1) Definition of a variable or result of type decimal(1), can store at the
most one total number of digits both to the left and to the right of the
decimal point, so you''ll never get a result with anything more than a single
digit number, even if the result should be 10 or more, in which case you
should get an overflow error.

2) The division by the integer number 60 forces the operation to be an
integer division, as you can easily see by executing this statement:
select datediff(n,''08:00'',''14:25'')/60,

convert(decimal(1),datediff(n,''08:00'',''14:25'')/60),

datediff(n,''08:00'',''14:25'')/60.0,

convert(decimal(1),datediff(n,''08:00'',''14:25'')/60.0)

Hope this helps,

Palli

<Do*******@gmail.comwrote in message
news:11**********************@g4g2000hsf.googlegro ups.com...

Hi,

ddl & dml
project varchar(10) start char(5) stop char(5)
------------------------- ----- -----
hey now 21:00 19:25
new test 20:25 20:30
t 10 21:00 NULL
t 11 21:10 21:35
t 12 21:30 22:40
t 12 7:05 11:10
test me 08:00 14:25
test me 17:00 17:55

what I want is to calculate time duration using hour (h.1decimal) e.g.
1.2 :
what I have now using the following query:
select project, start, stop,
CASE WHEN (datediff(n,start,stop) < 0) THEN -1
WHEN (datediff(n,start,stop) < 1) THEN (CAST(datediff(n,start,stop)
as decimal(1)))
ELSE Convert(decimal(1),(datediff(n,start,stop)/60)) END as
total_hours
from testTBl
group by project, start, stop

output:
project start stop total_hours
------------------------- ----- ----- -----------
hey now 21:00 19:25 -1
new test 20:25 20:30 0
t 10 21:00 NULL NULL
t 11 21:10 21:35 0
t 12 21:30 22:40 1
t 12 7:05 11:10 4
test me 08:00 14:25 6
test me 17:00 17:55 0

If the calcuate is right I''d like to remove start and stop columns,
so, it would just return project and the sum of hours including less
than an hour in decimal for each.

Thank you.



Beautiful, thank you.

On Sep 18, 9:43 am, "Pall Bjornsson" <pa...@kvos.iswrote:

Hi !

What I can see via quick read are two errors or mistakes.

1) Definition of a variable or result of type decimal(1), can store at the
most one total number of digits both to the left and to the right of the
decimal point, so you''ll never get a result with anything more than a single
digit number, even if the result should be 10 or more, in which case you
should get an overflow error.

2) The division by the integer number 60 forces the operation to be an
integer division, as you can easily see by executing this statement:
select datediff(n,''08:00'',''14:25'')/60,

convert(decimal(1),datediff(n,''08:00'',''14:25'')/60),

datediff(n,''08:00'',''14:25'')/60.0,

convert(decimal(1),datediff(n,''08:00'',''14:25'')/60.0)

Hope this helps,

Palli

<DonLi2...@gmail.comwrote in message

news:11**********************@g4g2000hsf.googlegro ups.com...

Hi,

OP omitted
- Show quoted text -



ahe, I spoke a bit too soon, new prob.
data sets:
start stop
19:30 02:15 (next day morning)
26:15 (invalid hh:mm time range)

CASE WHEN (datediff(n,start,stop) < 0) THEN 0 END

above stmt not good, what now? got to go eat, could you help me to
think, oh, you may ask, may I eat for you as well? :) thanks a
billion...

On Sep 18, 10:58 am, DonLi2...@gmail.com wrote:

Beautiful, thank you.

On Sep 18, 9:43 am, "Pall Bjornsson" <pa...@kvos.iswrote:

Hi !

What I can see via quick read are two errors or mistakes.

1) Definition of a variable or result of type decimal(1), can store at the
most one total number of digits both to the left and to the right of the
decimal point, so you''ll never get a result with anything more than a single
digit number, even if the result should be 10 or more, in which case you
should get an overflow error.

2) The division by the integer number 60 forces the operation to be an
integer division, as you can easily see by executing this statement:
select datediff(n,''08:00'',''14:25'')/60,

convert(decimal(1),datediff(n,''08:00'',''14:25'')/60),

datediff(n,''08:00'',''14:25'')/60.0,

convert(decimal(1),datediff(n,''08:00'',''14:25'')/60.0)

Hope this helps,

Palli

<DonLi2...@gmail.comwrote in message

news:11**********************@g4g2000hsf.googlegro ups.com...

Hi,

OP omitted
- Show quoted text -- Hide quoted text -


- Show quoted text -



这篇关于时间转换打嗝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆