BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

DECODE in MSSQL Server

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (SQL Servers)
View previous topic :: View next topic  
Author Message
CrOri Chris
Guest





PostPosted: Thu Feb 09, 2006 11:03 am    Post subject: DECODE in MSSQL Server Reply with quote



Is there an SQL command like oracle's DECODE in MSSQL?
i am trying to run this query

SELECT
LEFT (NAME,CHARINDEX(' - ',NAME)-1),
RIGHT(NAME,LEN(NAME)-CHARINDEX(' - ',NAME,0)-2)
FROM STOCK

but it seems that there are lines where CHARINDEX(' - ',NAME) returns 0, in
these lines i want '0' as a result and not '-1', and i can't find a way to
do this....

i am going to use this in a migrator so plz don't comment the stupidity of
having 2 rows in one....

thanks
Back to top
Arthur E.F.Heinrich
Guest





PostPosted: Thu Feb 09, 2006 7:03 pm    Post subject: Re: DECODE in MSSQL Server Reply with quote



The decode can be translated to case

decode(a,b,c,d,e,f)

can be

case a
when b then c
when d then e
else f
end

But this will not help you in this query.

[]s
Arthur


"CrOri Chris" <crori49 (AT) hotmail (DOT) com> wrote in message
news:43eb1bfd (AT) newsgroups (DOT) borland.com...
Quote:
Is there an SQL command like oracle's DECODE in MSSQL?
i am trying to run this query

SELECT
LEFT (NAME,CHARINDEX(' - ',NAME)-1),
RIGHT(NAME,LEN(NAME)-CHARINDEX(' - ',NAME,0)-2)
FROM STOCK

but it seems that there are lines where CHARINDEX(' - ',NAME) returns 0,
in
these lines i want '0' as a result and not '-1', and i can't find a way to
do this....

i am going to use this in a migrator so plz don't comment the stupidity of
having 2 rows in one....

thanks

Back to top
Sunil Furtado
Guest





PostPosted: Fri Feb 10, 2006 5:03 am    Post subject: Re: DECODE in MSSQL Server Reply with quote



"CrOri Chris" <crori49 (AT) hotmail (DOT) com> wrote in message
news:43eb1bfd (AT) newsgroups (DOT) borland.com...
Quote:
Is there an SQL command like oracle's DECODE in MSSQL?
i am trying to run this query

SELECT
LEFT (NAME,CHARINDEX(' - ',NAME)-1),
RIGHT(NAME,LEN(NAME)-CHARINDEX(' - ',NAME,0)-2)
FROM STOCK

but it seems that there are lines where CHARINDEX(' - ',NAME) returns 0,
in
these lines i want '0' as a result and not '-1', and i can't find a way to
do this....

You could try something like this:

LEFT (name,case when CHARINDEX(' - ',name)-1<0 then 0 else CHARINDEX(' -
',name)-1 end)


Sunil
Back to top
CrOri Chris
Guest





PostPosted: Fri Feb 10, 2006 9:03 am    Post subject: Re: DECODE in MSSQL Server Reply with quote

it worked!!!!

LEFT (name,case when CHARINDEX(' - ',name)=0 then 0 else CHARINDEX(' -
',name)-1 end)
gave the result, there was a problem with the negative comparing but this
worked fine,
Thank you

Chris


"Sunil Furtado" <sunil (AT) furtado (DOT) de> wrote in message
news:43ec133a (AT) newsgroups (DOT) borland.com...
Quote:

"CrOri Chris" <crori49 (AT) hotmail (DOT) com> wrote in message
news:43eb1bfd (AT) newsgroups (DOT) borland.com...
Is there an SQL command like oracle's DECODE in MSSQL?
i am trying to run this query

SELECT
LEFT (NAME,CHARINDEX(' - ',NAME)-1),
RIGHT(NAME,LEN(NAME)-CHARINDEX(' - ',NAME,0)-2)
FROM STOCK

but it seems that there are lines where CHARINDEX(' - ',NAME) returns 0,
in
these lines i want '0' as a result and not '-1', and i can't find a way
to
do this....

You could try something like this:

LEFT (name,case when CHARINDEX(' - ',name)-1<0 then 0 else CHARINDEX(' -
',name)-1 end)


Sunil

Back to top
Sunil Furtado
Guest





PostPosted: Fri Feb 10, 2006 10:03 am    Post subject: Re: DECODE in MSSQL Server Reply with quote

"CrOri Chris" <crori49 (AT) hotmail (DOT) com> wrote in message
news:43ec5359$1 (AT) newsgroups (DOT) borland.com...
Quote:
it worked!!!!

LEFT (name,case when CHARINDEX(' - ',name)=0 then 0 else CHARINDEX(' -
',name)-1 end)
gave the result, there was a problem with the negative comparing but this
worked fine,
Thank you

Chris


Use +2 instead of -2 for the second part of the name.

RIGHT(name,LEN(name)-case when CHARINDEX(' - ',name)-1<0 then 0 else
CHARINDEX(' - ',name,0)+2 end)

Sunil
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (SQL Servers) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.