且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

关于日期&的sql查询时间格式

更新时间:2023-02-05 16:07:58

取出"a":
Take the "a" out:
Select * from ( SELECT (convert(varchar, [InTime], 103))as Date FROM CarEnteries
Union SELECT (convert(varchar, [OutTime], 103)) as Date FROM CarEnteries)
Where (convert(DateTime, [Date], 103)) BETWEEN '08-05-2012' and '01-07-2012'
a order by (convert(DateTime, [Date], 103)) asc


变为


becomes

Select * from ( SELECT (convert(varchar, [InTime], 103))as Date FROM CarEnteries
Union SELECT (convert(varchar, [OutTime], 103)) as Date FROM CarEnteries)
Where (convert(DateTime, [Date], 103)) BETWEEN '08-05-2012' and '01-07-2012'
order by (convert(DateTime, [Date], 103)) asc




顺便说一句:永远不要将日期存储为字符串.而是将它们存储为DateTime,这样就不必在每次需要进行任何数学运算时都进行转换.这也使工作变得更加轻松,因为您将国际化移动到尽可能靠近输入和输出的位置-因此数据库中的数据采用完全独立的格式.




BTW: never, ever store dates as strings. Store them as DateTime instead, then you don''t have to faff with converting them every time you need to do any math. It also makes life a lot easier, because you move the internationalization to as close to input and output as possible - so the data in your DB is in a completely independent format.


您的查询语法错误.
carEnteries的末尾有一个额外的barcket.

但是,请注意,您的查询仍然无法使用(在删除此括号之后).

当您执行UNION时,在UNION 查询的所有组成部分中返回的列应返回相同的列集.
There is a syntax error with your query.
There is an extra barcket at the end of carEnteries.

However, note that your query will still not work (after removing this bracket).

When you do a UNION, the columns returned in all the constituents of the UNION queries should return the same set of columns.


如果ur [date]字段数据类型为datetime,则使用此查询将起作用

if ur [date] field datatype is datetime then use this query it will work

Select * from ( SELECT (convert(varchar, [InTime], 103))as Date FROM CarEnteries
Union SELECT (convert(varchar, [OutTime], 103)) as Date FROM CarEnteries)
Where [Date] BETWEEN '08-05-2012' and '01-07-2012'
order by [Date] asc