none
Type conversion RRS feed

  • Question

  •  

    Can comebody please help me get past this error?

     

    select Name,

    CASE WHEN DATEDIFF (yy ,JoinDate,GETDATE()) < 1
      THEN (CASE WHEN DATEDIFF (M ,JoinDate,GETDATE()) < 1
         THEN DATEDIFF (D ,JoinDate,GETDATE())+'Days'
         ELSE DATEDIFF (M ,JoinDate,GETDATE())+' Months'
         END)
      ELSE DATEDIFF (M ,JoinDate,GETDATE())
    END
    from
    dbo.Customer

    Conversion failed when converting the varchar value ' Months' to data type int.

    • Moved by Tom PhillipsModerator Tuesday, June 22, 2010 3:26 PM TSQL Question (From:SQL Server Database Engine)
    Monday, June 21, 2010 7:04 PM

Answers

  • Hi Sam,

    Try with the following query.

     

    select Name,

     CASE WHEN DATEDIFF (yy ,JoinDate,GETDATE()) < 1

     THEN (CASE WHEN DATEDIFF (M ,JoinDate,GETDATE()) < 1

       THEN CONVERT(varchar(10),DATEDIFF (D ,JoinDate,GETDATE()))+'Days'

       ELSE CONVERT(varchar(10),DATEDIFF (M ,JoinDate,GETDATE()))+' Months'

       END)

     ELSE CONVERT(varchar(10),DATEDIFF (M ,JoinDate,GETDATE()))

    END

    from

    dbo.Customer

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

    Thanks,

    RajaSekhar Reddy .K

    • Marked as answer by Sam1980 Monday, June 21, 2010 7:58 PM
    Monday, June 21, 2010 7:18 PM

All replies

  • Hi Sam,

    Try with the following query.

     

    select Name,

     CASE WHEN DATEDIFF (yy ,JoinDate,GETDATE()) < 1

     THEN (CASE WHEN DATEDIFF (M ,JoinDate,GETDATE()) < 1

       THEN CONVERT(varchar(10),DATEDIFF (D ,JoinDate,GETDATE()))+'Days'

       ELSE CONVERT(varchar(10),DATEDIFF (M ,JoinDate,GETDATE()))+' Months'

       END)

     ELSE CONVERT(varchar(10),DATEDIFF (M ,JoinDate,GETDATE()))

    END

    from

    dbo.Customer

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

    Thanks,

    RajaSekhar Reddy .K

    • Marked as answer by Sam1980 Monday, June 21, 2010 7:58 PM
    Monday, June 21, 2010 7:18 PM
  • Thank you so mcuh RajaSekhar
    Monday, June 21, 2010 7:58 PM