Select Records based on Year

  • I am trying to select records based on a year.

    Any help would be greatly appreciated.

    DECLARE @Date

    SET @Date = 2012

    DECLARE @Year int

    SET @Year = (SELECT DATEPART(yyyy,@Date))

    SELECT @Year AS Year

    SELECT *

    FROM [Orders].[dbo].[Orders] od

    WHERE DATEPART(yyyy,@Date)= @Year

  • Well you don't say what column in the table has the date in, and comparing one variable to another isn't going to do much.

    Should get you started:

    DECLARE @StartOfYear DATE

    SET @StartOfYear = '2012-01-01'

    SELECT *

    FROM [Orders].[dbo].[Orders] od

    WHERE SomeDateColumn >= @Date AND SomeDateColumn < DATEADD(yy,1,@StartOfYear);

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply