如何确定SQL Server中一个月的天数? [英] How to determine the number of days in a month in SQL Server?

查看:212
本文介绍了如何确定SQL Server中一个月的天数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要确定SQL Server中给定日期的一个月的天数。

I need to determine the number of days in a month for a given date in SQL Server.

是否有内置函数?如果没有,我应该如何用作用户定义的函数?

Is there a built-in function? If not, what should I use as the user-defined function?

推荐答案

您可以在第一天使用以下内容指定月份:

You can use the following with the first day of the specified month:

datediff(day, @date, dateadd(month, 1, @date))

使其适用于每个日期:

datediff(day, dateadd(day, 1-day(@date), @date),
              dateadd(month, 1, dateadd(day, 1-day(@date), @date)))

这篇关于如何确定SQL Server中一个月的天数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆