Home Forums SQL Server 2008 T-SQL (SS2K8) Problem with CAST to VARCHAR with SUBSTRING Function RE: Problem with CAST to VARCHAR with SUBSTRING Function

  • I had to use a CONVERT with the SUBSTRING Function as opposed to the CAST.

    This works.

    SELECT

    CASE WHEN CAST(LEFT(Customer.STARTDATE, 2 AS VARCHAR(2)))

    = '98' THEN CAST(substring(Customer.STARTDATE, 3, 2) AS VARCHAR(2))

    WHEN LEFT(CAST(Customer.STARTDATE, 2) AS VARCHAR(2)) = '99' THEN

    SUBSTRING(CONVFERT(VARCHAR(20),(Customer.STARTDATE, 3, 2) AS VARCHAR(2)) END AS LosstMo

    FROM Customer

    Thanks.

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/