Home > Database > Mysql Tutorial > SQL SERVER对月/周处理

SQL SERVER对月/周处理

WBOY
Release: 2016-06-07 16:19:29
Original
1098 people have browsed it

1.显示本月第一天 SELECT DATEADD(mm,DATEDIFF(mm,0,getdate()),0) select convert(datetime,convert(varchar(8),getdate(), 120)+'01',120) 2.显示本月最后一天 select dateadd(day,-1,convert(datetime,convert (varchar(8),dateadd(month,1,getdate()

   1.显示本月第一天

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

  select convert(datetime,convert(varchar(8),getdate(),

  120)+'01',120)

  2.显示本月最后一天

  select dateadd(day,-1,convert(datetime,convert

  (varchar(8),dateadd(month,1,getdate()),120)+'01',120))

  SELECT dateadd(ms,-3,DATEADD(mm,DATEDIFF(m,0,getdate())+1,0))

  3.上个月的最后一天

  SELECT dateadd(ms,-3,DATEADD(mm,DATEDIFF(mm,0,getdate()),0))

  4.本月的第一个星期一i

  select DATEADD(wk,DATEDIFF(wk,0, dateadd(dd,6-datepart(

  day,getdate()),,getdate())),0)

  5.本年的第一天

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

  6.本年的最后一天

  SELECT dateadd(ms,-3,DATEADD(yy,DATEDIFF(yy,0,getdate())+1,0))

  7.去年的最后一天

  SELECT dateadd(ms,-3,DATEADD(yy,DATEDIFF(yy,0,getdate()),0))

  8.本季度的第一天

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

  9.本周的星期一

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

  10.查询本月的记录

  select * from tableName where DATEPART(mm, theDate)

  =DATEPART(mm, GETDATE()) and DATEPART(yy, theDate)

  = DATEPART(yy, GETDATE())

  11.查询本周的记录

  select * from tableName where DATEPART(wk, theDate) = DATEPART

  (wk, GETDATE()) and DATEPART(yy, theDate) = DATEPART(yy, GETDATE())

  12查询本季的记录 注:其中:GETDATE()是获得系统时间的函数。

  select * from tableName where DATEPART(qq, theDate) = DATEPART

  (qq, GETDATE()) and DATEPART(yy, theDate) = DATEPART(yy, GETDATE())

  13.获取当月总天数:

  select DATEDIFF(dd,getdate(),DATEADD

  (mm, 1, getdate()))

  select datediff(day,

  dateadd(mm, datediff(mm,'',getdate()), ''),

  dateadd(mm, datediff(mm,'',getdate()), '1900-02-01'))

  14.获取当前为星期几

  DATENAME(weekday, getdate())

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template