SQL语句中日期的计算方法大全 (3)

--------------------------------------------------------------------------------

 
 
在sql中,smalldatetime及datetime型的数据显示不是那么让人满意,因此我找了些格式化的方法列出来"

convert(char(10),datetime,101)


在数据库取出来的时候就转换好
 select getdate()
 
 2006-05-12 11:06:08.177
 
 我整理了一下SQL Server里面可能经常会用到的日期格式转换方法:
 
 举例如下:
 select Convert(varchar(10),getdate(),120)

2006-05-12 
 select CONVERT(varchar, getdate(), 120 )
 2006-05-12  11:06:08

select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),\'-\',\'\'),\' \',\'\'),\':\',\'\')
 20060512110608
 
 select CONVERT(varchar(12) , getdate(), 111 )
 2006/05/12
 
 select CONVERT(varchar(12) , getdate(), 112 )
 20060512

select CONVERT(varchar(12) , getdate(), 102 )
 2006.05.12
 
 其它几种不常用的日期格式转换方法:

select CONVERT(varchar(12) , getdate(), 101 )
 0612/2005 select CONVERT(varchar(12) , getdate(), 103 )
 12/09/2004

select CONVERT(varchar(12) , getdate(), 104 )
 12.05.2006

select CONVERT(varchar(12) , getdate(), 105 )
 12-05-2006

select CONVERT(varchar(12) , getdate(), 106 )
 12 05 2006

select CONVERT(varchar(12) , getdate(), 107 )
 05 12, 2006

select CONVERT(varchar(12) , getdate(), 108 )
 11:06:08
 
 select CONVERT(varchar(12) , getdate(), 109 )
 0512 2006 1

select CONVERT(varchar(12) , getdate(), 110 )
 09-12-2004

select CONVERT(varchar(12) , getdate(), 113 )
 12 052006

select CONVERT(varchar(12) , getdate(), 114 )
 11:06:08.177
 

============================================================================================
实例演练

select * from salesmb
select count(*) from salesdreport where employeeid=\'11\'and productsid=\'7\' and (updatetime between \'2006-8-1 00:00:00\'and \'2006-8-30 00:00:00\') --销售数量
select sum(sales) from salesmb where eid=\'11\'and productsid=\'7\' and (mbrq between \'2006-8-1 00:00:00\'and \'2006-8-30 00:00:00\') --目标数量

select dateadd(d,-day(getdate()),getdate())

select cast(year(getdate()) as nvarchar)+\'-\'+cast(month(getdate()) as varchar)+\'-1 00:00\'
select cast(year(getdate()) as nvarchar)+\'-\'+cast(month(getdate()) as varchar)+\'-\'+cast(day(getdate()) as varchar)

select dateadd(m,1,dateadd(d,-day(getdate()),getdate()))


select dateadd(m,1,getdate())


=====================================================================================================================================================


select * from salesmb
select count(*) from salesdreport where employeeid=\'11\'and productsid=\'7\' and (updatetime between \'2006-8-1 00:00:00\'and \'2006-8-30 00:00:00\') --销售数量
select sum(sales) from salesmb where eid=\'11\'and productsid=\'7\' and (mbrq between \'2006-8-1 00:00:00\'and \'2006-8-30 00:00:00\') --目标数量

select dateadd(d,-day(getdate()),getdate())

select cast(year(getdate()) as nvarchar)+\'-\'+cast(month(getdate()) as varchar)+\'-1 00:00\'
select cast(year(getdate()) as nvarchar)+\'-\'+cast(month(getdate()) as varchar)+\'-\'+cast(day(getdate()) as varchar)

select dateadd(m,1,dateadd(d,-day(getdate()),getdate()))


select dateadd(m,1,getdate())


======================================================================================================================================================

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zypwys.html