Home All Groups Group Topic Archive Search About

Formatting in the SELECT statement help needed.

Author
30 Jun 2005 4:52 PM
SteveInBeloit
Hi,
I am trying to replicate this statement in a SELECT:

IIf(IsNull([cmZip4]),[cityst] & "  " & [cmZip],[cityst] & "  " & [cmZip] &
"-" & [cmZip4]) AS CityStZip

If cmZip4 is null, I want to format cityst + " " + cmzip, if it is not null,
I want to format it as cityst + "" cmZip + "-" + cmZip4

In my SELECT, I want to fomat this AS cityStzip. 

I am not having much luck, any thoughts or help is appriciated!

Thanks,
Steve

Author
30 Jun 2005 4:56 PM
--CELKO--
In a tiered archtecture, format is never done in the database; that is
a front end job.
Author
30 Jun 2005 4:58 PM
Alejandro Mesa
Try,

select
    coalesce([cityst] + "  " + [cmZip] + "-" + [cmZip4], [cityst] + "  " +
[cmZip]) as CityStZip
....


AMB

Show quote
"SteveInBeloit" wrote:

> Hi,
> I am trying to replicate this statement in a SELECT:
>
> IIf(IsNull([cmZip4]),[cityst] & "  " & [cmZip],[cityst] & "  " & [cmZip] &
> "-" & [cmZip4]) AS CityStZip
>
> If cmZip4 is null, I want to format cityst + " " + cmzip, if it is not null,
> I want to format it as cityst + "" cmZip + "-" + cmZip4
>
> In my SELECT, I want to fomat this AS cityStzip. 
>
> I am not having much luck, any thoughts or help is appriciated!
>
> Thanks,
> Steve
Author
1 Jul 2005 3:55 PM
JosephPruiett
Hope this helps.
/*
IIf(IsNull([cmZip4]),[cityst] & "  " & [cmZip],[cityst] & "  " & [cmZip] &
"-" & [cmZip4]) AS CityStZip

If cmZip4 is null, I want to format cityst + " " + cmzip, if it is not null,
I want to format it as cityst + "" cmZip + "-" + cmZip4

In my SELECT, I want to fomat this AS cityStzip. 
*/

Create table #Address
(
cityst varchar(20),
cmZip  varchar(5),
cmZip4 varchar(4)
)

Insert #address
values ('Dallas, Tx','75229',Null)


Insert #address
values ('Plano, Tx','75252','1234')

SELECT CASE WHEN cmZip4 IS NULL
            THEN cityst + ' ' + cmZip
            WHEN cmZip4 IS NOT NULL
            THEN cityst + ' ' + cmZip +'-'+ cmZip4
       END
    FROM #address

--output

Show quote
"SteveInBeloit" wrote:

> Hi,
> I am trying to replicate this statement in a SELECT:
>
> IIf(IsNull([cmZip4]),[cityst] & "  " & [cmZip],[cityst] & "  " & [cmZip] &
> "-" & [cmZip4]) AS CityStZip
>
> If cmZip4 is null, I want to format cityst + " " + cmzip, if it is not null,
> I want to format it as cityst + "" cmZip + "-" + cmZip4
>
> In my SELECT, I want to fomat this AS cityStzip. 
>
> I am not having much luck, any thoughts or help is appriciated!
>
> Thanks,
> Steve

AddThis Social Bookmark Button