简体   繁体   English

如何在SQL Server 2005中比较两个日期?

[英]How to compare two date in SQL Server 2005?

How can I convert date value into format of dd-MM-yyyy in SQL? 如何在SQL中将日期值转换为dd-MM-yyyy格式? I have stored date into database in varchar(10) format. 我已将日期以varchar(10)格式存储到数据库中。 Now want to compare date from 1-05-2015 to 31-06-2015 . 现在想比较日期1-05-2015年1 31-06-2015日至1-05-2015年6月31-06-2015

Query is : 查询是:

select date
from dates
where date >='1-05-2015' and date <='10-06-2015'

I need help in Crystal report. 我在Crystal报表中需要帮助。 How can i remove blank line in crystal report 2008? 如何删除Crystal Report 2008中的空白行?

it returns only (same result in use of between) 它仅返回(使用之间的相同结果)

  1. 1-06-2015 2015年1月1日
  2. 10-06-2015 2015年10月6日
  3. 1-07-2015 2015年1月1日

I cannot use Format Method because 2005 not support. 我不能使用格式方法,因为2005不支持。 I tried Convert method but not helpful. 我尝试了Convert方法,但没有帮助。

You should use this, if you store date as string, to convert it to DATE 如果您将日期存储为字符串,则应使用它来将其转换为DATE

https://msdn.microsoft.com/de-de/library/ms187928.aspx https://msdn.microsoft.com/de-de/library/ms187928.aspx

Can't remember whether the CONVERT was exactly like it is now, but you can try 不记得转换是否完全像现在这样,但是您可以尝试

SET DATEFORMAT dmy -- Needed if date format on database server is different so the start and end date string values 01-05-2015 and 10-06-2015 can be converted automatically with the custom format
select date from dates where CONVERT(Date, date, 105) >='01-05-2015' and CONVERT(Date, date, 105) <='10-06-2015'

I would suggest not to store dates as varchar(10) but as Date or DateTime. 我建议不要将日期存储为varchar(10),而是将其存储为Date或DateTime。 In addition I would use yyyy-MM-dd format, which can be interpreted regardless of whatever setting so you don't need the SET DATEFORMAT. 另外,我将使用yyyy-MM-dd格式,无论设置如何,都可以解释该格式,因此您不需要SET DATEFORMAT。 So if you always store dates as date you can do (among other features): 因此,如果您始终将日期存储为日期,则可以这样做(除其他功能外):

select date from dates where date >= '2015-05-01' and date <= '2015-06-10'

I agree with the other comments that dates stored in a proper date format are the best way to go, but sometimes changing the database isn't feasible. 我同意其他意见,即以正确的日期格式存储日期是最好的方式,但是有时更改数据库是不可行的。

Here's some code that will convert the date from your string format to datetime, assuming the month and year are always 2 and 4 characters, with the day being the only variable length. 这是一些将日期从字符串格式转换为日期时间的代码,假设月份和年份始终是2和4个字符,而日期是唯一的可变长度。

Note, I changed the dates in the where statement from '1-05-2015' to '2015-05-01' and '10-06-2015' to '2015-06-10'. 请注意,我将where语句中的日期从“ 1-05-2015”更改为“ 2015-05-01”,并将“ 10-06-2015”更改为“ 2015-06-10”。

select date
       , cast(right(date, 4)+'-'+SUBSTRING(date, charindex('-', date)+1, 2)+'-'+left(date, charindex('-', date)-1) as datetime) as converted_date
from dates
where
     cast(right(date, 4)+'-'+SUBSTRING(date, charindex('-', date)+1, 2)+'-'+left(date, charindex('-', date)-1) as datetime)
         >= cast('2015-05-01' as datetime)
     and cast(right(date, 4)+'-'+SUBSTRING(date, charindex('-', date)+1, 2)+'-'+left(date, charindex('-', date)-1) as datetime)
         <= cast('2015-06-10' as datetime)

I can convert varchar to datetime in sql 2014 using the following code 我可以使用以下代码在sql 2014中将varchar转换为datetime

ALTER TABLE table_you_want ALTER COLUMN column_you_want DATETIME ALTER TABLE table_you_want ALTER COLUMN column_you_want DATETIME

Well i am not sure whether 2005 supports it or not. 好吧,我不确定2005年是否支持它。 hope it helps..!!1 希望能有所帮助.. !! 1

Luckily I had 2005 installed on my machine and i got results. 幸运的是,我在计算机上安装了2005,并且得到了结果。

See this SQL Fiddle 看到这个SQL小提琴

Let me know if this doesn't help you. 让我知道这是否对您没有帮助。

You can convert to date type with style: 您可以将样式转换为日期类型:

select date 
from dates
where convert(date, date, 103) between '20150501' and '20150610' 

Never keep dates as strings, numerics as strings etc. Use appropriate types, which will keep you far from problems like this. 切勿将日期保留为字符串,将数字保留为字符串等。请使用适当的类型,这将使​​您远离此类问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM