简体   繁体   English

msql 中的 IF 函数

[英]IF FUNCTION in msql

What I need- A column that displays an X for each digit of the card_number column except for the last four digits.我需要什么 - 除了最后四位数字外,card_number 列的每个数字都显示一个 X 的列。

what I need- If the card number contains 16 digits, it should be displayed in this format: XXXX-XXXX-XXXX-1234, where 1234 are the actual last four digits of the number.我需要什么 - 如果卡号包含 16 位数字,则应以这种格式显示:XXXX-XXXX-XXXX-1234,其中 1234 是实际的最后四位数字。

If the card number contains 15 digits, it should be displayed in this format: XXXX-XXXXXX-X1234.如果卡号包含 15 位数字,则应按以下格式显示:XXXX-XXXXXX-X1234。 (Hint: Use an IF function to determine which format to use.) (提示:使用 IF 函数来确定要使用的格式。)

Below is what I got, I need the if function to do the above:以下是我得到的,我需要 if 函数来执行上述操作:

SELECT card_number, 
       length(card_number) as card_number_length,
       right(card_number, 4) AS last_four_digits,
       CONCAT('xxxx-xxxx-xxxx-', + RIGHT (card_number,4)) AS formatted_number
FROM Orders;

Rather simple, use the IF function inside the CONCAT :相当简单,在CONCAT使用IF函数:

SELECT card_number, 
       length(card_number) as card_number_length,
       right(card_number, 4) AS last_four_digits,
       CONCAT(IF(length(card_number)=15, 'XXXX-XXXXXX-X', 'XXXX-XXXX-XXXX-'), RIGHT (card_number,4)) AS formatted_number
FROM Orders;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM