Home Database Mysql Tutorial SQL Server和Access中的条件判断语法(case when和iff)

SQL Server和Access中的条件判断语法(case when和iff)

Jun 07, 2016 pm 03:42 PM
access server sql judgment condition grammar

很多时候为了程序的可扩展性和准确性等我们会在数据库中把对应bool类型或string类型的字段设置为int型。但是在程序中显示的时候我们希望能显示用户友好的内容,这个时候我们就需要把查找到的结果转换成用户友好的结果。要达到这个目的有几种方法可行,可以在

很多时候为了程序的可扩展性和准确性等我们会在数据库中把对应bool类型或string类型的字段设置为int型。但是在程序中显示的时候我们希望能显示用户友好的内容,这个时候我们就需要把查找到的结果转换成用户友好的结果。要达到这个目的有几种方法可行,可以在查询的时候直接按照要求判断输出,也可以在查询到结果后在程序中通过相应方法更改结果后显示。本文讨论第一种方法。

使用SQL Server数据库和使用Access数据库所用到的语句不同,下面分别叙述:

SQL Server:

使用case when语句。

语法如下:

case 数据库字段

when 条件1 then 结果1

when 条件2 then 结果2

……

else 上面条件都不满足时的结果

end

点击这里或这里,查看msdn对其的介绍。

例子:select UserName,case UserRole when '0' then '普通用户' when '1' then '管理员' end as 用户角色 from Users

根据长度截断字符串:select case when LEN(Title)>20  then SUBSTRING(Title,0,21) else Title end as Title from Articles


Access:

使用iff()函数。

语法如下:

IIf(expr, truepart, falsepart)

点击这里,查看Microsoft Office对其的介绍。

IIf 函数语法的参数说明:

expr            必需。要计算其值的表达式。

truepart      必需。exprTrue时返回的值或表达式。

falsepart    必需。exprFalse时返回的值或表达式。

意思是:当条件成立时,返回正确结果;当条件不成立时,返回错误结果。

注意:

尽管IIf只返回truepartfalsepart中的一个,但始终会对这两部分进行计算。因此,应当注意是否出现无谓的副作用。例如,如果计算falsepart时导致除数为零错误,那么即使expr为True,也会产生错误。

例子:select iif(IfPaid=1,'是','否') as 是否已付款 from Accounts

根据长度截断字符串:select iif(LEN(Title)>20,left(Title,20),Title) as Title from Articles

注意:

如果是要把数据绑定到DataGridView,而DataGridView的列事先已经定义好了的话,把查询语句中通过此方法获得值的列的as后的定义赋值给对应列的DataPropertyName即可显示出查询到的值。比如把上面语句的Title赋值给DataGridView添加的对应列的DataPropertyName即可显示数据库中原Title列中的值截取前20个字符后的值。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to configure zend for apache How to configure zend for apache Apr 13, 2025 pm 12:57 PM

How to configure Zend in Apache? The steps to configure Zend Framework in an Apache Web Server are as follows: Install Zend Framework and extract it into the Web Server directory. Create a .htaccess file. Create the Zend application directory and add the index.php file. Configure the Zend application (application.ini). Restart the Apache Web server.

Summary of phpmyadmin vulnerabilities Summary of phpmyadmin vulnerabilities Apr 10, 2025 pm 10:24 PM

The key to PHPMyAdmin security defense strategy is: 1. Use the latest version of PHPMyAdmin and regularly update PHP and MySQL; 2. Strictly control access rights, use .htaccess or web server access control; 3. Enable strong password and two-factor authentication; 4. Back up the database regularly; 5. Carefully check the configuration files to avoid exposing sensitive information; 6. Use Web Application Firewall (WAF); 7. Carry out security audits. These measures can effectively reduce the security risks caused by PHPMyAdmin due to improper configuration, over-old version or environmental security risks, and ensure the security of the database.

How to monitor Nginx SSL performance on Debian How to monitor Nginx SSL performance on Debian Apr 12, 2025 pm 10:18 PM

This article describes how to effectively monitor the SSL performance of Nginx servers on Debian systems. We will use NginxExporter to export Nginx status data to Prometheus and then visually display it through Grafana. Step 1: Configuring Nginx First, we need to enable the stub_status module in the Nginx configuration file to obtain the status information of Nginx. Add the following snippet in your Nginx configuration file (usually located in /etc/nginx/nginx.conf or its include file): location/nginx_status{stub_status

What is apache server? What is apache server for? What is apache server? What is apache server for? Apr 13, 2025 am 11:57 AM

Apache server is a powerful web server software that acts as a bridge between browsers and website servers. 1. It handles HTTP requests and returns web page content based on requests; 2. Modular design allows extended functions, such as support for SSL encryption and dynamic web pages; 3. Configuration files (such as virtual host configurations) need to be carefully set to avoid security vulnerabilities, and optimize performance parameters, such as thread count and timeout time, in order to build high-performance and secure web applications.

How to use Debian Apache logs to improve website performance How to use Debian Apache logs to improve website performance Apr 12, 2025 pm 11:36 PM

This article will explain how to improve website performance by analyzing Apache logs under the Debian system. 1. Log Analysis Basics Apache log records the detailed information of all HTTP requests, including IP address, timestamp, request URL, HTTP method and response code. In Debian systems, these logs are usually located in the /var/log/apache2/access.log and /var/log/apache2/error.log directories. Understanding the log structure is the first step in effective analysis. 2. Log analysis tool You can use a variety of tools to analyze Apache logs: Command line tools: grep, awk, sed and other command line tools.

Using Dicr/Yii2-Google to integrate Google API in YII2 Using Dicr/Yii2-Google to integrate Google API in YII2 Apr 18, 2025 am 11:54 AM

VprocesserazrabotkiveB-enclosed, Мнепришлостольностьсясзадачейтерациигооглапидляпапакробоглесхетсigootrive. LEAVALLYSUMBALLANCEFRIABLANCEFAUMDOPTOMATIFICATION, ČtookazaLovnetakProsto, Kakaožidal.Posenesko

Nginx performance monitoring and troubleshooting tools Nginx performance monitoring and troubleshooting tools Apr 13, 2025 pm 10:00 PM

Nginx performance monitoring and troubleshooting are mainly carried out through the following steps: 1. Use nginx-V to view version information, and enable the stub_status module to monitor the number of active connections, requests and cache hit rate; 2. Use top command to monitor system resource occupation, iostat and vmstat monitor disk I/O and memory usage respectively; 3. Use tcpdump to capture packets to analyze network traffic and troubleshoot network connection problems; 4. Properly configure the number of worker processes to avoid insufficient concurrent processing capabilities or excessive process context switching overhead; 5. Correctly configure Nginx cache to avoid improper cache size settings; 6. By analyzing Nginx logs, such as using awk and grep commands or ELK

How to configure Debian Apache log format How to configure Debian Apache log format Apr 12, 2025 pm 11:30 PM

This article describes how to customize Apache's log format on Debian systems. The following steps will guide you through the configuration process: Step 1: Access the Apache configuration file The main Apache configuration file of the Debian system is usually located in /etc/apache2/apache2.conf or /etc/apache2/httpd.conf. Open the configuration file with root permissions using the following command: sudonano/etc/apache2/apache2.conf or sudonano/etc/apache2/httpd.conf Step 2: Define custom log formats to find or

See all articles