日期时间查询SQL SERVER [英] Date time query SQL SERVER

查看:84
本文介绍了日期时间查询SQL SERVER的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have Date Column Order_Date In date Time format , I need To Extract Today's Order before 14:00 P:M 'O' Clock and in Second Column I want to extract date from 15:00 P:M till 23:00 P:M in SQL SERVER,

Date Is in 24:00 Hor Format.

What I have tried:

Not Have Idea of date time format

推荐答案

你的问题没有任何意义。如果订单日期/时间在1400之前,那么第二列将为空,反之亦然。除此之外,你指定的两次之间有一个小时 - 下午2点到下午3点的订单呢?



Your question doesn't make any sense. If the order date/time is before 1400, then the "2nd column" will be null, and vice versa. Besides that, there's the hour between the two times you specified - what about orders between 2 and 3pm?

SELECT CONVERT(DATEtime,Order_Date,103) AS OrderDate,
       CASE WHEN CONVERT(TIME,CONVERT(DATETIME,Order_Date,103)) <= '14:00' 
            THEN CONVERT(TIME,CONVERT(DATETIME,Order_Date,103)) 
            ELSE NULL 
            END AS before_2pm,
       CASE WHEN CONVERT(TIME,CONVERT(DATETIME,Order_Date,103)) >= '15:00' 
            THEN CONVERT(TIME,CONVERT(DATETIME,Order_Date,103)) 
            ELSE NULL 
            END AS after_3pm
FROM [your_table]





最后的注意事项 - 如果您不知道日期格式,请尝试使用Google搜索并查看您找到的内容。如果您无法掌握最简单的谷歌搜索,您也可以找到另一项工作。



编辑========== ==========================



我修改了代码来处理Order_Date列作为varchar / nvarchar。再一次,谷歌搜索这将产生答案。如果您想知道为什么必须以这种方式编写代码,谷歌就是您的朋友。



Final note - if you do "not have idea of date format", try googling it and see what you find. If you can't master even the simplest of google searches, you may as well find another line of work.

EDIT ====================================

I modified the code to treat the Order_Date column as a varchar/nvarchar. Once again, googling this would have yielded the answer. If you want to know why you have to write the code that way, google is your friend.


希望它会有所帮助 - 在这两个版本上工作。

<来自TABLE的SELECT * *
WHERE FIELDNAME> {ts'2013-02-01 15:00:00.001'}

AND FIELDNAME< {ts'2013-08-05 00:00:00.000'}
Hope it will help - work at both version.

SELECT * from TABLE
WHERE FIELDNAME > {ts '2013-02-01 15:00:00.001'}
AND FIELDNAME < {ts '2013-08-05 00:00:00.000'}


这篇关于日期时间查询SQL SERVER的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆