最大和最小日期问题 [英] MAX and MIN date problem

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

问题描述

你好,
我的问题是我在Employee表中有一些日期,例如01/01/2011,01/01/2010,24/02/2010,19/12/2009,07/07/2008,21/12/2009,24 /08/2010,03/03/2011,01/04/2011."



当我运行此查询从Employee中选择MAX(DateOf_joining)"时,我找到了此答案"2010/08/24".

我也运行此查询从Employee中选择MIN(DateOf_joining)",我发现此答案为"01/01/2010".

解决方案

为您提供最新日期. br/>

select top 1 DateOf_joining from Employee order by DateOf_joining desc



给您第一个(初始)日期

select top 1 DateOf_joining from Employee order by DateOf_joining asc


您好Ramalinga,

我已经尝试过Ur怀疑您使用的查询是否正确,它是否工作正常.

主要的事情是首先检查您的数据库列数据类型是Date还是Datatime.
我尝试使用Date数据类型正常工作.

这是我收到的输出:


从Employee中选择MIN(DateOf_joining)=====>给我:2008-07-07

还有

从Employee中选择MAX(DateOf_joining)=====>给我:2011-04-01

因此,只需检查Employee表中DateOf_joining列的ur数据类型字段即可.

最好.


嗯,根据您的结果,看起来您实际上将日期存储在varchar字段中,请不要.将列类型修改为日期(或日期时间,如果需要),并且已经编写的查询可以正常工作.

要测试日期数据类型的使用,您可以尝试运行以下命令:

SELECT MAX( CONVERT(DATE, DateOf_joining, 103) ) 
FROM  Employee


这样会将您的字符串转换为日期并获得最大值

hello,
my question is "i have some dates in Employee table like as 01/01/2011,01/01/2010,24/02/2010,19/12/2009,07/07/2008,21/12/2009,24/08/2010,03/03/2011,01/04/2011. "



When i run this query "select MAX(DateOf_joining) from Employee " i found this answer "24/08/2010".

also i run this query "select MIN(DateOf_joining) from Employee" i found this answer "01/01/2010".

解决方案

Gives you the latest date.

select top 1 DateOf_joining from Employee order by DateOf_joining desc



Gives you the first(initial) date

select top 1 DateOf_joining from Employee order by DateOf_joining asc


Hi Ramalinga,

I Have try Ur doubt it''s working fine the query u have used is right one.

Main thing is first check ur database column datatype whether is Date or Datatime .
I have tried with Date datatype it''s working Fine.

Here what i have received as output :


select MIN(DateOf_joining) from Employee =====> Gives me : 2008-07-07

And

select MAX(DateOf_joining) from Employee =====> Gives me :2011-04-01

So just Check ur datatype field of DateOf_joining column in ur Employee table.

Al d best.


Hm, Based on your result it looks like you actually store the dates in a varchar field, don''t. Modify the column type to date (or datetime if you need) and the query you have already written works correctly.

To test the usage of date datatype you can try running this:

SELECT MAX( CONVERT(DATE, DateOf_joining, 103) ) 
FROM  Employee


That would convert your string to dates and get the maximum


这篇关于最大和最小日期问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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