常用字符串操作函数:
--返回字符串中指定的子串出现的开始位置(索引从1开始) select CHARINDEX('34','1234567890123') as startIndex --返回字符串中指定的子串出现的开始位置(索引从1开始,字串前必须加%) select PATINDEX('%34%','1234567890123') as startIndex --大小写转化 select LOWER('abc') 小写,UPPER('abc') 大写 --去空格; LTRIM去除左侧空格; RTRIM去除左侧空格 select LTRIM(' abc ') 去除左侧空格,RTRIM(' abc ') 去除右侧空格 --截取字符串 retrun 1234 --从左侧截取 截取几位 select LEFT('123456',4) as 从左侧开始截取截取4位 --return1234 --从右侧截取 截取几位 select RIGHT('123456',4) as 从左侧开始截取截取4位 --return3456 --从第几位开始截取,截取多长 select SUBSTRING('123456',2,4) as 从左侧第二个位置开始截取4位 -- return 2345 --将指定字符串重复指定次数 select REPLICATE('123',2) -- return 123123 --字符串翻转 select REVERSE('123') --return 321 --替换字符串内容(将12替换为ab) select REPLACE('123456','12','ab') --return ab3456 --替换字符串 可指定长度和位置(从第三位开始替换2为,替换为ab) select STUFF('123456',3,2,'ab') -- return 12ab56 --应用 select (stuff((select '|' + b.DIRECTION from QUEUEDETAIL b where b.APPOINTSID = a.APPOINTSID for xml path('')),1,1,''))as DIRECTIONALL, (stuff((select '|' + b.PARTOFCHECK from QUEUEDETAIL b where b.APPOINTSID = a.APPOINTSID for xml path('')),1,1,''))as PARTOFCHECKALL from QUEUEDETAIL b,HIS_QUEUE a
索引:
SQL SERVER提供了两种索引:聚集索引(clustered index,也称聚类索引、簇集索引)和非聚集索引(nonclustered index,也称非聚类索引、非簇集索引)
注:主键列默认为聚簇索引,聚簇索引只能有一个,非聚簇索引可以有多个;一般建议在对表创建索引时,使用日期类型的列创建且创建为降序。
在创建复合索引是建议将日期列作为首列,同时在使用列上应遵循——使用频繁使用的列
索引分为:聚集索引、非聚集索引
1.聚集索引--表的存储顺序
在聚集索引中,每张表只能有一个;
在聚集索引中,索引的顺序决定表中数据的顺序;
在聚集索引中,在表中创建主键列或唯一约束时,会自动创建聚集索引(即在表中添加主键列时,数据库引擎会自动创建约束和索引);
2.非聚集索引
非聚集索引,可以有多个;
在非在聚集索引中,索引的结构完全独立与数据行的结构;
在非在聚集索引中,数据表中的数据行和索引的顺序不一致;
查看指定表的索引
exec sp_helpindex his_queue
go
exec sp_helpindex queuedetail
应用示例:
USE DB GO IF NOT EXISTS(SELECT Name FROM SysColumns WHERE id=Object_Id('[HIS_QUEUE]') AND Name = 'InsertDateTime') BEGIN ALTER TABLE HIS_QUEUE ADD InsertDateTime datetime DEFAULT (CONVERT([date],CONVERT([varchar](100),getdate(),(23)),(23))) END GO IF NOT EXISTS(SELECT Name FROM SysColumns WHERE id=Object_Id('[QUEUEDETAIL]') AND Name = 'InsertDateTime') BEGIN ALTER TABLE QUEUEDETAIL ADD InsertDateTime datetime DEFAULT (CONVERT([date],CONVERT([varchar](100),getdate(),(23)),(23))) END GO update QUEUEDETAIL set InsertDateTime=(CONVERT([datetime], QUEUEDETAIL.APPOINTDATE ,(20))) update HIS_QUEUE set InsertDateTime=(CONVERT([datetime], HIS_QUEUE.[InsertDate] ,(20))) GO /****** Object: Index [DF_HISQUEUE_InsertDateTime] Script Date: 2023-05-31 10:21:54 ******/ IF EXISTS (SELECT name FROM sys.indexes WHERE name = N'DF_HISQUEUE_InsertDateTime') DROP INDEX [DF_HISQUEUE_InsertDateTime] ON [dbo].[HIS_QUEUE] WITH ( ONLINE = OFF ) GO /****** Object: Index [DF_HISQUEUE_InsertDateTime] Script Date: 2023-05-31 10:21:54 ******/ CREATE CLUSTERED INDEX [DF_HISQUEUE_InsertDateTime] ON [dbo].[HIS_QUEUE] ( InsertDateTime desc, [APPOINTSDATE] DESC, [APPKIND] DESC, [HISEXAMNO] DESC, [HISID] DESC, [NAME] DESC, [QUEUENAME] DESC, [EXECUTEDEPT] DESC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] GO /****** Object: Index [DF_QUEUEDETAIL_InsertDateTime] Script Date: 2023-05-31 10:34:41 ******/ IF EXISTS (SELECT name FROM sys.indexes WHERE name = N'DF_QUEUEDETAIL_InsertDateTime') DROP INDEX [DF_QUEUEDETAIL_InsertDateTime] ON [dbo].[QUEUEDETAIL] WITH ( ONLINE = OFF ) GO /****** Object: Index [DF_QUEUEDETAIL_InsertDateTime] Script Date: 2023-05-31 10:34:41 ******/ CREATE CLUSTERED INDEX [DF_QUEUEDETAIL_InsertDateTime] ON [dbo].[QUEUEDETAIL] ( InsertDateTime desc, [APPOINTDATE] DESC, [APPKIND] DESC, [HISEXAMNO] DESC, [QUEUENAME] DESC, DIRECTION DESC, --CheckItemRemark DESC, PARTOFCHECK DESC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] GO
标签:QUEUEDETAIL,--,InsertDateTime,索引,006,select,DESC From: https://www.cnblogs.com/YYkun/p/17462788.html