Home Forums SQL Server 7,2000 SQL Server Newbies How to update just the year in date fields (non datetime data type) RE: How to update just the year in date fields (non datetime data type)

  • Here is a few to get you started.

    DECLARE @d1 VARCHAR(20)

    DECLARE @d2 NVARCHAR(20)

    DECLARE @d3 VARCHAR(20)

    DECLARE @d4 NVARCHAR(20)

    DECLARE @d5 VARCHAR(20)

    SET @d1 = '2004-06-23'

    SET @d2 = '2004-06-23'

    SET @d3 = '06/14/2011'

    SET @d4 = '06/14/2011'

    SET @d5 = 'May 20 2011'

    SET @d1 = DATEADD(YY,1,@d1)

    SET @d2 = DATEADD(YY,1,@d2)

    SET @d3 = DATEADD(YY,1,@d3)

    SET @d4 = DATEADD(YY,1,@d4)

    SELECT convert(VARCHAR(20),CAST(@d1 AS DATETIME),101) AS 'Cast of d1'

    SELECT convert(VARCHAR(20),CAST(@d2 AS DATETIME),101) AS 'Cast of d2'

    SELECT convert(VARCHAR(20),CAST(@d3 AS DATETIME),101) AS 'Cast of d3'

    SELECT convert(VARCHAR(20),CAST(@d4 AS DATETIME),101) AS 'Cast of d4'

    Results:

    Cast of d1

    --------------------

    06/23/2005

    Cast of d2

    --------------------

    06/23/2005

    Cast of d3

    --------------------

    06/14/2012

    Cast of d4

    --------------------

    06/14/2012

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]