一.
SQL知识大全(三):SQL中的字符串处理和条件查询 (zhihu.com)
二
- SELECT
-
- CONVERT(varchar, [CreateTime] ,23) AS [Date],
-
- SUM(1) AS [Count],
-
- SUM(IIF([ReportStatusId] = 3, 1, 0)) AS [FailCount],
-
- 1.0 * SUM(IIF([ReportStatusId] = 3, 1, 0)) / SUM(1) AS [FailRate]
-
- FROM Table
-
- WHERE [TypeId] = 9
-
- GROUP BY CONVERT(varchar, [CreateTime] ,23)
-
- order by [FailCount]
-
- Desc
解析为:CONVERT转换时间
Select CONVERT(varchar(100), GETDATE(), 23): 2006-05-16
(89条消息) CONVERT (VARCHAR,getdate(),23) ,后面的23是什么意思?_蒿豪郝灬号的博客-CSDN博客
- CREATE PROCEDURE [A].[sp]
- AS
- BEGIN
- DECLARE @ErrorMessage NVARCHAR(4000)
- DECLARE @TargetMonth INT
- DECLARE @MinMonth INT
-
- SELECT TOP 1 @TargetMonth = [MonthKey]
- FROM [A].[aa]
- ORDER BY [MonthKey] DESC
-
- IF @TargetMonth % 100 >= 8
- SET @MinMonth = @TargetMonth-7
- ELSE
- SET @MinMonth = @TargetMonth%100+12-7-100+@TargetMonth-@TargetMonth%100
-
-
- BEGIN TRY
- DELETE FROM [Opportunities].[FactOpportunityLifeCycleEventViewHalfYearWithSpendV2] WHERE [MonthKey] < @MinMonth
-
- END TRY
- BEGIN CATCH
- SET @ErrorMessage = ISNULL(@ErrorMessage, ERROR_MESSAGE())
- DECLARE @ErrorSeverity INT = ERROR_SEVERITY();
- DECLARE @ErrorState INT = ERROR_STATE();
- RAISERROR(@ErrorMessage, @ErrorSeverity, @ErrorState);
- END CATCH
- END
当@TargetMonth是只有年月的时候,如202307
- BEGIN TRY
- DELETE FROM [A].[a] WHERE [MonthKey] < CAST(FORMAT(DATEADD(MONTH,-7,CONVERT(date, CONVERT(varchar(8), @TargetMonth)+'01', 112)),'yyyyMM') AS INT)
-
-
- END TRY
关于年月日时间的计算:使用Dateadd()函数
(89条消息) SQL Server 使用DATEADD()函数实现秒、分钟、小时、日、周、月份、季度、年份加减_旭东怪的博客-CSDN博客
三 CRUD语句
INSERT INTO 表名称 VALUES (值1, 值2,....)
INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值2,....)
UPDATE table SET CustomerId = 123 WHERE AccountId = 1
四 KUSTO