Francisco Carvalho
Francisco Carvalho

Reputation: 4451

SQL Get data in this year

i have a DB with a atribute "Date" which is a string ...

its format is: "Jan 5 2014 6:26 PM" and would like to get the number of rows where the date is this year.

already know how to convert the date:

SELECT convert(datetime, 'Oct 23 2012 11:01AM')

i found this code but I do not know how to join the two

select * from datetimes2
where dtm2 >= CAST(CURRENT_TIMESTAMP AS DATE)
and dtm2 < DATEADD(DD, 1, CAST(CURRENT_TIMESTAMP AS DATE))
;

Now I do not know how to do what I want :(

Upvotes: 0

Views: 139

Answers (2)

Karthik D V
Karthik D V

Reputation: 926

Is this what you looking for ?

DECLARE @datetime DATETIME = 'Jan 5 2014 6:26 PM' 
SELECT 
* 
FROM datetimes2
WHERE [Date] >= CONVERT(DATETIME, CAST(DATEPART(YEAR, @datetime) AS VARCHAR) + '-01-01')
  AND [Date] <= CONVERT(DATETIME, CAST(DATEPART(YEAR, @datetime) AS VARCHAR) + '-12-31')

This will get the records for complete year 2014. Hope this helps!

Upvotes: 1

Joe Rinehart
Joe Rinehart

Reputation: 763

As simple as (assuming SQL server, given convert() )

select * from mytable where year( convert(datetime, date) ) = year( getDate() )

?

Upvotes: 0

Related Questions