用户登录
用户注册

分享至

从某个日期开始计算年数 SQL

  • 作者: -趴在墙头等红杏
  • 来源: 51数据库
  • 2023-02-07

问题描述

我想计算从这个人出生到今天的年数.我有要使用的组件,但不知道如何使用它们.

I want to count the amount of years from a the year the person is born to today. I have the components to use but dont know how to use them.

SELECT COLUMN1, COLUMN2, DATEPART(YY,GETDATE()),
CONVERT(INT,+19)))LEFT(TABLE.COLUMN,2)

我想用 +19 在出生年之前显示它.例如,在数据库中,出生年份显示为 YY 而不是 YYYY.这就是为什么我要添加 19,所以 SQL 将计算从 19YY 到 2013 的年数.

I want to use the +19 to show it before the birtyear. Example in the database the birthyear is showed as YY not YYYY. That is why I want to add the 19, so the SQL will count years from 19YY to 2013.

推荐答案

试试这个

SELECT  id
    ,   Name
    ,   DATEDIFF(yy, CONVERT(DATETIME, DOB), GETDATE()) AS AGE
    ,   DOB
FROM MyTable
软件
前端设计
程序设计
Java相关