随笔分类 - sql server
摘要:-- 列转行拼接 select stuff((select ','+字段名 from 表名 for xml path('')),1,1,'') -- 示例 select stuff((select ','+user_name from t_user for xml path('')),1,1,'')
阅读全文
摘要:-- sql server生成uuid select newid(); -- 替换- select replace(newid(),'-','');
阅读全文
摘要:-- 字段设置默认值 alter table t_ward add default 0 for valid;
阅读全文
摘要:-- sql server分组排序取出每组顺序第一条 select * from ( select *,row_number() over(partition by bloodbag_id order by operation_date desc) rn from tt_blood_log )t w
阅读全文
摘要:select dateadd(minute,-10,getdate());-- 减10分钟 select dateadd(hour,-10,getdate());-- 减10小时 select dateadd(day,-10,getdate());-- 减10天
阅读全文
摘要:alter table t_user add constraint UK_user_code -- 指定约束的名称 unique (user_code) -- 指定唯一键的列名 go
阅读全文
摘要:-- 创建触发器 CREATE TRIGGER tr_t_user_update_time ON t_user -- 触发时机、条件 AFTER UPDATE AS BEGIN SET NOCOUNT ON; UPDATE t_user SET update_time = SYSDATETIME()
阅读全文
摘要:-- SQL根据字符串排序 order by cast(card_no as int)
阅读全文
摘要:update tb_user set des = replace(des, '你好', '啦啦啦');
阅读全文
摘要:--执行语句生成函数fn_GetPy create function fn_GetPy(@str nvarchar(4000)) returns nvarchar(4000) --WITH ENCRYPTION as begin declare @intLen int declare @strRet
阅读全文
摘要:-- 1、匹配一个字段 比如user_name select * from tb_user where user_name like '%'+ #{keyWord} +'%'; select * from tb_user where user_name like concat('%',#{keyWo
阅读全文
摘要:-- 批量删除重复数据 delete t from ( select row_number() over(partition by user_code order by id) as rowNumber,* from t_user )t where t.rowNumber > 1
阅读全文
摘要:select left(str,len(str)-1) -- 非空校验 select case when len(str)>1 then left(str,len(str)-1) else str end
阅读全文
摘要:-- 去除左侧空格ltrim() select ltrim(' abc');-- abc -- 去除右侧空格rtrim() select rtrim('abc ');-- abc -- 去除两侧空格 select ltrim(rtrim(' abc '));-- abc
阅读全文
摘要:-- 打开 set identity_insert [表名] on -- 关闭 set identity_insert [表名] off -- 注:IDENTITY_INSERT默认是OFF
阅读全文
摘要:-- 执行得到批量删除语句 select CONCAT('drop table ', table_name, ';') from information_schema.tables where table_name like 'pre_%'; -- 执行结果: drop table pre_acco
阅读全文
摘要:-- MYSQL SELECT UUID(); -- 将'-'符号替换掉 SELECT REPLACE(UUID(), '-', ''); -- SQL SERVER SELECT NEWID(); -- 将'-'符号替换掉 SELECT REPLACE(NEWID(), '-', ''); --
阅读全文
摘要:-- 转换成大写 SELECT UPPER('a'); -- 转换成小写 SELECT LOWER('A');
阅读全文
摘要:--ROUND函数 SELECT ROUND(12.3456 , 2) --12.3500 SELECT ROUND(12.3456 , 0) --12.0000 SELECT ROUND(12.3456 , -1) --10.0000 SELECT ROUND(12.3 , 2) --12.3 -
阅读全文
摘要:SQL中CONCAT()、CONCAT_WS()和GROUP_CONCAT()函数的用法CONCAT()函数语法:CONCAT(str1, str2, ...)将多个字符串连接成一个字符串SELECT CONCAT(id, name, age) FROM tb_user;结果: CONCAT_WS(
阅读全文