SQL Server ->> CONCAT函数

本文深入探讨了一个SQLServer2012后引入的函数,该函数用于将NULL值转换为空字符串,并支持多种列输入及复杂数据类型。通过实验证明其对多种基本数据类型具有良好的兼容性。

这是一个SQL Server 2012后引进的新函数。作用就如同它名字的意思。它对NULL值得处理是空字符串。当然它能做的不仅是对字符的支持。它支持N个列输入,列的类型支持更加完善。不过其实它的原理不过是把传入的数据用CONVERT函数转换成NVARCHAR类型。不信你传个XML或者SQL_VARIANT类型的数据进去,你会看到马上报错。错误提示是:

Msg 257, Level 16, State 3, Line 35
Implicit conversion from data type xml to nvarchar is not allowed. Use the CONVERT function to run this query.

 

至少我测试结果显示它对INT, FLOAT, DATETIME, NVARCHAR都是支持的。

测试脚本

WITH T AS 
(
SELECT TOP 10 
CAST(Num AS INT) AS int_fld,
CAST(Num AS NVARCHAR) AS str_fld,
CAST(Num AS FLOAT) AS flt_fld,
CAST(NULL AS NVARCHAR) AS NULL_FLD,
CAST(Num AS datetime) AS dt_fld
--CAST(Num AS sql_variant) AS var_fld,
--CAST('<a>aa</a>'AS xml) AS xml_fld
FROM dbo.Numbers 
)

SELECT CONCAT(int_fld,str_fld,flt_fld,NULL_FLD, dt_fld)
FROM  T

 

转载于:https://www.cnblogs.com/jenrrychen/p/5164997.html

[root@yfw ~]# cd /www/wwwroot/szrengjing.com [root@yfw szrengjing.com]# mysql -u szrengjing_com -pZparETNy4DTZBAiT szrengjing_com mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 100269 Server version: 5.7.42-log Source distribution Copyright (c) 2000, 2023, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> DROP TRIGGER IF EXISTS tr_after_update_credit; Query OK, 0 rows affected (0.00 sec) mysql> DELIMITER ;; mysql> CREATE TRIGGER tr_after_update_credit -> AFTER UPDATE ON ecs_user_credit -> FOR EACH ROW -> BEGIN -> -- 只有当分数发生变化时才记录 -> IF OLD.score <> NEW.score THEN -> INSERT INTO ecs_user_credit_log ( -> user_id, -> change_value, -> old_score, -> new_score, -> reason -> ) VALUES ( -> NEW.user_id, -> NEW.score - OLD.score, -> OLD.score, -> NEW.score, -> CONCAT('自动记录: ', NEW.score - OLD.score) -> ); -> END IF; -> END;; Query OK, 0 rows affected (0.02 sec) mysql> DELIMITER ; mysql> CALL sp_add_credit_by_action(1, 'checkin'); ERROR 1644 (45000): 今日已签到,不可重复 mysql> DELIMITER ;; mysql> DROP TRIGGER IF EXISTS tr_after_update_credit;; Query OK, 0 rows affected (0.01 sec) mysql> CREATE TRIGGER tr_after_update_credit -> AFTER UPDATE ON ecs_user_credit -> FOR EACH ROW -> BEGIN -> -- 只有当分数发生变化时才记录 -> IF OLD.score <> NEW.score THEN -> INSERT INTO ecs_user_credit_log ( -> user_id, -> change_value, -> old_score, -> new_score, -> reason -> ) VALUES ( -> NEW.user_id, -> NEW.score - OLD.score, -> OLD.score, -> NEW.score, -> CONCAT('自动记录: ', NEW.score - OLD.score) -> ); -> END IF; -> END;; Query OK, 0 rows affected (0.01 sec) mysql> DELIMITER ; mysql> UPDATE ecs_user_credit SET score = 94 WHERE user_id = 1; Query OK, 0 rows affected (0.00 sec) Rows matched: 1 Changed: 0 Warnings: 0 mysql> SELECT * FROM ecs_user_credit_log WHERE user_id = 1 ORDER BY created_at DESC LIMIT 10; +----+---------+-----------+-----------+--------------+-----------------------+---------------------+ | id | user_id | old_score | new_score | change_value | reason | created_at | +----+---------+-----------+-----------+--------------+-----------------------+---------------------+ | 6 | 1 | 94 | 94 | 0 | 自动记录: 0 | 2025-11-09 07:03:34 | | 7 | 1 | 94 | 94 | 0 | 初始化测试用户 | 2025-11-09 07:03:34 | | 5 | 1 | 95 | 90 | -5 | 系统调整 | 2025-11-09 06:22:39 | | 4 | 1 | 90 | 95 | 5 | 系统调整 | 2025-11-09 06:21:06 | | 3 | 1 | 85 | 90 | 5 | 测试加分 | 2025-11-09 05:55:47 | | 1 | 1 | 80 | 95 | 15 | 完善个人资料 | 2025-11-09 05:38:14 | | 2 | 1 | 95 | 85 | -10 | 评论被举报 | 2025-11-09 05:38:14 | +----+---------+-----------+-----------+--------------+-----------------------+---------------------+ 7 rows in set (0.00 sec) mysql> DELETE FROM ecs_user_credit_log -> WHERE user_id = 1 -> AND change_value = 0 -> AND reason LIKE '自动记录:%'; Query OK, 1 row affected (0.01 sec) mysql> -- 改成 mysql> SET v_reason = CONCAT('action:', p_action_type); ERROR 1193 (HY000): Unknown system variable 'v_reason' mysql> mysql> -- 或带数值 mysql> SET v_reason = CONCAT('action:', p_action_type, ':', v_credit_value); ERROR 1193 (HY000): Unknown system variable 'v_reason' mysql>
最新发布
11-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值