year and month in where clause in oracle 10g - php

I have my date in format March 2014. How do I get month and year in number format to compare it in Oracle 10g query.
<?php
$date = "March 2014";
?>
My query that I am trying to get is
select EMP_NAME where month = "03" and year = "2014".
Here is my table structure
EMP_ID | EMP_NAME | C_DATE
1 | ABC | 01-FEB-14
2 | XYZ | 03-MAR-14
Note: I am writing the coding in php

For a sargable predicate you should avoid altering every row of data to suit the single parameter.
Instead: Adjust the parameter to suit the data
select
*
from your_table
where C_DATE>= to_date('March 2014','MON YYYY', 'NLS_DATE_LANGUAGE = American')
and C_DATE< add_months(to_date('March 2014','MON YYYY', 'NLS_DATE_LANGUAGE = American'),1)
Note the data - C_DATE - is not affected by any function, yet you get the desired outcome (all records of March 2014); this method permits use of indexes on the field C_DATE for query efficiency.

You can try with TO_CHAR(date, 'MON') for the months and TO_CHAR(date, 'YYYY') for the year.
The query will be
select EMP_NAME where TO_CHAR(C_DATE, 'MON') = month and TO_CHAR(C_DATE, 'YYYY') = year
And in php you'll have to manage the date to get year and month and pass them to the query
$dt = new DateTime("March 2014");
$month = $dt->format("m"); //03
$year = $dt->format("Y"); //2014
//Here connect to the db..
$db->query("SELECT EMP_NAME FROM yourTable WHERE TO_CHAR(C_DATE, 'MON') = :month AND TO_CHAR(C_DATE, 'YYYY') = :year");
$db->bindParam(":month", $month);
$db->bindParam(":year", $month);

Related

Laravel Eloquent Query With Between And Just Month And Day

I have a date in the datatabase like this: 2019-08-28
I need to make a query where I can select the rows between two dates BUT just filtering the month and the day. I have this:
$session_data = $request->session()->all();
$date1 = date('Y-m-d');
$date2 = date('Y-m-d', strtotime($date1. ' + 4 days'));
$employees = Employee::whereBetween('born_date', array($date1, $date2))->take(4)->get();
but this selects does not work because it is filtering with the year..i need something which it uses this:
'DAY(born_date) = '. date('d')
'MONTH(born_date) = '. date('m'))
But I do not know how to add this with between.. Thanks
Laravel has helpful whereXYZ() methods to filter by day/month/year/time.
You can filter by a given month:
/*
|
| Get all the employees that were born between
| june and december of any year in your db
|
*/
$employees = Employee::whereMonth('born_date', '>=', 6)->get();
or make a filter by day:
/*
|
| Get all the employees that were born between the
| 1st and the 10th of every month of every year
|
*/
$employees = Employee::whereDay('born_date', '=<', 10)->get();
Of course you can combine this methods to accomplish your desired output.
Try the code below
$employees = Employee::whereRaw("DATE_FORMAT(born_date, '%m%d') BETWEEN DATE_FORMAT(NOW(), '%m%d') AND DATE_FORMAT(DATE_ADD(NOW(), INTERVAL 4 DAY), '%m%d')")->take(4)->get();
The idea is to format dates as %m%d and compare these values

Given date as parameter retrieve month and year

In PHP, I want the system to automatically get the fixed date (14th) regardless of any time I request.
Question:
So do I need to write a custom function for this matter or are there any built in function that I can use?
I've also provided some example as below:
A=Current Date(input)
B=System output
Scenarios
Case 1
A: 2017-03-30
B: 2017-03-14
Case 2
A: 2017-05-31
B: 2017-05-14
Case 3
A: 2017-06-03
B: 2017-05-14
PHP
$fulldate = date("Ymd");
$year = substr($fulldate,0,4);
$month = substr($fulldate,4,2);
$date = substr($fulldate,6,2);
if($date <= 14){
$month -= 1;
}
echo $year.$month.'14';
MySQL
select * from table where DATE_FORMAT(my_date,'%Y%m%d') = $year.$month.'14';
You can get the "last 14th of a month" in SQL with
select concat_ws('-', year(now()), month(now()), 14) - interval (day(now()) <= 14) month
The "trick" ist this part:
- interval (day(now()) <= 14) month
which will substract one month only if the current day is <= 14
So you can use this query
select *
from `table`
where my_date = concat_ws('-', year(now()), month(now()), 14) - interval (day(now()) <= 14) month
without doing anything in PHP.
Using PHP you can do it the following way:
$date = new DateTime();
$date->modify('-14 day');
$last14th = $date->format('Y-m-14');
echo $last14th;
$sql = "select * from `table` where my_date = '{$last14th}'";
Update
Looking at the PHP code I realized, that the same logic can be used in SQL:
select date_format(now() - interval 14 day, '%Y-%m-14')
you may use this:
select DAY(DATE_FORMAT("2017-06-03",'%Y-%m-%d'));
+-------------------------------------------+
| DAY(DATE_FORMAT("2017-06-03",'%Y-%m-%d')) |
+-------------------------------------------+
| 3 |

Count records this month

I have a table that stores dates in the format of MM/DD/YY (field name is date). I need to have a count that totals just the records for the current month. From searching, this is what I currently have but something is not right. Do I need to do a convertToDate, is it that my field is called "date", or am I missing something entirely?
$totalcount = mysql_query("select count(*) as 'total'
FROM state_to_state
WHERE status = 99 AND type = 1
AND MONTH(`date`) = MONTH(CURDATE())
AND YEAR(`date`) = YEAR(CURDATE())");
$totalnum = mysql_fetch_array($totalcount);
if($totalnum['total'] > 0) { $month_status = $totalnum['total']." this Month. "; }
Your dates are NOT native mysql dates (varchar, probably?), so you cannot use the mysql date functions on them directly. date('MM/DD/YY') will not work reliably:
mysql> select month('03/03/14'), month('03/18/14');
+-------------------+-------------------+
| month('03/03/14') | month('03/18/14') |
+-------------------+-------------------+
| 3 | NULL |
+-------------------+-------------------+
Convert your date fields to native mysql date types, or take a huge hit on performance and convert them to native date values onthefly via str_to_date().

Comparing current date/time with date/time in a database

I need to create something to display an image based on the current date and time. I have an SQL table set up that looks like this:
eventid | startdate | enddate | imgfile | status
6 | 2013-04-29 | 2013-05-03 | finals.jpg | active
What I need to do is compare the start date and end date to the current date, and if the current date falls within the startdate and enddate, then display the imgfile.
I'm not sure how to go about this, but this is what I tried.
//get current date and time
$currentdatetime = strtotime(now);
$formatteddatetime = date("y-m-d", $currentdatetime);
while($row=mysql_fetch_array($getimage))
{
//get variable for startdate
$formattedstartdate = date("y-m-d", $row[startdate]);
$formattedenddate = date("y-m-d", $row[enddate]);
if($formatteddatetime >= $formattedstartdate AND $formatteddatetime <= $formattedenddate AND $row[status] == 'active')
{
echo $row[imgfile];
}
}
?>
Right now, it doesn't show anything inside of the entire while statement. Am I at least on the right track?
the CURRENT_DATE returns the current date in ‘YYYY-MM-DD’ format or YYYYMMDD format depending on whether numeric or string is used in the function. CURDATE() and CURRENT_DATE() are the synonym of CURRENT_DATE.
You should do it in your SQL query instead of PHP. For example:
SELECT * FROM table WHERE CURRENT_DATE BETWEEN startdate AND enddate;
CURRENT_DATE() is a MySQL function that returns the current date, as the name suggests. It is one of a few special functions that have an alias without the parentheses.

from date to date - search and display

I have a database holding two bits of information. from date to date. I need this entry to appear in my calender on every day, but I can only manage to get to appear on the first and last date.
example of DB:
job_no | date1(from)| date2(to)
________________________________
1 |2013-01-28 | 2013-02-03
2 |2013-01-14 | 2013-01-18
Edit for question. the search bar I have allows for ONE date input and the the calender finds entries through date1 and the next 6 days.
I cannot have a search which contains two date inputs because my users are so used to this way and i do not want to increase searching time. I started to think that I had to find the dates between the dates, add that to an array then use an if statement to find matches...but even saying this makes no sense to me.
regarding job 1, I need my calender to show this job up on all dates 28/29/30/31/01/02/03.
My current search SELECT * FROM jobs WHERE date1='$searchinput' PER day and calender this search. I use strtotime to increase the input date by +1 to add to the search for each day.
Calender page.
What I want with my results. User searched Date 28th.
Mon 28 | Tues 29 | Wed 30 ...... | Fri 03 |
_________________________________________________________________________
Job no 1 | job no 1 | job no 1 ...... | job no 1
What I have now.
Mon 28 | Tues 29 | Wed 30 ...... | Fri 03 |
_________________________________________________________________________
Job no 1 | blank | blank | job no 1
each day has a new select query right now. It matches days with date 1 and date 2. I dont need this as before I only had jobs out on one day now they go out for more than one day and need the job to be noted on all days it is out by only using a job from date and job to date.
EDIT 2:
SELECT * FROM calender_db NATURAL JOIN job_db
WHERE section='two'
AND date1 < '$day' AND date2 > '$day'
OR date1 = '$day' OR date2 = '$day'
This query selects what I need, but as I am using OR the first WHERE CLAUSE can be null. I need that to always be in the clause. I have been looking at using IIF or CASE to rectify but do not how to implement 100%...?
why not use BETWEEN
SELECT * FROM tableName WHERE date BETWEEN 'date1' AND 'date2'
SQLFiddle Demo
UPDATE 1
SELECT *
FROM tableName
WHERE date BETWEEN '2013-01-28' AND '2013-01-28' + INTERVAL 6 DAY
SQLFiddle Demo
To generate a list of days between two dates:
$days = array();
$stop = strtotime($date2);
for ($current = strtotime($date1); $current <= $stop; $current = strtotime('+1 days', $current)) {
$days[] = date('d', $current);
}
echo join('/', $days);
Demo
Update
Misunderstood the question it seems, if both dates are stored as columns and you're querying with a single date:
SELECT *
FROM jobs
WHERE 'date_from_input' BETWEEN date1 AND date2
Update 2
Your latest query can be written as:
SELECT *
FROM calender_db NATURAL JOIN job_db
WHERE section='two' AND '$day' BETWEEN date1 AND date2
Try like this
SELECT * FROM Mytimetable WHERE date BETWEEN 'date1' AND 'date2'
Query between date1 - date2
SELECT *
FROM `objects`
WHERE (date_field BETWEEN '2013-01-30 14:15:55' AND '2013-02-19 10:15:55')
OR
SELECT *
FROM `objects`
WHERE (date_field BETWEEN date1 AND date2)
query to select dates between two dates with PHP variables.
i will explain with exapmle :
$date1 = "2013-02-19";
$date2 = "2013-02-25";
then sql query will be :
$query = " SELECT * FROM table_xyz where (date_item BETWEEN date('".$date1."') AND date('".$date2."')";
hope it solves your problem
SELECT
*
FROM
tablename
WHERE
date between '$date1' and '$date2'
order by
date

Categories