How can you identify the year in a smalldatetime column?

  • Exactly as it reads, how can you extract the year in a smalldatetime column.

    I what to make a selection on the year in the smalldatetime column,

    when I used substring it did not work, it's an invalida argument.

    Is there another way to do it, without using a convert or cast?

  • DECLARE @dt SMALLDATETIME

    SET @dt = '2009/01/01'

    SELECT YEAR(@dt)

    SELECT DATEPART(yy,@dt)

    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
  • Like this:

    declare @ADateTime smalldatetime;

    set @ADateTime = '2009-07-05 09:00:00';

    select @ADateTime, year(@ADateTime);

    You may want to spend some time reading Books Online (BOL, the SQL Server Help System that may be accessed from SSMS by pressing the {f1} function key).

    You want to look up functions.

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

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