0

我在 SQL Server 中有一个表,其中有一个数据类型为 DateTime 的列,我想获取特定月份或年份的数据,即

Select * from Table where datejoined='07'//Getting July Data

或者

Select * from Table where datejoined='2013'//Getting 2013 Data

我试过这个,但它比较时间的分钟

select * from Table where datejoined like '%07%'
4

5 回答 5

5

你正在寻找的是这样的:

SELECT * 
FROM   TABLE 
WHERE  Year(DATEJOINED) = 2013 

或者:

SELECT * 
FROM   TABLE 
WHERE  Month(DATEJOINED) = 7 
于 2013-08-15T07:35:50.010 回答
2

也许以下解决方案很复杂,但如果您有索引,它将为您提供更好的性能(这意味着 Index Seek)。这些示例基于AdventureWorks2008R2数据库:

SET NOCOUNT ON;
GO
DECLARE @StartDate DATETIME,@EndDate DATETIME;
PRINT 'Test #1: By YEAR';
DECLARE @Year SMALLINT;
SET     @Year=2005;
SET     @StartDate=CONVERT(CHAR(4),@Year)+'0101';
SET     @EndDate=DATEADD(YEAR,DATEDIFF(YEAR,0,@StartDate)+1,0)

SELECT @StartDate AS StartDate,@EndDate AS EndDate
SELECT  h.OrderDate,h.SalesOrderID
FROM    Sales.SalesOrderHeader h
WHERE   h.OrderDate>=@StartDate AND h.OrderDate<@EndDate;

PRINT 'Test #2: By MONTH';
DECLARE @Month TINYINT,@FromYear SMALLINT,@ToYear SMALLINT;
SET     @Month=7;
SELECT  @FromYear=YEAR(MIN(h.OrderDate)),@ToYear=YEAR(MAX(h.OrderDate))
FROM    Sales.SalesOrderHeader h

SELECT @FromYear AS FromYear,@ToYear AS ToYear;

SET     @StartDate=CONVERT(CHAR(4),@Year)+'0101';
SET     @EndDate=DATEADD(YEAR,DATEDIFF(YEAR,0,@StartDate)+1,0);

WITH N10(Num)
AS
(
    SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL 
    SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10
)
SELECT  y.*,h.OrderDate,h.SalesOrderID
FROM
(
    SELECT  x.StartDate,DATEADD(MONTH,DATEDIFF(MONTH,0,x.StartDate)+1,0) AS EndDate
    FROM
    (
        SELECT  CONVERT(DATETIME,CONVERT(CHAR(4),@FromYear-1+n.Num)+RIGHT('00'+CONVERT(VARCHAR(2),@Month),2)+'01') AS StartDate
        FROM    N10 n
        WHERE   n.Num<=(@ToYear+1-@FromYear)
    ) x
) y INNER JOIN Sales.SalesOrderHeader h ON h.OrderDate>=y.StartDate AND h.OrderDate<y.EndDate

结果:

Test #1: By YEAR
StartDate               EndDate
----------------------- -----------------------
2005-01-01 00:00:00.000 2006-01-01 00:00:00.000

OrderDate               SalesOrderID
----------------------- ------------
2005-07-01 00:00:00.000 43659
...
2005-12-31 00:00:00.000 45037


Test #2: By MONTH

FromYear ToYear
-------- ------
2005     2008

StartDate EndDate                 OrderDate               SalesOrderID
--------- ----------------------- ----------------------- ------------
20050701  2005-08-01 00:00:00.000 2005-07-01 00:00:00.000 43659
...
20080701  2008-08-01 00:00:00.000 2008-07-01 00:00:00.000 74159

执行计划:

在此处输入图像描述

于 2013-08-15T08:14:32.400 回答
2

使用 to_char() 函数:

Select * from Table where to_char(datejoined,'dd')='07'     //Getting July Data

Select * from Table where to_char(datejoined,'yyyy')='2014'      //Getting 2014 year Data
于 2014-02-28T11:05:46.870 回答
1

如果在大表上使用这样的列进行计算会产生糟糕的性能。

一年用这个:

SELECT * FROM table where datejoined >= '20130101' and datejoined < '20140101'

如果您遇到性能问题,请将此月用于一个月,它将比较“1950-07-01”和现在之间的所有日期

;with cte as
(
 SELECT convert(date, '1950-07-01', 121) d1, 
        convert(date, '1950-08-01', 121) d2
 UNION ALL
 SELECT dateadd(year, 1, d1), dateadd(year, 1, d2) 
 FROM cte
 WHERE d1 < getdate()
)
SELECT * FROM table t
join cte
ON datejoined >= d1 and
datejoined < d2
于 2013-08-15T08:20:19.673 回答
1

DATEPART似乎很理想:

Select * from Table where DATEPART(month,datejoined) = 7

Select * from Table where DATEPART(year,datejoined) = 2013

虽然有用于获取和的专用函数YEAR,但对于日期时间的时间部分没有这样的等价物。相比之下,可以提取 a 的任何组件。例如,它还可以提取工作日。MONTHDAYDATEPARTdatetime

于 2013-08-15T07:35:53.780 回答