views:

147

answers:

5

SQL Syntax is still something I am learning. I am getting the error noted below the this snippet of code.

SELECT
 CASE  WHEN    LTRIM(RTRIM(cLehmanNo)) =' ' THEN NULL 
       WHEN cLehmanNo IS NOT NULL  THEN REPLACE ( cLehmanNo,SUBSTRING (cLehmanNo,PATINDEX( '%[^a-zA-Z0-9 '''''']%',cLehmanNo),1), ' ' )
 END asLOAN_NUMBER
,CASE  WHEN    LTRIM(RTRIM(cMERS)) =' ' THEN NULL 
       WHEN cMERS IS NOT NULL  THEN REPLACE ( cMERS,SUBSTRING (cMERS,PATINDEX( '%[^a-zA-Z0-9 '''''']%',cMERS),1), ' ' )
 END asMERS_ID

and 100+ more of same.

Msg 8133, Level 16, State 1, Line 1
None of the result expressions in a CASE specification can be NULL.

What am I doing wrong? How do I keep the gist of the statement and not get this crazy error?

+2  A: 

You need to convert NULL to a correct type matching the overall values, e.g. CONVERT(VARCHAR(10), NULL), otherwise the server can't deduce which type to make the resulting value.

DVK
or CAST(NULL AS VARCHAR(10));
a1ex07
@a1ex07 - Ack... stuck in Sybase mindset
DVK
Ah, okay I understand. I tried a couple of other things and found that this works: CASE WHEN LTRIM(RTRIM(cLehmanNo)) ='' THEN NULL WHEN cLehmanNo IS NOT NULL THEN REPLACE ( cLehmanNo,SUBSTRING (cLehmanNo,PATINDEX( ' %[^a-zA-Z0-9 '''''']%' ,cLehmanNo),1), ' ' )END as LOAN_NUMBER (had to remove an additional Space on ' ' after the = sign. Then I had found later in the code where I had to add THEN REPLACE(Null,'','') prior END as in the following: ,CASE WHEN LTRIM(RTRIM(dtMicIssue)) ='' THEN NULL WHEN cMICNumber IS NOT NULL THEN REPLACE (NULL, '', '') END as MIP_CERTIFICATION_DATEThx!
JMS49
A: 

This happens when it can't infer the type.

e.g.

SELECT CASE WHEN 1 = 2 THEN NULL ELSE NULL END 

But this works

SELECT CASE WHEN 1 = 2 THEN NULL ELSE replace(NULL,'','') END

so I doubt the error is from the code you have shown us (You are using string functions and the following quick test shows that it will assume that to be varchar(8000))

SELECT CASE WHEN 1 = 2 THEN NULL ELSE REPLACE(NULL,'','') END a
INTO t /*Creates column of datatype varchar(8000)*/
Martin Smith
+1  A: 

The error message actually means that all results in one of your case expressions are null. You have an expression like:

case when something then null when something then null end

At least one of the results has to be something other than null. You could circumvent this, but most likely there is a mistake in the query, as a case exression that always returns the same result is pointless.

The error message has been changed to:

At least one of the result expressions in a CASE specification must be an expression other than the NULL constant.

Guffa
+1 I think there must be a mistake in the query as well if all parts return the NULL constant.
Martin Smith
A: 
SELECT
 CASE  WHEN    LTRIM(RTRIM(cLehmanNo)) =' ' THEN NULL 
       WHEN cLehmanNo IS NOT NULL  THEN REPLACE ( cLehmanNo,SUBSTRING (cLehmanNo,PATINDEX( '%[^a-zA-Z0-9 '''''']%',cLehmanNo),1), ' ' )

ELSE ''

 END asLOAN_NUMBER
,CASE  WHEN    LTRIM(RTRIM(cMERS)) =' ' THEN NULL 
       WHEN cMERS IS NOT NULL  THEN REPLACE ( cMERS,SUBSTRING (cMERS,PATINDEX( '%[^a-zA-Z0-9 '''''']%',cMERS),1), ' ' )

ELSE ''

 END asMERS_ID
Conrad Frix
Adding Else '' won't make any difference I think. This doesn't raise the error `SELECT CASE WHEN 1 = 2 THEN NULL WHEN 2 = 3 THEN REPLACE(NULL,'','') END`
Martin Smith
A: 

On a slight tangent, this looks like a good case for using a User Defined Function:

create function dbo.PrepareString (@input varchar(100))
returns varchar(100)
AS
begin
    set @input = ltrim(ltrim(isnull(@input, '')))
    if( @input = '' )
        return null
    return replace(@input, substring(@input, patindex('%[^a-zA-Z0-9 '''''']%', @input), 1), ' ')
end

Then your select statement gets really simple:

select
    dbo.PrepareString(cLehmanNo) as asLOAN_NUMBER,
    dbo.PrepareString(cMERS) as asMERS_ID

If there are any changes later down the line, you only have to modify the one function and all columns in your select statement will return the updated result.

dana