34,593
社区成员
发帖
与我相关
我的任务
分享
select mydate from aa where substring(convert(varchar(20),getdate(),120),0,8)=substring(convert(varchar(20),mydate,120),0,8)
SELECT DATEDIFF(MM,'20091223','20100101')
-----------
1
(所影响的行数为 1 行)
declare @mm varchar(30)
select @mm=substring(convert(varchar(20),getdate(),120),1,7)
print(@mm)
exec('SELECT '''+@mm+'''')
/*
2010-01
-------
2010-01
----------------------------------------------------------------
-- Author :SQL77(只为思齐老)
-- Date :2010-01-16 16:31:14
-- Version:
-- Microsoft SQL Server 2000 - 8.00.194 (Intel X86)
-- Aug 6 2000 00:57:48
-- Copyright (c) 1988-2000 Microsoft Corporation
-- Desktop Engine on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:#A
if object_id('tempdb.dbo.#A') is not null drop table #A
go
create table #A([mydate] datetime)
insert #A
select '20090212' union all
select '20090215' union all
select '20100101' union all
select '20100108' union all
select '20100113'
--------------开始查询--------------------------
SELECT * FROM #A WHERE
CONVERT(CHAR(6),CONVERT(DATETIME,mydate),112)=
CONVERT(CHAR(6),GETDATE(),112)
----------------结果----------------------------
/*
(所影响的行数为 5 行)
mydate
------------------------------------------------------
2010-01-01 00:00:00.000
2010-01-08 00:00:00.000
2010-01-13 00:00:00.000
(所影响的行数为 3 行)
*/
SELECT * FROM TB WHERE CONVERT(CHAR(6),TIME,112)=CONVERT(CHAR(6),GETDATE(),112)
datediff(m,mydate,getdate())=0
--本月数据:
select * from aa where datediff(mm,mydate,getdate())=0