在DATEDIFF函数

时间:2016-04-04 19:26:39

标签: sql-server tsql date datediff

所以我正在学习TSQL,并且已经完成了一个应用程序的任务,该应用程序详细介绍了某些系列电影的内容。我希望添加到成品的其中一件事是DATEDIFF,其中我有主演员的出生日期和电影的首映日期,但是想要在首映时返回演员的年龄。 / p>

我的问题是,如果表格中已经存在这些日期,我如何指定日期,给定DATEDIFF声明?

表格的例子如下。

BondID   FirstName   LastName   Birthdate
1        Sean        Connery    8/25/1930
2        George      Lazenby    9/5/1939
3        Roger       Moore      10/14/1927
4        Timothy     Dalton     3/21/1946
5        Pierce      Brosnan    5/16/1953
6        Daniel      Craig      3/2/1968

薄膜

ID   BondID    FilmTitle             Premiere
1      1       Dr. No                1963-05-08
2      1       From Russia With Love 1974-04-08
3      1       Goldfinger            1964-12-22
4      1       Thunderball           1965-12-29 

这就是我现在所拥有的。

SELECT b.Birthdate, f.Premiere, b.FirstName, b.LastName, f.FilmTitle
FROM Bond b INNER JOIN FilmID  f ON b.BondID = f.BondID
WHERE b.BondID = 1

SELECT DATEDIFF(YEAR, GETDATE(), GETDATE())  AS "YearDif"
WHERE GETDATE() = Bond.Birthdate AND GETDATE() = f.Premiere

数据

     Birthdate  Premiere    FirstName LastName  FilmTitle
1    1930-08-25 1963-05-08  Sean      Connery   Dr. No
2    1930-08-25 1974-04-08  Sean      Connery   From Russia With Love
3    1930-08-25 1964-12-22  Sean      Connery   Goldfinger
4    1930-08-25 1965-12-29  Sean      Connery   Thunderball

我想在首映时添加一个列出演员年龄的专栏...我想我已经掌握了那个......但是......

我知道DATEDIFF部分的第二部分不正确,但是如何告诉DATEDIFF在哪里获取日期值?

任何帮助都会很棒!

2 个答案:

答案 0 :(得分:1)

您的查询:

SELECT DATEDIFF(YEAR, GETDATE(), GETDATE())  AS "YearDif"
WHERE GETDATE() = Bond.Birthdate AND GETDATE() = f.Premiere

无法满足您的需求。 GETDATE()只会返回今天的日期,所以实际上,如果生日和首映版今天是您运行查询的那一天,则上述内容只返回一行。

您需要做的是下面的QUERY部分:

示例数据:

IF OBJECT_ID('tempdb..#Bond') IS NOT NULL
DROP TABLE #Bond
CREATE TABLE #Bond
(BondID INT ,FirstName VARCHAR(20),LastName VARCHAR(20),Birthdate  DATE)

INSERT INTO #Bond
VALUES
 (1        ,'Sean'        ,'Connery'    ,'8/25/1930')
,(2        ,'George'      ,'Lazenby'    ,'9/5/1939')
,(3        ,'Roger'       ,'Moore'      ,'10/14/1927')
,(4        ,'Timothy'     ,'Dalton'     ,'3/21/1946')
,(5        ,'Pierce'      ,'Brosnan'    ,'5/16/1953')
,(6        ,'Daniel'      ,'Craig'      ,'3/2/1968')

IF OBJECT_ID('tempdb..#Film') IS NOT NULL
DROP TABLE #Film

CREATE TABLE #Film
(ID  INT , BondID  INT , FilmTitle       VARCHAR(50),      Premiere  DATE)
INSERT INTO #Film
VALUES (1    ,  1,'Dr. No'              ,'1963-05-08')
,(2    ,  1,'From Russia With Love','1974-04-08')
,(3    ,  1,'Goldfinger'           ,'1964-12-22')
,(4    ,  1,'Thunderball'          ,'1965-12-29')

QUERY:

    SELECT b.Birthdate, f.Premiere, b.FirstName, b.LastName, f.FilmTitle , DATEDIFF(YEAR, b.Birthdate, Premiere) AS AgeOfActor
FROM #Bond b INNER JOIN #Film  f ON b.BondID = f.BondID 

结果:

enter image description here

对评论的回应:

你可以随时计算天数,然后再按365:

SELECT b.Birthdate, f.Premiere, b.FirstName, b.LastName, f.FilmTitle , DATEDIFF(day, b.Birthdate, Premiere)/365 AS AgeOfActor
FROM #Bond b INNER JOIN #Film  f ON b.BondID = f.BondID 

enter image description here

或者为了更准确,您可以在几秒钟内完成:P

SELECT b.Birthdate, f.Premiere, b.FirstName, b.LastName, f.FilmTitle ,  DATEDIFF(second, b.Birthdate, Premiere)/(365.25*24*60*60)   AS AgeOfActor
FROM #Bond b INNER JOIN #Film  f ON b.BondID = f.BondID 

enter image description here

答案 1 :(得分:0)

这样的事情只需一步就可以在SQL中完成。没有必要将中间结果存储在任何地方以便稍后传递它们。

您只需要将表中的列值传递给函数调用,如下所示:

SELECT 
 b.Birthdate, f.Premiere, b.FirstName, b.LastName, f.FilmTitle,
 DATEDIFF(YEAR, b.Birthdate, f.Premiere) AS actor_age_at_premiere
FROM 
 Bond b 
 INNER JOIN FilmID f 
  ON b.BondID = f.BondID

假设演员在他/她演奏的电影的首映日期之前出生(这是一个公平的假设),结果将是积极的Integer

如果你想包括截至目前的演员年龄,那么你的第三个参数将是GETDATE()而不是Premiere列:

DATEDIFF(Year, b.Birthdate, GETDATE()) AS actor_age_current