Home > database >  SQL, decode, what is the difference between case the when? Their usage?
SQL, decode, what is the difference between case the when? Their usage?

Time:09-17

What is the difference between decode, case the when? Their usage?

CodePudding user response:

Case the when is stronger than the decode function, wait until you meet decode not achieve the function of the, you thought about the case when,

CodePudding user response:

Decode is equivalent to compare, use simple grammar, don't write too long,

Is the case when there are two ways, but a bit more complicated syntax,
- conditions are very flexible
Case
1 then the when condition value 1
2 then the when condition value 2
The else value 3
End

- equivalent to decode
Case value
When value 1 then 11
When value 2 then 22
The else value 33
end

CodePudding user response:

1. Handling of null different
2. To decode knock the word less

CodePudding user response:

Case the when are more powerful than decode, suitable for many conditions, and conditions for more complex situation,
If the condition is simpler decode, can make the code more concise,

CodePudding user response:

Scope of a specific value compare with decode, compared with the case the when

CodePudding user response:

Added: the way the when case value, the decode (null, null, 1) can return 1, but the case of null when null then end cannot return 1
refer to the second floor selling fruit net reply:
decode is equivalent to compare, use simple grammar, don't write too long,

Is the case when there are two ways, but a bit more complicated syntax,
- conditions are very flexible
Case
1 then the when condition value 1
2 then the when condition value 2
The else value 3
End

- equivalent to decode
Case value
When value 1 then 11
When value 2 then 22
The else value 33
End

CodePudding user response:

refer to the sixth floor of tertiary CSDN reply:
added: the way the when case value when the decode (null, null, 1) can return 1, but the case of null when null then end cannot return 1
Quote: refer to the second floor selling fruit net reply:

Decode is equivalent to compare, use simple grammar, don't write too long,

Is the case when there are two ways, but a bit more complicated syntax,
- conditions are very flexible
Case
1 then the when condition value 1
2 then the when condition value 2
The else value 3
End

- equivalent to decode
Case value
When value 1 then 11
When value 2 then 22
The else value 33
End



CodePudding user response:

I think decode is cut version of the case the when, with simple decode judgment logic, logic judgment complex use case the when, case when three or more judgment code readability is higher,

CodePudding user response:

This nothing good of tangled, decode is ORACLE's unique writing is convenient, and the case when is written based on standard SQL syntax, basic all database support

CodePudding user response:

Behind the case when a big advantage is the when can be expressions, and decode judgment can only use the specific value,

CodePudding user response:

The case when the function is stronger than decode,
Decode can realize the function of case the when can, however, on the other hand, not necessarily

CodePudding user response:

Case when understanding, better readability, a see will know that logic, and Oracle and MS SQL are available
Decode should think about it,
Colleagues in decode a heap of decode group, after a week to debug, has been unable to distinguish, took a lot of time to change a good