DateTime query on only year in SQL Server

前端 未结 8 1622
灰色年华
灰色年华 2021-01-12 08:01

I want to select multiple records on the basis of a matching year, for example in table tab where columns are [id] int, [name] varchar, [boo

8条回答
  •  -上瘾入骨i
    2021-01-12 08:34

    You could create a view that has the IssueYear as a separate column

    CREATE VIEW vIssueHis
    AS
    SELECT
    [ACCNO],
    [Roll No],
    [IssueDate],
    [DueDate],
    DATEPART(yyyy,IssueDate) as IssueYear,
    DATEPART(yyyy,DueDate) as DueYear
    FROM [test1].[dbo].[IssueHis$]

    Then you could query the view like this

    SELECT [ACCNO]
          ,[Roll No]
          ,[IssueDate]
          ,[DueDate]
    FROM vIssueHis
    WHERE IssueYear = 2009

提交回复
热议问题