 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
CrOri Chris Guest
|
Posted: Thu Feb 09, 2006 11:03 am Post subject: DECODE in MSSQL Server |
|
|
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
|
Posted: Thu Feb 09, 2006 7:03 pm Post subject: Re: DECODE in MSSQL Server |
|
|
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
|
Posted: Fri Feb 10, 2006 5:03 am Post subject: Re: DECODE in MSSQL Server |
|
|
"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
|
Posted: Fri Feb 10, 2006 9:03 am Post subject: Re: DECODE in MSSQL Server |
|
|
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
|
Posted: Fri Feb 10, 2006 10:03 am Post subject: Re: DECODE in MSSQL Server |
|
|
"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 |
|
 |
|
|
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
|
|