从日期时间中获取时间并将其转换为秒? [英] Take the time from the datetime and convert it into seconds?
问题描述
我正在运行 SQL Server 2005.从技术上讲,我知道如何从 tsql 日期时间中获取时间.
I am running SQL Server 2005. Technically I know how to take the time from a tsql datetime.
CONVERT(VARCHAR(8),GETDATE(),108) AS HourMinuteSecond
问题是我有一个日期时间字段,我需要基本上获取时间部分,将其转换为整数,特别是秒.然后我需要对这个整数做一堆算术,我不会在这里讨论.我在 stackoverflow 上搜索过,但没有找到特定于这个问题的问题.有任何想法吗?我真的在这里寻找最佳实践,我担心为此特定目的创建 udf,因为它完全将查询优化器抛到了窗外.
The problem is that I have a datetime field and I need to essentially grab the time portion convert that to an integer specifically seconds. Then I need to do a bunch of arithmetic on this integer that I won't discuss here. I have searched on stackoverflow and haven't found a question that is specific to this question. Any ideas? I am really looking for best practices here, I am worried about creating a udf for this specific purpose as it completely throws the query optimizer out the window.
我看过这个网页,所以请不要粘贴这个.:
I have seen this webpage so please don't paste this. :
http://forums.devx.com/showthread.php?171561-TSQL-Converting-HH-MM-SS-to-seconds
推荐答案
使用 <代码>日期部分代码>:
(DATEPART(HOUR, GETDATE()) * 3600) +
(DATEPART(MINUTE, GETDATE()) * 60) +
(DATEPART(SECOND, GETDATE()))
这篇关于从日期时间中获取时间并将其转换为秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!