Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use case when in sql statement

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article is about how to use case when in sql statements. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Sql statement case when usage

When we need to determine the meaning of the data display representation directly from the data source, we can use the function Case When in the SQL statement.

Case comes in two formats. Simple Case function and Case search function.

The first format: simple Case function:

Format description

Case column name

When condition value 1 then option 1

When condition value 2 then option 2.

Else default value end

Select case job_level when'1 'then' 1111 'when' 2 'then' 1111 'when' 3 'then' 1111 'else' eee' end from dbo.employee

The second format: Case search function

Format description

Case

When column name = condition value 1 then option 1

When column name = condition value 2 then option 2.

Else default value end

Eg: update employee set e_wage = case when job_level ='1' then e_wage*1.97 when job_level ='2' then e_wage*1.07 when job_level ='3' then e_wage*1.06 else e_wage*1.05 end

Tip: usually when we write Case When statements, it is easy to forget the end of end, be sure to remember yo!

Comparison: the two formats can achieve the same function.

The writing of the simple Case function is relatively simple, but compared with the Case search function, there are some functional limitations, such as writing judgment. One more thing to note is that the Case function only returns the first qualified value, and the rest of the Case will be automatically ignored.

Thank you for reading! This is the end of the article on "how to use case when in sql sentences". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report