关键词搜索

源码搜索 ×
×

SqlServer 测试删除与日期计算

发布2023-01-30浏览483次

详情内容

删除语句

  1. -- 创建t1表,并插入3条数据
  2. CREATE TABLE [dbo].[t1] ([id] [INT] NOT NULL, [name] [NCHAR](10) NULL) ON [PRIMARY];
  3. GO
  4. INSERT INTO dbo.t1(id, name)
  5. VALUES(1, -- id - int
  6. N'lisi' -- name - nchar(10)
  7. );
  8. INSERT INTO dbo.t1(id, name)
  9. VALUES(2, -- id - int
  10. N'zhangsan' -- name - nchar(10)
  11. );
  12. INSERT INTO dbo.t1(id, name)
  13. VALUES(3, -- id - int
  14. N'aben' -- name - nchar(10)
  15. );
  16. -- 创建t2表,并插入2条数据
  17. CREATE TABLE [dbo].[t2] ([id] [INT] NOT NULL, [name] [NCHAR](10) NULL) ON [PRIMARY];
  18. GO
  19. INSERT INTO dbo.t2(id, name)
  20. VALUES(1, -- id - int
  21. N'lisi' -- name - nchar(10)
  22. );
  23. INSERT INTO dbo.t2(id, name)
  24. VALUES(2, -- id - int
  25. N'zhangsan' -- name - nchar(10)
  26. );
  27. SELECT * FROM dbo.t1;
  28. SELECT * FROM dbo.t2;
  29. SELECT b.* FROM t1 AS a INNER JOIN t2 AS b ON a.id=b.id;
  30. -- 删除内关联t1表后的2条记录
  31. DELETE t1 FROM t1 AS a INNER JOIN t2 AS b ON a.id=b.id;
  32. -- 删除内关联t2表后的2条记录
  33. DELETE t2 FROM t1 AS a INNER JOIN t2 AS b ON a.id=b.id;
  34. SELECT b.* FROM t1 AS a LEFT JOIN t2 AS b ON a.id=b.id;
  35. -- 删除左外关联后的t1表3条记录
  36. DELETE t1 FROM t1 AS a LEFT JOIN t2 AS b ON a.id=b.id;
  37. -- 删除左外关联后的t2表2条记录
  38. DELETE t2 FROM t1 AS a LEFT JOIN t2 AS b ON a.id=b.id;
  39. -- 关联删除t1
  40. delete from t1 from t2 inner join t1 on t1.id=t2.id ;

日期计算

三天 select * from T_news where datediff(day,addtime,getdate())<= 2 and datediff(day,addtime,getdate())>= 0
一周 select * from T_news WHERE (DATEPART(wk, addtime) = DATEPART(wk, GETDATE())) AND (DATEPART(yy, addtime) = DATEPART(yy, GETDATE()))
注意:此时不能用 datediff 差值为7,因为,datediff只表示间隔数
一月 select * from T_news WHERE (DATEPART(yy, addtime) = DATEPART(yy, GETDATE())) AND (DATEPART(mm, addtime) = DATEPART(mm, GETDATE()))
一季度 select * from T_news where DATEPART(qq, addtime) = DATEPART(qq, GETDATE()) and DATEPART(yy, addtime) = DATEPART(yy, GETDATE())
本周:select * from table where datediff(week,C_CALLTIME,getdate())=0 --C_CALLTIME 为日期字段
本月:select * from table where datediff(Month,C_CALLTIME,getdate())=0 --C_CALLTIME 为日期字段
本季:select * from table where datediff(qq,C_CALLTIME,getdate())=0
前半年1-6,后半年7-12:select * from table where datepart(mm,C_CALLTIME)/7 = datepart(mm,getdate())/7

  1. --sql得到当前系统时间得 日期部分
  2. CONVERT(varchar(10),getDate(),120)
  3. --昨天
  4. select convert(varchar(10),getdate() - 1,120)
  5. --明天
  6. select convert(varchar(10),getdate() + 1,120)
  7. --最近七天
  8. select * from tb where 时间字段 >= convert(varchar(10),getdate() - 7,120)
  9. --随后七天
  10. select * from tb where 时间字段 <= convert(varchar(10),getdate() + 7,120) and 时间字段 >= --时间字段
  11. --上月
  12. select * from tb where month(时间字段) = month(getdate()) - 1
  13. --本月
  14. select * from tb where month(时间字段) = month(getdate())
  15. --下月
  16. select * from tb where month(时间字段) = month(getdate()) + 1
  17. --昨天
  18. Select * From TableName Where DateDiff(dd, DateTimCol, GetDate()) = 1
  19. --明天
  20. Select * From TableName Where DateDiff(dd, GetDate(), DateTimCol) = 1
  21. --最近七天
  22. Select * From TableName Where DateDiff(dd, DateTimCol, GetDate()) <= 7
  23. --随后七天
  24. Select * From TableName Where DateDiff(dd, GetDate(), DateTimCol) <= 7
  25. --上周
  26. Select * From TableName Where DateDiff(wk, DateTimCol, GetDate()) = 1
  27. --本周
  28. Select * From TableName Where DateDiff(wk, DateTimCol, GetDate()) = 0
  29. --下周
  30. Select * From TableName Where DateDiff(wk, GetDate(), DateTimCol ) = 1
  31. --上月
  32. Select * From TableName Where DateDiff(mm, DateTimCol, GetDate()) = 1
  33. --本月
  34. Select * From TableName Where DateDiff(mm, DateTimCol, GetDate()) = 0
  35. --下月
  36. Select * From TableName Where DateDiff(mm, GetDate(), DateTimCol ) = 1
  37. --------------------------------------------------------
  38. --本周
  39. select * from tb where datediff(week , 时间字段 ,getdate()) = 0
  40. --上周
  41. select * from tb where datediff(week , 时间字段 ,getdate()) = 1
  42. --下周
  43. select * from tb where datediff(week , 时间字段 ,getdate()) = -1

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载