Problem with CASE stmt

  • Hello,

    I am trying to use CASE in a SELECT list that provides data for an INSERT. I keep getting a syntax error on a line in the SELECT stmt where the CASE stmt is (BDate). The following is the code:

    *****************************

    CREATE PROCEDURE dbo.st_POS_INSERT_ClientInfoCkTobaccoCode

    @EntrCode NVARCHAR(3),--required

    @AppId INTEGER, --required

    @ClientId INTEGER, --required

    @ClientType NVARCHAR(1),

    @FirstName NVARCHAR(20),

    @MidName NVARCHAR(20),

    @LastName NVARCHAR(30),

    @BDate DATETIME,

    @BirthState NVARCHAR(3),

    @BirthCountry NVARCHAR(10)

    AS

    DECLARE @CheckBDate INTEGER, @NullBDate INTEGER

    SET @CheckBDate = CAST(@BDate AS INTEGER)

    SET @NullBDate = NULL

    INSERT INTO snbat011_app_clnt

         (entr_cd,

          app_id,

          clnt_id,

          clnt_type,

          clnt_frst_name,

          clnt_mid_name,

          clnt_lst_name,

          clnt_dob,

          clnt_brth_st,

          clnt_brth_cntry

         &nbsp

       

    SELECT

     @EntrCode,

     @AppId,

     @ClientId,

     '', -- client type

     @FirstName,

     @MidName,

     @LastName,

     BDate = CASE @CheckBDate WHEN = 0 THEN @NullBDate ELSE @BDate END,

     @BirthState,

     @BirthCountry

    **********************************

    Can I not use a CASE statement this way?

    Thanks!

    CSDunn

  • Is it a casting error?

  • CASE @CheckBDate WHEN 0 THEN @NullBDate ELSE @BDate END AS BDate,

  • you should use the searched way instead:

    CASE WHEN @CheckBDate  = 0 THEN @NullBDate ELSE @BDate END

    because the types of the expresssions are not the same

     


    * Noel

  • No,

    What I meant is that if you are going to use the way that the poster issued all expressions involved should either be of the same type or implicitely converrtible otherwise you have to use the searched version of case which is what we posted

      


    * Noel

  • Thank you for your help!

    CSDunn

  • What was the problem (so that new readers know what to try)?

  • >> What was the problem (so that new readers know what to try)?  <<

    I think you are reading too fast ( or not ?) today 

    He was getting a syntax error (Do to the implicit conversion issue... read my post )

     


    * Noel

  • There was 2 slightly different solutions posted... maybe one of them didn't work for him (however not likely). Not everyone here is a seasoned DBA Noeld .

  • I must admit that it was me who didn't read your post carefully 

     

    CASE @CheckBDate WHEN = 0 THEN @NullBDate ELSE @BDate END AS BDate

    should have been

    CASE @CheckBDate WHEN 0 THEN @NullBDate ELSE @BDate END AS BDate

    no "="

     


    * Noel

  • Now that's 3 solution. Only 2 workings though . I should stop taking those for granted and check the spelling anyways... always the small things that byte you in the @$$.

  • Thanks god for the update button!!


    * Noel

  • You mean the edit button ??

  • all u need to do is not          BDate = CASE @CheckBDate WHEN = 0

     

    BDate = CASE WHEN  @CheckBDate = 0

Viewing 14 posts - 1 through 13 (of 13 total)

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