handling of column fields SQL Case Do the column processing Sql> Select deptno,sum (SAL) from the EMP Group by DEPTNO; Select Case When deptno=10 Then ' ACCOUNTING ' When deptno=20 Then ' Reserch ' When deptno=30 Then ' SALES ' End sum (SAL) from EMP GROUP BY Deptno
handling of column fields SQL Decode
Oracle Decode () functions and case statements are all common use, so what is the difference between them? Here's a detailed description of
The difference between the Oracle Decode () function and the case statement for your reference.
First, give 2 simple examples to compare the differences between the 2.
1.CASE statement
SQL statement DECODE () and NVL () function usage SELECT DECODE (WP01.ONDO _ KBN, 0 ,'?? For example, when '1, 'normal temperature ', 2, and 'cold recovery') AS ONDO_KBNFROM WP01_S_HAITOTAL WP01 // analysis: When WP01.ONDO _ KBN = 0 "?? When WP01.ONDO _ KBN = 1, assign "Normal Temperature" to "Normal Temperature". When
not support the boolean Type (there is no other database at hand, and you do not know whether mysql and sqlserver support it ):SQL> create or replace function is_true return booleanIsBeginReturn true;End;/
Function created.
SQL> select is_true from dual;Select is_true from dual*ERROR at line 1:ORA-06552: PL/SQL: Statement
select, or other values you want to define, such as other;
Example:
A table named output is defined, and two columns are defined as monthid (VAR type) and sale (number type). If sale is set to 1000, = 2000 translate to C, = 3000 translate to B, = 4000 translate to a, if other values translate to other;
The SQL statement is as follows:
Select monthid, decode
Tags: return com out table based on tab from if statement berSyntax for the decode () function: 1 Select decode (ColumnName, value 1, translation value 1, value 2, translation value 2,... Value n, translation value n, default value)
2
3 from talbename
4
5 Where ... Where: ColumnName is the column defined in the table to be selected; The default value can be th
The Decode function is a unique method of computing provided by Oracle's SQL software Oracle Pl/sql, known for its concise operations, controllable data models, and flexible format conversions.
Look at other people's SQL today to see that there are decode () function, has n
Case 1: query Table A data, if a column (Param_value) value is too long, the foreground is not good display, only take the first 20 characters, mouse hover and then use the layer to display all values;SQL notation: SELECTM. Param_value as Param_value,decode (sign (Length (m.param_value) -20), 1,concat (SUBSTR (m.param_value,0,20), ' ... '), M. Param_value) as Param_value_msgfrom TableA mIntroduction to the
Tags: default special case ICA To_char else understanding spider. com clearLook at other people's SQL today look at this inside there are also decode () function, has never been in contact with the Internet to check a bit, but also very useful for a function, write down hope to help friends Ah! Introduction to the Decode () function: Main role: Translation of q
Tags: style blog http os io using AR div spIntroduction to the Decode () function in SQLLook at other people's SQL today look at this inside there are also decode () function, has never been in contact with the Internet to check a bit, but also very useful for a function, write down hope to help friends Ah!Introduction to the
Look at other people's SQL today look at this inside there are also decode () function, has never been in contact with the Internet to check a bit, but also very useful for a function, write down hope to help friends Ah!????????Introduction to the Decode () function:Main role: Translation of query results into other values (that is, in other forms of expression,
Tags: select font car redshift span BSP code sort willSELECT * from A ORDER by DECODE (Aa, NULL,Ab) ; Aa Ab -------- ---------- 1 19 null 2 3 1 4 10 with decode When a function's statement is sorted, it is found that the AA field in a row is null when it is given a Span style= "Font-size:medium" > the A
Text transfer from Freespider to WeiboLook at other people's SQL today look at this inside there are also decode () function, has never been in contact with the Internet to check a bit, but also very useful for a function, write down hope to help friends Ah!Introduction to the Decode () function:Main role: Translation of query results into other values (that is,
,=3000 when sale value =1000 translation to a , if other values are translated as other;SQL is as follows:Select Monthid, decode (sale,1000, ' D ', +, ' C ', +, ' B ', 4000, ' A ', ' other ') sale from outputSpecial cases:If you are comparing with only one valueSelect Monthid, decode (sale, NULL, '---', sale) sale from outputThatif (sale==null)Return "---"ElseSal
,=2000 when translated to c,=3000 when sale value =1000 translation to a , if other values are translated as other; SQL is as follows: Select Monthid, decode (sale,1000, ' D ', +, ' C ', +, ' B ', 4000, ' A ', ' other ') sale from output Special cases: If you are comparing with only one value Select Monthid, decode (sale, NULL, '---', sale) sale from output Anoth
Cited:To illustrate this, a score table is created, as shown in:Comparison:1, in SQL, these two functions we can only use case, the code and the results are as follows:Select Name,Case SubjectWhen ' Chinese ' then 1When ' math ' then 2When ' English ' then 3--else 3End as ' account code 'From ResultsSimilarly, we can use case to implement row to column, the code and results are as follows:Select Name,SUM (case when subject= ' language ' then Result en
SQL statement optimization improves database performance and SQL statement database performance
In systems with unsatisfactory performance, apart from the fact that the application load exceeds the server's actual processing capacity, it is more because the system has a large number of
Label:Decode () corresponds to case-when function Case CHARINDEX ('/', start_point_name) When 0 then Start_point_name else substring (start_point_name,1,charindex ('/', start_point_name)-1) End Note: The syntax is different when SQL Server uses case to determine whether it is null Null should be judged by: Case when identifier was NULL then Airway_point_name else identifier end, The simplest way is to use the ISNLL function IsNull (idenfier, Airway_po
Equivalent if statementWhen the decode function compares 1 parametersSELECT Id,decode (inparam, ' becomparedparam ', ' value 1 ', ' Value 2 ') name from bank#如果第一个参数inParam = = ' Becomparedparam ', the name given by select is displayed as a value of 1,#如果第一个参数inParam! = ' Becomparedparam ', then select gets the name displayed as a value of 2When the decode functi
)) whenT.price like 'Price:%' ThenSUBSTR (T.price, -, LENGTH (T.price)- *)ELSE '\ n' ENDPrice , Case whenT.sellername like 'linkman%" " Thensubstr (T.sellername,Ten, Length (T.sellername)-Ten) whenT.sellername like 'linkman%>' Thensubstr (T.sellername,Ten, Length (T.sellername)-185)Else '\ n' EndSellername fromTABLENAME TThe resulting data is more normative:Thus, we can summarize the following case-when syntax: Case when Condition1 Then Sentence1 when Condition2 Then Sentence2 ... ELSE S
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.