Eran Meir
Eran Meir

Reputation: 983

SQL Server 2005 : how to check for empty string

I have a Procedure which returns MobileAreaCode+Mobile if they are not null I just want to add to it support for empty strings as well I tried(without the handle for empty string it works)

ALTER PROCEDURE PROC_NAME
    @Identification INT
AS
BEGIN
    SELECT 
    CASE WHEN MobileAreaCode is NOT NULL OR Mobile is NOT NULL 
OR MobileAreaCode<>'' OR Mobile<>''
    THEN
        MobileAreaCode+Mobile
    END
    FROM 
        TABLE_NAME
    WHERE 
        id = 123456789
END
GO

which doesn't work and results the following error:

Incorrect syntax near the keyword 'FROM'.

Upvotes: 0

Views: 3222

Answers (2)

Mikael Eriksson
Mikael Eriksson

Reputation: 139010

You can use Use NULLIF (Transact-SQL).

select nullif(MobileAreaCode, '')+nullif(Mobile, '') as MobileAreaCodeMobile
from YourTable

SQL Fiddle

Upvotes: 1

Simple Fellow
Simple Fellow

Reputation: 4622

ALTER PROCEDURE PROC_NAME
@Identification INT
AS
BEGIN
SELECT 
CASE WHEN (MobileAreaCode is NOT NULL) AND (Mobile is NOT NULL) 
AND (len(MobileAreaCode)>0) AND (len(Mobile)>0)
THEN
    MobileAreaCode+Mobile
END
FROM 
    TABLE_NAME
WHERE 
    id = 123456789
END
GO

Upvotes: 0

Related Questions