简体   繁体   English

sql server 2000转换datetime得到hhmm

[英]sql server 2000 convert datetime to get hhmm

I'm using 我正在使用

convert(varchar(20), getdate(), 112)

to convert getdate() to yyyymmdd format (ISO format), which works great. 将getdate()转换为yyyymmdd格式(ISO格式),效果很好。 Now I need to do something similiar to get the time in hhmm format. 现在我需要做类似的事情以获得hhmm格式的时间。 How can I achieve this? 我怎样才能做到这一点?

Example: 12:10 pm should look like 1210, 3:43 pm should look like 1543. 例如:下午12:10应该看起来像1210,下午3:43应该看起来像1543。

SELECT REPLACE(CONVERT(varchar(5), GETDATE(), 108), ':', '')
SELECT REPLACE(CONVERT(CHAR(5),GETDATE(),108), ':', '')

如果你不需要冒号,只需删除它...

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: 不要只做几小时的datepart,得到分钟的datepart,并连接19:05将结束195.如果你走这条路线,你需要做这样的事情来处理分钟:

right('0' + varchar(datepart(mi,getdate())),2) 对('0'+ varchar(datepart(mi,getdate())),2)

at this point it's getting pretty inefficient. 在这一点上它变得非常低效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM