473,466 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to decode in SQL Server 2005

I am a novice to SQL Server, so this is probably a really easy problem
to fix. I'm translating an Oracle query and need to change the
'decode' to something compatible. Everything I've read points me to
using 'case' but no matter how I write it I can't get it to work and
get a syntax error. Suggestions?

select SYST CTR
, isnull(substring(CD_A, 1, 3), ' ') RESCODE
, DES DESCRIPTION
, decode (substring(CD_A, 1, 3), CODE,PRICE,0) UNIT_PRICE
, count (distinct OR_NO) QTYW
Mar 6 '08 #1
11 13092
da***@yahoo.com wrote:
I am a novice to SQL Server, so this is probably a really easy problem
to fix. I'm translating an Oracle query and need to change the
'decode' to something compatible. Everything I've read points me to
using 'case' but no matter how I write it I can't get it to work and
get a syntax error. Suggestions?

select SYST CTR
, isnull(substring(CD_A, 1, 3), ' ') RESCODE
, DES DESCRIPTION
, decode (substring(CD_A, 1, 3), CODE,PRICE,0) UNIT_PRICE
, count (distinct OR_NO) QTYW
*googles (Oracle "decode function")*

select SYST CTR
, isnull(substring(CD_A, 1, 3), ' ') RESCODE
, DES DESCRIPTION
, case substring(CD_A, 1, 3)
when CODE then PRICE
else 0
end UNIT_PRICE
, count (distinct OR_NO) QTYW
Mar 6 '08 #2
select SYST CTR
, isnull(substring(CD_A, 1, 3), ' ') RESCODE
, DES DESCRIPTION
, case substring(CD_A, 1, 3)
* when CODE then PRICE
* else 0
end UNIT_PRICE
, count (distinct OR_NO) QTYW
Thank you. That worked great!
Mar 6 '08 #3
Oracle now has the CASE expression, too.
Mar 6 '08 #4
--CELKO-- wrote:
Oracle now has the CASE expression, too.
Since version 9.01 which is more than 7 years old.
--
Daniel A. Morgan
University of Washington
da******@x.washington.edu (replace x with u to respond)
Mar 7 '08 #5
"DA Morgan" <da******@psoug.orgwrote in message
news:12***************@bubbleator.drizzle.com...
--CELKO-- wrote:
>Oracle now has the CASE expression, too.

Since version 9.01 which is more than 7 years old.
--
Daniel A. Morgan
University of Washington
da******@x.washington.edu (replace x with u to respond)

Exactly. Yet I still see Oracle developers today using the legacy
non-standard, non-portable features such as the (+) join syntax, DECODE, NVL
(to name just a few) even though Oracle has long since supported the ISO
standard alternatives.

Oracle implemented SQL92 features much later than IBM, Microsoft or Sybase
so I suppose that's one reason why the standard seems less widely adopted on
Oracle. Are there other reasons why some Oracle developers aren't using
standard SQL?

--
David Portas
Mar 8 '08 #6
David Portas wrote:
"DA Morgan" <da******@psoug.orgwrote in message
news:12***************@bubbleator.drizzle.com...
>--CELKO-- wrote:
>>Oracle now has the CASE expression, too.
Since version 9.01 which is more than 7 years old.
--
Daniel A. Morgan
University of Washington
da******@x.washington.edu (replace x with u to respond)


Exactly. Yet I still see Oracle developers today using the legacy
non-standard, non-portable features such as the (+) join syntax, DECODE, NVL
(to name just a few) even though Oracle has long since supported the ISO
standard alternatives.

Oracle implemented SQL92 features much later than IBM, Microsoft or Sybase
so I suppose that's one reason why the standard seems less widely adopted on
Oracle. Are there other reasons why some Oracle developers aren't using
standard SQL?
The main reason is that we use what we know best. Most of us
learned it, it works, and there are no advantages to the change.
The second reason is that the initial implementation 7-8 years
ago had a small bug or two that decreased any initial appeal.

I teach SQL and speaking for myself only use ANSI when I am
doing something that "requires" it such as a full join as some
of the syntax is just plain idiocy (natural join) and the changes
provide no substance.

SELECT COUNT(*)
FROM t1, t2
WHERE t1.c1 = t2.c1;

Ok replace the comma with the word JOIN. (+3 bytes)
Replace the word WHERE with ON. (-3 bytes)
Net change in bytes 0
Net change in functionality 0
Are we getting excited yet? <g>

With outer joins using (+) you get to delay the onset of carpal
tunnel syndrome by a fraction of a second.

The other argument you allude to is portability. And I'm sorry
to say it but that argument, from an Oracle perspective, is nonsense.
The only code that is truly portable is also truly mediocre.

To make Oracle code portable to DB2, Informix, or Sybase would
be a wasted effort as these products are marginalized to niche
markets. To make it portable to SQL Server would require not
using more than 50% of Oracle's functionality. I can understand
why Microsoft might make that argument but it will fall on deaf
ears in an Oracle shop with good reason. Give up regular expressions
because another product doesn't have them? Why? Give up before
triggers because another product doesn't have them? The list is very
long.

But back to the original topic ... CASE was quickly adopted, and the
reason is that it provided new and advantageous functionality over
the older decode.
--
Daniel A. Morgan
Oracle Ace Director & Instructor
University of Washington
da******@x.washington.edu (replace x with u to respond)
Puget Sound Oracle Users Group
www.psoug.org
Mar 8 '08 #7
"DA Morgan" <da******@psoug.orgwrote in message
news:12***************@bubbleator.drizzle.com...
David Portas wrote:
>"DA Morgan" <da******@psoug.orgwrote in message
news:12***************@bubbleator.drizzle.com.. .
>>--CELKO-- wrote:
Oracle now has the CASE expression, too.
Since version 9.01 which is more than 7 years old.
--
Daniel A. Morgan
University of Washington
da******@x.washington.edu (replace x with u to respond)


Exactly. Yet I still see Oracle developers today using the legacy
non-standard, non-portable features such as the (+) join syntax, DECODE,
NVL (to name just a few) even though Oracle has long since supported the
ISO standard alternatives.

Oracle implemented SQL92 features much later than IBM, Microsoft or
Sybase so I suppose that's one reason why the standard seems less widely
adopted on Oracle. Are there other reasons why some Oracle developers
aren't using standard SQL?

The main reason is that we use what we know best. Most of us
learned it, it works, and there are no advantages to the change.
The second reason is that the initial implementation 7-8 years
ago had a small bug or two that decreased any initial appeal.

I teach SQL and speaking for myself only use ANSI when I am
doing something that "requires" it such as a full join as some
of the syntax is just plain idiocy (natural join) and the changes
provide no substance.

SELECT COUNT(*)
FROM t1, t2
WHERE t1.c1 = t2.c1;

Ok replace the comma with the word JOIN. (+3 bytes)
Replace the word WHERE with ON. (-3 bytes)
Net change in bytes 0
Net change in functionality 0
Are we getting excited yet? <g>
But your example query already IS standard in ANSI/ISO 92/99/2003 syntax so
this doesn't advance any argument against the standard.
With outer joins using (+) you get to delay the onset of carpal
tunnel syndrome by a fraction of a second.
Hmm... I think you were right the first time though. People use it because
that's what they know, perhaps without even realising the alternatives. I
would hope that teachers would take the time to educate people about the
newer standard syntax rather than sticking purely or mainly to the old
cliquey ways though. There are millions of people using other database
products who are more familiar with the standard syntax. Even if you never
have to port that's no excuse for obfuscating code that others may inherit
one day.

The other argument you allude to is portability. And I'm sorry
to say it but that argument, from an Oracle perspective, is nonsense.
The only code that is truly portable is also truly mediocre.

To make Oracle code portable to DB2, Informix, or Sybase would
be a wasted effort as these products are marginalized to niche
markets. To make it portable to SQL Server would require not
using more than 50% of Oracle's functionality. I can understand
why Microsoft might make that argument but it will fall on deaf
ears in an Oracle shop with good reason. Give up regular expressions
because another product doesn't have them? Why? Give up before
triggers because another product doesn't have them? The list is very
long.

But back to the original topic ... CASE was quickly adopted, and the
reason is that it provided new and advantageous functionality over
the older decode.
--
Daniel A. Morgan
Oracle Ace Director & Instructor
University of Washington
da******@x.washington.edu (replace x with u to respond)
Puget Sound Oracle Users Group
www.psoug.org

Mar 8 '08 #8
DA Morgan (da******@psoug.org) writes:
The main reason is that we use what we know best.
That's probably why the syntax with the JOIN operators have been so
endorsed in the SQL Server community. Looking back, I can clearly
say that never really understood how *= worked. Yeah, I used it, but
it was always fragile. The ANSI syntax is such a great relief.

Apparently += in Oracle is more cleanly defined.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Mar 8 '08 #9
David Portas wrote:
"DA Morgan" <da******@psoug.orgwrote in message
news:12***************@bubbleator.drizzle.com...
>David Portas wrote:
>>"DA Morgan" <da******@psoug.orgwrote in message
news:12***************@bubbleator.drizzle.com. ..
--CELKO-- wrote:
Oracle now has the CASE expression, too.
Since version 9.01 which is more than 7 years old.
--
Daniel A. Morgan
University of Washington
da******@x.washington.edu (replace x with u to respond)

Exactly. Yet I still see Oracle developers today using the legacy
non-standard, non-portable features such as the (+) join syntax, DECODE,
NVL (to name just a few) even though Oracle has long since supported the
ISO standard alternatives.

Oracle implemented SQL92 features much later than IBM, Microsoft or
Sybase so I suppose that's one reason why the standard seems less widely
adopted on Oracle. Are there other reasons why some Oracle developers
aren't using standard SQL?
The main reason is that we use what we know best. Most of us
learned it, it works, and there are no advantages to the change.
The second reason is that the initial implementation 7-8 years
ago had a small bug or two that decreased any initial appeal.

I teach SQL and speaking for myself only use ANSI when I am
doing something that "requires" it such as a full join as some
of the syntax is just plain idiocy (natural join) and the changes
provide no substance.

SELECT COUNT(*)
FROM t1, t2
WHERE t1.c1 = t2.c1;

Ok replace the comma with the word JOIN. (+3 bytes)
Replace the word WHERE with ON. (-3 bytes)
Net change in bytes 0
Net change in functionality 0
Are we getting excited yet? <g>

But your example query already IS standard in ANSI/ISO 92/99/2003 syntax so
this doesn't advance any argument against the standard.
>With outer joins using (+) you get to delay the onset of carpal
tunnel syndrome by a fraction of a second.

Hmm... I think you were right the first time though. People use it because
that's what they know, perhaps without even realising the alternatives. I
would hope that teachers would take the time to educate people about the
newer standard syntax rather than sticking purely or mainly to the old
cliquey ways though.
We do but the reality is that when they hit the marketplace, in an
Oracle shop, they will find 99.9% of the code with the old format so
they have to know how to read it. And a lot of places having internal
coding standards that they must comply with.
There are millions of people using other database
products who are more familiar with the standard syntax. Even if you never
have to port that's no excuse for obfuscating code that others may inherit
one day.
Using the standard for a product is hardly obfuscation. And the number
of people using those other products is surprisingly smaller than you
think (provided you exclude "users" who never see a line of code). The
only two widely deployed commercial RDBMS platforms for development are
Oracle and SQL Server. The others are niche and/or legacy.
--
Daniel A. Morgan
Oracle Ace Director & Instructor
University of Washington
da******@x.washington.edu (replace x with u to respond)
Puget Sound Oracle Users Group
www.psoug.org
Mar 9 '08 #10
DA Morgan (da******@psoug.org) writes:
Erland Sommarskog wrote:
>Apparently += in Oracle is more cleanly defined.

Excuse me ... but Oracle doesn't have any *= or += syntax.
Did I ever say that I knew Oracle? :-)
If you think one is clearer than the other it is only
because it is the one you know best. I find them both
equal in all respects until you get to the minor players
such as the CROSS JOIN, FULL OUTER JOIN, and the totally
stupid NATURAL JOIN.
Good. *= and LEFT JOIN is definitely not equal in SQL Server.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Mar 9 '08 #11
DA Morgan wrote:
I teach SQL and speaking for myself only use ANSI when I am
doing something that "requires" it such as a full join as some
of the syntax is just plain idiocy (natural join) and the changes
provide no substance.

SELECT COUNT(*)
FROM t1, t2
WHERE t1.c1 = t2.c1;

Ok replace the comma with the word JOIN. (+3 bytes)
Replace the word WHERE with ON. (-3 bytes)
Net change in bytes 0
Net change in functionality 0
Are we getting excited yet? <g>

With outer joins using (+) you get to delay the onset of carpal
tunnel syndrome by a fraction of a second.

The other argument you allude to is portability. And I'm sorry
to say it but that argument, from an Oracle perspective, is nonsense.
The only code that is truly portable is also truly mediocre.
The /other/ other argument is that, once your query gets a little less
trivial than the above, JOIN puts the join conditions closer to the
tables involved, making it easier to understand how they're joined and
harder to introduce an unintended cross join by omitting a condition.
Mar 10 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Pavils Jurjans | last post by:
Hallo, I am working on multilingual web-application, and I have to be very sure about how the international characters are encoded and decoded in the client-server form requests. There's a...
1
by: Sherman H. | last post by:
I have to run a query to give a column a value based on a time range. Can I use DECODE? select decode(trans_date, trans_date>='01-Jul-2002' and trans_date<='30-Jun-2003','Fiscal2002', .....) as...
10
by: N | last post by:
What is the function in SQL that works like DECODE in Oracle?" Thanks, N
0
by: Johann Blake | last post by:
In my need to decode a JPEG 2000 file, I discovered like many that there was no functionality for this in the .NET Framework. Instead of forking out a pile of cash to do this, I came up with the...
5
by: Jonas Åkermark | last post by:
Hello! I have a problem decoding the czech character: r The HTML code for this character is ř but when running Server.HTMLdecode on that string it just returns ř instead of the real char....
7
by: jtfaulk | last post by:
I need to encode some information on the server side using ASP.NET with C#; sending via HTTP to a client side application, that needs to be decoded in an MFC C++ application. I'm not sure if I...
5
by: robert.bull | last post by:
Hello - I am currently working on a project in classic ASP where I receive an XML file that contains an encoded base64 string that I need to do the following to: 1. Decode the base64 string for...
16
by: Medhatithi | last post by:
Hi, I am facing a strange problem with decode function in oracle. My table name is status_hist. Below is the query I am hitting on this table: select...
1
by: dande | last post by:
my problem is I have used invoice table, patient table.In Invoice table date,cash,credit, cheque,advance,total is coming.but in patient table having registration type code having gen registraion...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.