• 主页
• 基础类
• 应用实例
• 新技术前沿

# 请问SELECT能否选出这个月的第一天和最后一天分别做为两个字段？Field1=2003-01-01, Field2=2003-01-31

jerry_huang 维苏威高级陶瓷（苏州）有限公司 商务系统经理  2003-12-23 10:04:33

Field1=2003-01-01
Field2=2003-01-31
...全文
41 点赞 收藏 5

5 条回复

playyuer 2003-12-23
declare @ datetime
set @ = getdate()

DigJim 2003-12-23
http://expert.csdn.net/Expert/FAQ/FAQ_Index.asp?id=182715

SELECT DATEADD(wk, DATEDIFF(wk,0,getdate()), 0)

SELECT DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)

SELECT DATEADD(qq, DATEDIFF(qq,0,getdate()), 0)

), 0)

---------------------------------------------------------------------------------
1)去掉时分秒
declare @ datetime
set @ = getdate() --'2003-7-1 10:00:00'
SELECT @,DATEADD(day, DATEDIFF(day,0,@), 0)

2）显示星期几
select datename(weekday,getdate())

3）如何取得某个月的天数
declare @m int
set @m=2 --月份
select datediff(day,'2003-'+cast(@m as varchar)+'-15' ,'2003-'+cast(@m+1 as varchar)+'-15')

select datediff(day,cast(month(GetDate()) as varchar)+'-'+cast(month(GetDate()) as varchar)+'-15' ,cast(month(GetDate()) as varchar)+'-'+cast(month(GetDate())+1 as varchar)+'-15')

4）判断是否闰年：
SELECT case day(dateadd(mm, 2, dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)))) when 28 then '平年' else '闰年' end

when 28 then '平年' else '闰年' end

5）一个季度多少天
declare @m tinyint,@time smalldatetime
select @m=month(getdate())
select @m=case when @m between 1 and 3 then 1
when @m between 4 and 6 then 4
when @m between 7 and 9 then 7
else 10 end
select @time=datename(year,getdate())+'-'+convert(varchar(10),@m)+'-01'

DigJim 2003-12-23

SELECT DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)

gmlxf 2003-12-23

wzh1215 2003-12-23
try:

select convert(varchar(10),dateadd(day,-datepart(dd,getdate())+1,getdate()),120) as field1

MS-SQL Server

3.2w+

MS-SQL Server相关内容讨论专区