Simple logic processing for query fields in various databases

Source: Internet
Author: User
In specific applications, we often use numeric values to identify various states, such as 0 indicating male, 1 indicating female, or Boolean indicating a logical yes or no State, to make the end user visually see the results, it is clear that it is a good habit to make a simple judgment and processing of the Field Values in the database. You can perform the following operations in access, sqlserver, and Oracle:

In access, select IIF ([field] = 0, 'male', IIF ([field] = 1, 'femal') as Alias from table, in addition, we can add more processing capabilities,

SQL Server uses the select case when field = 0 then 'male' else 'female' end as Alias from table

In Oracle and sqlserver, the syntax is similar to the select case when field = 0 then 'male' else 'female' end as Alias from table. In multiple cases, multiple when then statements can be used for processing.

Under the guidance of the above processing ideas, we can also use more extensive processing methods, such as join table queries and taking access as an example. For example, if data is selected from another table when a certain condition is met, when other conditions are met, use another table ..... for example:
Select IIF ([Status] = 0, (select field from othertable where id = 1), IIF ([Status] = 1, 'femal') as Alias from table

in this way, some application logic can be processed in the database, saving the complexity of Program processing, such as integration. when the net gridview control displays data, it is unnecessary to implement the corresponding conversion logic.

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.