Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime query on only year in SQL Server

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, [bookyear] datetime

I want to select all records where the year is 2009.

The following query gives 0 results:

    SELECT [ACCNO]
      ,[Roll No]
      ,[IssueDate]
      ,[DueDate]
  FROM [test1].[dbo].[IssueHis$] 
  where [IssueDate]  between 12-12-2004 and 1-01-2010
like image 799
user1074474 Avatar asked Dec 28 '11 10:12

user1074474


2 Answers

select id,name,bookyear from tab1 where year(bookyear) = 2009
like image 88
dotnetstep Avatar answered Oct 06 '22 00:10

dotnetstep


SELECT [ACCNO]
    ,[Roll No]
    ,[IssueDate]
    ,[DueDate]
FROM [test1].[dbo].[IssueHis$] 
WHERE [IssueDate] >= '20090101' AND
      [IssueDate] < '20100101'
like image 35
Mikael Eriksson Avatar answered Oct 05 '22 22:10

Mikael Eriksson