且构网

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

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

更新时间:2023-02-20 12:18:26

Swapnil 2009,

查询中的小变化

Hi Swapnil 2009,

Small changes in your query

select * from retinvoice
where retinvoice.CREATEDDATE > '08/27/2010' and
retinvoice.CREATEDDATE < '08/29/2010'



我建议在运算符之间使用也可以实现输出.



My suggestion use between operator also you can achieve your output.

select * from retinvoice
where retinvoice.CREATEDDATE between '08/27/2010' and
 '08/29/2010'



干杯:)



Cheers :)


首先,为什么要使用convert(varchar....请改用YYYY-MM-DD格式的日期.
您的CREATEDDATE字段也包含时间值吗?那可能是不获得任何输出的原因之一.如果是这样,并且您仍要转换日期,则转换CREATEDDATE字段也与转换日期值相同.
Firstly why are you using convert(varchar.... Use YYYY-MM-DD format date instead.
Does your CREATEDDATE field contain time value also? Thats may be one of the reason not to geting any output. If so and you still want to convert the date then convert your CREATEDDATE field also same as you are converting the date value.


您的查询不起作用,因为您的字段可能是DateTime类型.当您保存数据时,您将节省时间.

因此,如果您提供的参数如where retinvoice.CREATEDDATE = convert(varchar,''08/28/2010'',103),那么您将不会获取数据,因为它会随时间存储,例如"08/28/2010 4" :31:29 PM.因此,您的状况将永远不会符合给定的条件.

因此,我建议您不要在select语句中传递convert函数,而在insert语句中传递它.

希望对您有所帮助.
your query is not working because perhaps your field is of type DateTime. And when you are saving data you are saving with time.

So if your providing parameter like where retinvoice.CREATEDDATE = convert(varchar,''08/28/2010'',103) then you won''t get data because it stored with time for example like "08/28/2010 4:31:29 PM". So your condition will never match the given criteria.

So what I can suggest you is instead of passing convert function in select statement pass it in insert statement.

I hope this will help you.