Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Searching a Date between 2 dates with DBIx::Class

by phildeman (Scribe)
on Sep 05, 2019 at 21:49 UTC ( [id://11105687]=perlquestion: print w/replies, xml ) Need Help??

phildeman has asked for the wisdom of the Perl Monks concerning the following question:

HELP!!

I want to search for a row in a table using current date that falls between a start and end date, using DBIx::Class. The data type for the 2 date columns (in SQL Server) is datetime.

my $dt = DateTime->now( time_zone => "America/New_York" ); my $mydatarow = $schema->resultset( 'DataTable' )->search({ START_DATE + => { '>=' => $dt }, END_DATE => { '=<' => $dt }})->single;

The results are empty, yet when I go into the database there is a record that meets my criteria. The record that meets the criteria is
START_DATE = 2019-09-03 00:00:00.000 and
END_DATE = 2019-12-20 00:00:00.000.

The current date is 2019-09-05. $dt is 2019-09-05 00:00:00.

I am using DBIx::Class::InflateColumn::DateTime as part of my database package.

What can I do to make this return the result I am looking for?

Thanks for your help!

Replies are listed 'Best First'.
Re: Searching a Date between 2 dates with DBIx::Class
by phildeman (Scribe) on Sep 06, 2019 at 01:51 UTC

    Nevermind, I figured it out. I placed the <= and >= in the wrong places. And need to format the current date ($dt) It should have been:

    my $mydatarow = $schema->resultset( 'DataTable' )->search({ START_DATE + => { '<=' => $dt }, END_DATE => { '>=' => $dt->ymd() }})->single +;

    Thanks anyway!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11105687]
Approved by Marshall
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-25 19:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found