Question on spliting Only date

Hello

i would like to get only date from this ActiveSupport::TimeWithZone
class 2009-11-16 11:00:51.

i want to get only 2009-11-16

since i want to use like below

@groups = GroupsInfo.find(:all, :conditions=>[“date >= ? and date <=
?”,@from_date,@to_date])

Is there any other ways i can think of ?

Thanks in advance for your reply

Hi

You can type cast from timestamp to date .For your example it like (I
am changing date to created_at for readability You can change it)

@groups = GroupsInfo.find(:all, :conditions=>[“date >= ? and date <=
?”,@from_date,@to_date])

@groups = GroupsInfo.find(:all, :conditions=>["CAST(created_at as date)

= ? and CAST(created_at as date) <= ?",@from_date,@to_date])

Sijo

@groups = GroupsInfo.find(:all, :conditions=>["CAST(created_at as date)

= ? and CAST(created_at as date) <= ?",@from_date,@to_date])

Sijo

Sijo k g wrote:

@groups = GroupsInfo.find(:all, :conditions=>["CAST(created_at as date)

= ? and CAST(created_at as date) <= ?",@from_date,@to_date])

Sijo

Thank u all it works fine.

Thanks a lot for your reply

Newb N. wrote:

Sijo k g wrote:

@groups = GroupsInfo.find(:all, :conditions=>["CAST(created_at as date)

= ? and CAST(created_at as date) <= ?",@from_date,@to_date])

FYI: If you don’t want to have to rely on database functions, I believe,
you could do the type conversion in Ruby:

@groups = GroupsInfo.find(:all, :conditions=>[“date >= ? and date <= ?”,
@from_date.to_date ,@to_date.to_date])

I am assuming @from_date and @to_date are instances of Time or DateTime.

Robert W. wrote:

Newb N. wrote:

Sijo k g wrote:

@groups = GroupsInfo.find(:all, :conditions=>["CAST(created_at as date)

= ? and CAST(created_at as date) <= ?",@from_date,@to_date])

FYI: If you don’t want to have to rely on database functions, I believe,
you could do the type conversion in Ruby:

@groups = GroupsInfo.find(:all, :conditions=>[“date >= ? and date <= ?”,
@from_date.to_date ,@to_date.to_date])

That won’t work. It will only affect the values in the WHERE clause.

I am assuming @from_date and @to_date are instances of Time or DateTime.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Marnen Laibow-Koser wrote:

Robert W. wrote:

Newb N. wrote:

Sijo k g wrote:

@groups = GroupsInfo.find(:all, :conditions=>["CAST(created_at as date)

= ? and CAST(created_at as date) <= ?",@from_date,@to_date])

FYI: If you don’t want to have to rely on database functions, I believe,
you could do the type conversion in Ruby:

@groups = GroupsInfo.find(:all, :conditions=>[“date >= ? and date <= ?”,
@from_date.to_date ,@to_date.to_date])

That won’t work. It will only affect the values in the WHERE clause.

Disregard. I just reread the code and realized that it will work.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Marnen Laibow-Koser wrote:

Robert W. wrote:

FYI: If you don’t want to have to rely on database functions, I believe,
you could do the type conversion in Ruby:

@groups = GroupsInfo.find(:all, :conditions=>[“date >= ? and date <= ?”,
@from_date.to_date ,@to_date.to_date])

That won’t work. It will only affect the values in the WHERE clause.

Yep. I just thought about that myself. Was about to post a correction.

Wouldn’t an alternate fix be to make the condition inclusive on the low
end and exclusive on the high end?

@groups = GroupsInfo.find(:all, :conditions=>[“date >= ? and date < ?”,
@from_date.to_date ,@to_date.to_date])

Robert W. wrote:

@groups = GroupsInfo.find(:all, :conditions=>[“date >= ? and date < ?”,
@from_date.to_date ,@to_date.to_date])

I also should have mentioned that @to_date should be set to the day
after your ending target date:

@to_date.to_date + 1.day

One final note/suggestion…

It is also possible to use the hash form with a range.

Example:
from_date = Time.now.to_date - 10.days # 10 days ago
to_date = Time.now.to_date + 1.day # midnight one day after target
@posts = Post.find(:all, :conditions => { :published_at =>
from_date…to_date })

SELECT * FROM “posts” WHERE (“posts”.“published_at” >= ‘2009-11-07’ AND
“posts”.“published_at” < ‘2009-11-18’)

Notice use of exclusive range (x…y), which would include all of
2009-11-17 up to but not including midnight of 2009-11-18.

I only mention all this because I tend to avoid database specific
implementation when possible/practical. However, there are definitely
practical uses for database specific implementations.

I don’t know if there are any performance considerations to be taken
into account in this particular case. I suppose one would be weighing
database CAST+date range test performance against datetime range
testing. My guess would be that the later would perform slightly better,
but it’s difficult to know without benchmarking.