gpt4 book ai didi

sql - 如何获取格式为 MM/DD/YYYY 的最新日期顺序

转载 作者:行者123 更新时间:2023-12-03 02:52:34 27 4
gpt4 key购买 nike

IF OBJECT_ID('TEMPDB..#CommaDate') IS NOT NULL
DROP TABLE #CommaDate

CREATE TABLE #CommaDate
(
DateFormate DATE
)

INSERT INTO #CommaDate VALUES('2018-02-24'),('2018-01-10'),('2017-05-23'),('2017-04-06')

SELECT DISTINCT STUFF((SELECT DISTINCT ',' + CONVERT(VARCHAR(20),DateFormate,101)
FROM #CommaDate
ORDER BY ',' + CONVERT(VARCHAR(20),DateFormate,101) DESC
FOR XML PATH('')), 1,1, '')
FROM #CommaDate

我期望的是甲酸盐的最新订单日期 MM/DD/YYY

02/24/2018,01/10/2018,05/23/2017,04/06/2017

最佳答案

由于数据类型是DATE,您可以轻松地对子查询内的结果进行分组和排序。显然,您期望得到 1row x 1col 结果,因此您可以放弃外部 FROM 子句:

SELECT STUFF((SELECT ',' + CONVERT(VARCHAR(20), DateFormate, 101)
FROM #CommaDate
GROUP BY DateFormate
ORDER BY DateFormate DESC
FOR XML PATH('')
), 1, 1, '')

输出:

(No column name)
02/24/2018,01/10/2018,05/23/2017,04/06/2017

关于sql - 如何获取格式为 MM/DD/YYYY 的最新日期顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52181945/

27 4 0
文章推荐: sql-server - SSRS 报告表添加列表达式记录 x of y?
文章推荐: javascript - JS : Why don't the switch statement cases `case 2` and `case day == 2` print out the same result?
文章推荐: javascript - 为什么我在此