July 12, 2005 at 10:20 am
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
 
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
July 12, 2005 at 10:33 am
Is it a casting error?
July 12, 2005 at 10:33 am
CASE @CheckBDate WHEN 0 THEN @NullBDate ELSE @BDate END AS BDate,
July 12, 2005 at 10:33 am
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
July 12, 2005 at 10:39 am
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
July 12, 2005 at 12:11 pm
Thank you for your help!
CSDunn
July 12, 2005 at 12:13 pm
What was the problem (so that new readers know what to try)?
July 12, 2005 at 12:30 pm
>> 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
July 12, 2005 at 12:34 pm
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 .
July 12, 2005 at 12:38 pm
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
July 12, 2005 at 12:41 pm
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 @$$.
July 12, 2005 at 12:44 pm
Thanks god for the update button!!
* Noel
July 12, 2005 at 12:45 pm
You mean the edit button ??
July 13, 2005 at 12:58 am
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