Questions[SQL]:Calculate the Number of Days in a Month
2008-10-13 10:21
447 查看
Q:Can you use a batch SQL or store procedure to calculating the Number of Days in a Month?
A:
解法一:使用CTE,结构清晰
with arg0 as
(
--计算当月第一天
select cast(
cast(year(getdate()) as varchar)+'-'+cast(month(getdate()) as varchar)+'-01'
as datetime) as nowdt
),
arg1 as
(
--计算下月第一天
select dateadd(mm,1,nowdt) as dt from arg0
),
arg2 as
(
--计算下月减1,即本月最后一天
select dateadd(dd,-1, dt) as dt from arg1
),
arg3 as
(
--取天数部分
select datepart(dd, dt) as daies from arg2
)
select * from arg3
解法二:使用变量
declare @now datetime
set @now= cast(
cast(year(getdate()) as varchar)+'-'+cast(month(getdate()) as varchar)+'-01'
as datetime)
declare @day int
set @day = datepart(dd, dateadd(dd,-1, dateadd(mm,1,@now)))
select @day
解法三:单一语句
select datepart(dd,dateadd(dd,-1,dateadd(mm,1,cast(cast(year(getdate()) as varchar)+'-'+cast(month(getdate()) as varchar)+'-01' as datetime))));
A:
解法一:使用CTE,结构清晰
with arg0 as
(
--计算当月第一天
select cast(
cast(year(getdate()) as varchar)+'-'+cast(month(getdate()) as varchar)+'-01'
as datetime) as nowdt
),
arg1 as
(
--计算下月第一天
select dateadd(mm,1,nowdt) as dt from arg0
),
arg2 as
(
--计算下月减1,即本月最后一天
select dateadd(dd,-1, dt) as dt from arg1
),
arg3 as
(
--取天数部分
select datepart(dd, dt) as daies from arg2
)
select * from arg3
解法二:使用变量
declare @now datetime
set @now= cast(
cast(year(getdate()) as varchar)+'-'+cast(month(getdate()) as varchar)+'-01'
as datetime)
declare @day int
set @day = datepart(dd, dateadd(dd,-1, dateadd(mm,1,@now)))
select @day
解法三:单一语句
select datepart(dd,dateadd(dd,-1,dateadd(mm,1,cast(cast(year(getdate()) as varchar)+'-'+cast(month(getdate()) as varchar)+'-01' as datetime))));
相关文章推荐
- [ActionScript 3.0] AS3 获取某年某月的天数(Get number of days in a month)
- The Maximum Number of Bytes Per Row in SQL Server 2000 -- 8060.
- Calculate difference between two dates (number of days) in C#?
- How to calculate the number of parameters in CNNs?
- sql异常-The used SELECT statements have a different number of columns
- sql: Query to Display Foreign Key Relationships and Name of the Constraint for Each Table in Database
- Sample script that displays all of the users in a given SQL Server DB
- 【Lintcode】Restore IP Addresses, Number of Airplanes in the Sky
- sharepoint2010:The number of items in this list exceeds the list view threshold, which is 20000 items.
- the interview questions of sql server
- Change the value of "Maximum number of concurrent connections" about SQL Server 2012
- Android Error:The number of method references in a .dex file cannot exceed 64K.
- This function has none of Deterministic,no sql,or reads sql data in its declaration and binary logging is enabled(you *might* want to use the less safe log_bin_trust_function_creators variable
- How to choose the number of topics/partitions in a Kafka cluster?
- Sample script that displays all of the users in a given SQL Server DB
- 解决Error in module RSQL of the database interface
- calculate the number of characters-统计文件中的字符数,非空白字符数,字母数,输入到文件和屏幕:
- Warning: Unable to determine the number of frames in this file
- Error: The version of SQL Server in use does not support datatype 'datetime2'
- Given constant integers x and t, write a function that takes no argument and returns true if the function has been called x number of times in last t secs.