sql server 2000 将日期时间转换为 hhmm
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8126088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sql server 2000 convert datetime to get hhmm
提问by MAW74656
I'm using
我正在使用
convert(varchar(20), getdate(), 112)
to convert getdate() to yyyymmdd format (ISO format), which works great. Now I need to do something similiar to get the time in hhmm format. How can I achieve this?
将 getdate() 转换为 yyyymmdd 格式(ISO 格式),效果很好。现在我需要做一些类似的事情来获得 hhmm 格式的时间。我怎样才能做到这一点?
Example: 12:10 pm should look like 1210, 3:43 pm should look like 1543.
示例:下午 12:10 应该看起来像 1210,下午 3:43 应该看起来像 1543。
回答by stuartd
SELECT REPLACE(CONVERT(varchar(5), GETDATE(), 108), ':', '')
回答by Alexander Galkin
SELECT REPLACE(CONVERT(CHAR(5),GETDATE(),108), ':', '')
If you don't need colon, just remove it...
如果您不需要冒号,只需将其删除...
回答by Paul D.
Do NOT do just the datepart for the hours, get the datepart for the minutes, and concatenate as 19:05 will end up 195. If you go this route, you'll need to do something like this to deal with minutes:
不要只做小时的日期部分,获取分钟的日期部分,并连接为 19:05 将结束 195。如果你走这条路,你需要做这样的事情来处理分钟:
right('0' + varchar(datepart(mi,getdate())),2)
right('0' + varchar(datepart(mi,getdate())),2)
at this point it's getting pretty inefficient.
在这一点上,它变得非常低效。