当前位置: 首页 > news >正文 SQL SERVER查询时间条件式写法 news 来源:原创 2024/5/18 14:50:21 如下表: tid tname tdate 1 张三 2009-10-29 19:04:50 2 李四 2006-11-12 11:04:50 3 王五 2007-6-11 4 陈六 2007-8-26 12:04:50 5 小二 2009-11-28 下面开始做各种时间查询:注意:避免在WHERE条件式中对字段做运算,比如convert(char(10),tdate,120) select tname from tt Where tdate='2007-06-11' --当时间段值为2007-06-11 00:00:00.000时,此条件式成立 --select tname 时间值没有 from tt Where tdate='2007-08-26' --时间段值为2007-08-26 12:04:50.000,此条件式不成立,时间值不同,这样找不到当天值 select tname 查找当天时间 from tt Where tdate between '2007-08-26 00:00:00' and '2007-08-26 23:59:59' --select tname 今天的值 from tt Where tdate =getdate() --不行,因为SQL会默认当天起点是00:00:00开始, select tname 今天的值方式一 from tt Where tdate between convert(char(10),getdate(),120) and convert(char(10),getdate(),120)+' 23:59:59' select tname 今天的值方式二 from tt Where tdate >= convert(char(10),getdate(),120) and tdate<convert(char(10),getdate()+1,120) --select tname from tt Where left(tdate,10)='2007-06-11' -- --select tname from tt Where convert(char(10),tdate,120)='2007-06-11' --查找当天值,可以用,但效率低 --select tname aa from tt Where convert(char(10),tdate,120)='2007-08-26' --查找当天值,可以用,但如果条件式后面为‘2007-8-26’则不行.因为char(10)限定十位 --select tname 时间 from tt Where tdate Like '2007-08-26%' --不可用,找不到记录 --select tname 时间段 from tt Where tdate between '2007-06-11' and '2007-09-12' 查找时间段也是一样的,如: select tname 查找2007年6月11号到8月26的记录 From tt Where tdate >='2007-06-11' and tdate<= '2007-08-26 23:59:59' 相关文章: C#完整的通信代码(一)(点对点,点对多,同步,异步,UDP,TCP) C#完整的通信代码(二)(点对点,点对多,同步,异步,UDP,TCP) C# byte数组常用扩展 八种情况 C# Stream 和 byte[] 之间的转换 常用SQL/oracle循环语句 ORACLE 字符串操作 jquery下json数组的操作用法实例 c#写windows服务程序 JS操作select相关方法:新增 修改 删除 选中 清空 判断存在 等 js判断undefined类型,undefined,null,NaN的区别 oracle的exp/imp使用方法学习 C# 操作线程的通用类[测试通过] c# 获取电脑硬件信息通用查询类[测试通过] oracle学习笔记_触发器 常见算法思想 9月CHINA-PUB-OPENDAY技术沙龙——IPHONE 230. Kth Smallest Element in a BST android 一些 utils - C#编程大幅提高OUTLOOK的邮件搜索能力! C++入门教程(10):for 语句 ES6简单总结(搭配简单的讲解和小案例) isset在php5.6-和php7.0+的一些差异 java正则表式的使用 Storybook 5.0正式发布:有史以来变化最大的版本\n 技术攻略】php设计模式(一):简介及创建型模式 前端 前端 CSS : 5# 纯 CSS 实现24小时超市 前端学习笔记之原型——一张图说明`prototype`和`__proto__`的区别 前端之Sass/Scss实战笔记 山寨一个 Promise 微信如何实现自动跳转到用其他浏览器打开指定页面下载APP 在Unity中实现一个简单的消息管理器 AI又要和人类“对打”,Deepmind宣布《星战Ⅱ》即将开始 ... linux 淘宝开源监控工具tsar ###C语言程序设计-----C语言学习(6)# #WEB前端(HTML属性) #Z2294. 打印树的直径 (多级缓存)缓存同步 (二)JAVA使用POI操作excel (利用IDEA+Maven)定制属于自己的jar包 (每日持续更新)jdk api之FileReader基础、应用、实战 (四)Android布局类型(线性布局LinearLayout) (一)基于IDEA的JAVA基础10 (转载)Google Chrome调试JS .net framework4与其client profile版本的区别 .Net IE10 _doPostBack 未定义 .NET Micro Framework初体验 .NET Remoting学习笔记(三)信道 .net 前台table如何加一列下拉框_如何用Word编辑参考文献 .net开源工作流引擎ccflow表单数据返回值Pop分组模式和表格模式对比 .NET命名规范和开发约定 .Net转Java自学之路—SpringMVC框架篇六(异常处理) .stream().map与.stream().flatMap的使用 ??如何把JavaScript脚本中的参数传到java代码段中 @cacheable 是否缓存成功_让我们来学习学习SpringCache分布式缓存,为什么用?
如下表: tid tname tdate 1 张三 2009-10-29 19:04:50 2 李四 2006-11-12 11:04:50 3 王五 2007-6-11 4 陈六 2007-8-26 12:04:50 5 小二 2009-11-28 下面开始做各种时间查询:注意:避免在WHERE条件式中对字段做运算,比如convert(char(10),tdate,120) select tname from tt Where tdate='2007-06-11' --当时间段值为2007-06-11 00:00:00.000时,此条件式成立 --select tname 时间值没有 from tt Where tdate='2007-08-26' --时间段值为2007-08-26 12:04:50.000,此条件式不成立,时间值不同,这样找不到当天值 select tname 查找当天时间 from tt Where tdate between '2007-08-26 00:00:00' and '2007-08-26 23:59:59' --select tname 今天的值 from tt Where tdate =getdate() --不行,因为SQL会默认当天起点是00:00:00开始, select tname 今天的值方式一 from tt Where tdate between convert(char(10),getdate(),120) and convert(char(10),getdate(),120)+' 23:59:59' select tname 今天的值方式二 from tt Where tdate >= convert(char(10),getdate(),120) and tdate<convert(char(10),getdate()+1,120) --select tname from tt Where left(tdate,10)='2007-06-11' -- --select tname from tt Where convert(char(10),tdate,120)='2007-06-11' --查找当天值,可以用,但效率低 --select tname aa from tt Where convert(char(10),tdate,120)='2007-08-26' --查找当天值,可以用,但如果条件式后面为‘2007-8-26’则不行.因为char(10)限定十位 --select tname 时间 from tt Where tdate Like '2007-08-26%' --不可用,找不到记录 --select tname 时间段 from tt Where tdate between '2007-06-11' and '2007-09-12' 查找时间段也是一样的,如: select tname 查找2007年6月11号到8月26的记录 From tt Where tdate >='2007-06-11' and tdate<= '2007-08-26 23:59:59'