有效地以 MMDDYYYY 格式输出日期

2021-09-10 00:00:00 sql tsql sql-server sql-server-2012

我有下面的脚本,结果 YYYYMMDD.但是,我需要 MMDDYYYY 格式的结果.

I have the below script which results YYYYMMDD. However, I need the result in MMDDYYYY format.

有人可以帮忙吗?我不希望数字之间有破折号/笔划/句点.

Can someone please assist? I want no dash/stroke/periods between the numbers.

SELECT CONVERT(VARCHAR(10), RowUpdateDateTime, 112)
from MyTable

结果:20141113

我希望它看起来像 11132014.

我尝试了 FORMAT 语法,但它需要很长时间才能运行.

I tried the FORMAT syntax but it was taking forever to run.

推荐答案

查看 CONVERT() 文档:没有一种格式与您正在寻找的完全匹配.看起来 110 是最接近的.我们可以通过添加一个 REPLACE() 调用来完成:

Take a look at the CONVERT() documentation: none of formats match exactly what you're looking for. It looks like 110 is the closest. We can finish by adding a REPLACE() call:

SELECT REPLACE(CONVERT(VARCHAR(10), RowUpdateDateTime, 110),'-','') from MyTable

我也想知道你为什么要这样做.大多数情况下,您的客户端代码可以更有效地处理此类转换.

I also wonder why you're doing this at all. Much of the time, a conversion like this can be handled more effectively by your client code.

相关文章