CodeIgniter active record compare a date - codeigniter

I have a column in a db with a date stored as mm/dd/yyyy. I need to grab all the rows with dates greater than a certain date. (Ex. any date after 01/01/2013). How do I do this using active record?
I have tried
$this->db->select('DATE_FORMAT(date, '%mm/%dd/%Y') as mydate');
$this->db->where('mydate >','01/01/2013');

$this->db->select("DATE_FORMAT(date, '%m/%d/%Y') as mydate",FALSE);
$this->db->from('table');
$this->db->where("DATE_FORMAT(date,'%Y-%m-%d') > '2013-01-01'",NULL,FALSE);

Related

Failed to parse input string "31-Dec-2019" ERROR

Data Type of my date column in the big query table is String.
Format of the date: 31-Dec-2019
I have records for 2018, 2019, 2020 years.
Now I want to filter out the data after 2020-01-01.
With the below code, where-condition is not working here and returning all the records of the table.
Select
*
from table T
where date > '2020-01-01'
Tried the below and their respective errors:
PARSE_DATE('%d-%m-%Y',SUBSTR(date,1,12)) - Failed to parse input string "31-Dec-2019"
CAST( date as DATE) > '2020-01-01' -- returning all the records in the table/where condition is not working
Can someone please help me with this?
You need to use %b for abreviated month name.
select PARSE_DATE('%d-%b-%Y',SUBSTR('31-Dec-2019',1,12))

How do i get result by comparing Date column and Time column when Date and Time column are separate columns in Laravel

I want to fetch events based on their end_date and end_time from events table. So i need to fetch those events only which are in the future. So if current date and time is greater than end_date and end_time then then it should not fetch those records because the the date and time has passed.
I have done so far :
$today = Carbon::now();
return $query->where('end_date', '>=', today()->format('Y-m-d'))
->where('end_time', '>=', $today->toTimeString());
But this code will not work as expected.
Lets suppose The record is end_date = 2020-02-05 and end_time = 08:00:00 and right now it's 20:10:00 in my town. So As i can see this is a future record and i want it to be fetched but it will not fetch because of where('end_time) because 08:00:00 >= 20:10:00 is false hence this solution will not work
How can i write a query where i will be able to check first if date is today then only check time.
Thanks in advance.
You can try to combine the two fields at a database level (assuming you are using MySql):
return $query->whereRaw('TIMESTAMP(`end_date`,`end_time`) >= ?', [now()->toDateTimeString()]);
You have to change the two columns and use just one column of the type datetime and then you can get the date or the time in the code

Filter table by another table based on date field

I have a detail table in my model with a row for every hour in a day and the sales amount generated to that point of the date. Its not displayed on my report, rather it acts as the base for another reference table in the model:
Datetime Sales
2019-11-10 06:00:00 100.00
2019-11-10 12:00:00 200.00
2019-11-10 18:00:00 500.00
2019-11-10 23:59:59 999.00
The first (reference) table in the model only displays in my report the last hour value of each day in my base table. they are related via the datetime field using a 1:M with a single cross filter setting:
let
Source = Base,
#"Filtered Rows" = Table.SelectRows(Source, each ([Datetime] = #time(23, 59, 59)))
in
#"Filtered Rows"
Datetime Sales
2019-11-10 23:59:59 999.00
2019-11-11 23:59:59 950.00
2019-11-12 23:59:59 900.00
I would like to place a third table on my report that shows each of the hourly rows from the base table when a user clicks on a row in the daily table with the default setting being today's data which is added hourly.
I went down the path of using Power Query to try and filter the base table but have not been able to make it work. Should this instead be done using DAX? Either way, what would the query look like?
thanks in advance!
Thanks, but I have just resolved this. By changing my relationship to use a date field instead of the datetime field, and changing it to a (*:1) type, i am now getting the filter to do what I want. Now just have to figure out how to default to today's date. Thanks again!

Date format in Oracle- fetching Date of certain range

I have a date table in my db in Oracle. When I run a query I get the date format as '01-05-2015' but when I run a similar query in BIRT, I get the date format as '01-MAY-2015 12:00 AM'. How can I get the date format in dd/mm/yyy by keeping the data type of date field as date.
here is sample of my database.
EQ_DT
05-07-2015
06-06-2015
15-02-2015
19-09-2015
28-12-2015
also my query is :
select to_date(to_char(to_date(enquiry_dt,'DD/MM/YYYY'),'DD/MM/YY'),'DD/MM/YY') as q from xxcus.XXACL_SALES_ENQ_DATAMART where to_date(to_char(to_date(enquiry_dt,'DD/MM/YY'),'DD/MM/YY'),'DD/MM/YY')>'21-06-2012' order by q
I am getting error of NOT A VALID Month also
If enquiry_dt is already a date column, why are you trying to convert it to date (and then to char and to date again)?
SELECT to_char(enquiry_dt, 'DD/MM/YYYY') AS q
FROM xxcus.xxacl_sales_enq_datamart
WHERE enquiry_dt > to_date('21-06-2012', 'dd-mm-yyyy')
ORDER BY enquiry_dt
In birt, where you place the field on the report, set the field type to date. Then in properties for that field , go to format date time, and finally specify the date formatting you want for that field .
I prefer to always use pass date parameters as strings to BIRT, using a known date format. This is for report parameters as well as for DataSet parameters.
Then, inside the query, I convert to date like this:
with params as
( select to_date(pi_start_date_str, 'DD.MM.YYYY') as start_date_incl,
to_date(pi_end_date_str, 'DD.MM.YYYY') + 1 as end_date_excl
from dual
)
select whatever
from my_table, params
where ( my_table.event_date >= params.start_date_incl
and
my_table.end_date < params.start_date_excl
)
This works independent of the time of day.
This way, e.g. to select all events for january 2016, I could pass the query parameters '01.01.2016' and '31.01.2016' (I'm using german date format here).

How to calculate Date difference in Hive

I'm a novice. I have a employee table with a column specifying the joining date and I want to retrieve the list of employees who have joined in the last 3 months. I understand we can get the current date using from_unixtime(unix_timestamp()). How do I calculate the datediff? Is there a built in DATEDIFF() function like in MS SQL? please advice!
datediff(to_date(String timestamp), to_date(String timestamp))
For example:
SELECT datediff(to_date('2019-08-03'), to_date('2019-08-01')) <= 2;
If you need the difference in seconds (i.e.: you're comparing dates with timestamps, and not whole days), you can simply convert two date or timestamp strings in the format 'YYYY-MM-DD HH:MM:SS' (or specify your string date format explicitly) using unix_timestamp(), and then subtract them from each other to get the difference in seconds. (And can then divide by 60.0 to get minutes, or by 3600.0 to get hours, etc.)
Example:
UNIX_TIMESTAMP('2017-12-05 10:01:30') - UNIX_TIMESTAMP('2017-12-05 10:00:00') AS time_diff -- This will return 90 (seconds). Unix_timestamp converts string dates into BIGINTs.
More on what you can do with unix_timestamp() here, including how to convert strings with different date formatting: https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-DateFunctions
yes datediff is implemented; see:
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF
By the way I found this by Google-searching "hive datediff", it was the first result ;)
I would try this first
select * from employee where month(current_date)-3 = month(joining_date)

Resources