DateTime query on only year in SQL Server

前端 未结 8 1616
灰色年华
灰色年华 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条回答
  • 2021-01-12 08:33
    SELECT [ACCNO]
        ,[Roll No]
        ,[IssueDate]
        ,[DueDate]
    FROM [test1].[dbo].[IssueHis$] 
    WHERE [IssueDate] >= '20090101' AND
          [IssueDate] < '20100101'
    
    0 讨论(0)
  • 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
    0 讨论(0)
提交回复
热议问题