Oracle SQL about Case-when,if-then,decode

Source: Internet
Author: User

Today, when collecting 58-second-hand market data, there is a problem: the style of the Web page is not enough to standardize the data collected, the specific data table is as follows:

This data is not conducive to observation, nor can it be used for analysis. I use Case-when to carry out the preliminary cleaning. (startindex and Endindex in substr need to count ~)

Select         Case  whenT.condition like '<!---color-->%'  Thensubstr (t.condition, -, Length (t.condition)-  -) whent.condition like 'Color:%'  Thensubstr (t.condition, A, Length (t.condition)-  -)Else '\ n' ENDCONDITION, T.classification2, Case  whenT.release_date like '<li class= "Time" title= "Release date" >%'  Thensubstr (T.release_date, to, Length (t.release_date)- *) whent.release_date like '<li class= "Time" >%'  Thensubstr (T.release_date, -, Length (t.release_date)- A) whent.release_date like 'Update Time%'  Thensubstr (T.release_date,6, Length (t.release_date))Else '\ n' ENDRelease_date, Case  whenT.price like '<span%>'  Thensubstr (T.price, -, Length (T.price)- the) whenT.price like '%<'  Thensubstr (T.price, -, Length (T.price)- $)||SUBSTR (T.price,length (T.price)-2, LENGTH (T.price)) 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 T

The resulting data is more normative:

Thus, we can summarize the following case-when syntax:

 Case  when Condition1  Then Sentence1  when Condition2  Then Sentence2 ... ELSE Sentencen END

In addition, If-then-elif-else can also be used.

At first I thought Decode was not applicable here because decode (condition, value 1, translation value 1, value 2, translation value 2,... Value n, translation value n, default value)

The function has the following meanings:
IF condition = value 1 Then
RETURN (translation value 1)
elsif condition = value 2 Then
RETURN (translation value 2)
......
elsif condition = value n Then
RETURN (translated value N)

ELSE
RETURN (default value)
END IF

And I need the condition like value.

Later access to Data discovery: You can decode combine InStr function to achieve "conditional like value":

InStr (STR1,STR2), meaning that if str1 contains str2, returns the position of str2 in str1, otherwise returns 0.

However, this method is not recommended because only Oracle has the DECODE function, which is not good enough in other databases.

Finish.

Oracle SQL about Case-when,if-then,decode

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.