如何将从日期部分获得的数字转换为日期名称?

发布于 2024-07-05 03:47:58 字数 158 浏览 7 评论 0原文

是否有一种快速的方法可以在 Sql Server 中调用 datepart 并返回当天的名称而不仅仅是数字?

select datepart(dw, getdate());

这将返回 1-7,周日为 1。我想要“周日”而不是 1。

Is there a quick one-liner to call datepart in Sql Server and get back the name of the day instead of just the number?

select datepart(dw, getdate());

This will return 1-7, with Sunday being 1. I would like 'Sunday' instead of 1.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

音栖息无 2024-07-12 03:47:58
select datename(weekday, getdate());
select datename(weekday, getdate());
素手挽清风 2024-07-12 03:47:58

实际上,我花了比我想象的更多的搜索才能找到这个答案。 有趣的是,你可以使用一项技术多年,却从来不知道像这样的简单功能。

select datename(dw,  getdate())

我不确定本地化如何与此功能配合使用。 在客户端获取名称可能是答案,但最好在数据库上执行此操作。 Sql Server 会使用排序规则设置来确定其输出吗?

It actually took me more searching than I thought it would to find this answer. It's funny how you can use a technology for ages and never know about simple functions like this.

select datename(dw,  getdate())

I'm not sure how localization would work with this function. Getting the name client-side is probably the answer, but it would be nice to do it on the database. Would Sql Server use the collation setting to determine the output for this?

┾廆蒐ゝ 2024-07-12 03:47:58

如果不使用结果自行选择日期,这是不可能的。 一方面,当天的文本表示取决于区域设置。 另一方面,返回的值取决于“datefirst”设置。

This is not possible without using the result to select the day yourself. For one thing the textual representation of the day is locale-dependent. For another the returned value depends upon the 'datefirst' setting.

无人问我粥可暖 2024-07-12 03:47:58

如果您想要一个可本地化的解决方案,只需将结果与包含名称和数字的表连接起来即可。

If you want a localizable solution, just join the result against a table with the names and numbers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文