Reputation: 4451
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
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
Reputation: 763
As simple as (assuming SQL server, given convert() )
select * from mytable where year( convert(datetime, date) ) = year( getDate() )
?
Upvotes: 0