My current table is the following. Rosters: ID(int), RosterDate(varchar), Worker(varchar), Dealership(varchar), Yard(varchar), Start(varchar).
I know my RosterDate column is not set to datetime, i'm hoping for a solution where I don't need to change the datatype and have the SQL handle the query/solution.
I'm trying to retrieve all rows that is todays date and greater, and the order by date in descending order. My date entries are formated in the following DD/MM/YYYY.
The issues that i'm having is that SQL operates the date formatting in the following format YYYY-MM-DD HH:MM:SS.
My current SQL statement.
SELECT * FROM rosters WHERE Worker = 'John Doe' AND FORMAT(RosterDate, 'dd-MM-yyyy') >= CURRENT_DATE() ORDER BY FORMAT(RosterDate, 'dd-MM-yyyy') DESC
Theres two issues with this SQL Statement
FORMAT(RosterDate, 'dd-MM-yyyy') >= CURRENT_DATE()
Doesn't work as intended.
AND
FORMAT(RosterDate, 'dd-MM-yyyy') DESC
Only Orders By the DD value if do
SELECT FORMAT(RosterDate, 'dd-MM-yyyy') AS Test ....
It returns
27
26
25
Instead of
27/10/2019
26/10/2019
25/10/2019
What's the appropriate SQL query in order to retrieve the results? I've brushed through the documentations searching for a solution but I keep getting issued by the dates' formatting.
Here's most of the code im using:
The form:
<?php include '../../dbsession.php'; ?>
<?php
$workers_stmt = $db->prepare("SELECT * FROM workers");
$workers_stmt->execute();
$workers_row_count = $workers_stmt->rowCount();
$yards_stmt = $db->prepare("SELECT * FROM yards GROUP BY YardName");
$yards_stmt->execute();
$yards_row_count = $yards_stmt->rowCount();
$rosters_stmt = $db->prepare("SELECT * FROM rosters");
$rosters_stmt->execute();
$rosters_row_count = $rosters_stmt->rowCount();
$datetime = isset($_POST['datetime']) ? date("j/n/Y", strtotime($_POST['datetime'])) : date('j/n/Y', strtotime('now'));
?>
<thead>
<tr>
<th class="darkest" style="width: 150px;"><?php echo isset($_POST['datetime']) ? date('l', strtotime($_POST['datetime'])) : date('l'); ?>
<?php echo $datetime; ?></th>
<?php
$i = 0;
$worker = array();
if($workers_row_count > 0){
while($row = $workers_stmt->fetch(PDO::FETCH_ASSOC)){
$worker[$i] = $row['FName'].' '.$row['LName'];
echo '<th class="text-center darkify" style="width: 100px !important;">'.$row['FName'].' '.$row['LName'].'</th>';
$i++;
}
}
?>
</tr>
</thead>
<tbody>
<?php
while($row = $yards_stmt->fetch(PDO::FETCH_ASSOC)){
$html = '<tr>';
$html .= '<td class="darkify" style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">
<b>['.$row['Dealership'].']</b><br/><span class="text-primary">['.$row['YardName'].']</span>
</td>';
for($i = 0; $i < count($worker); $i++){
$variables = "'".$datetime."', '".$row['Dealership']."', '".$row['YardName']."', '".$worker[$i]."'";
$html .= '<td class="p-0 m-0" id="'.str_replace(" ", "_", '['.$datetime.']['.$row['Dealership'].']['.$row['YardName'].']['.$worker[$i]).']">
<button class="btn-dark w-100 text-success" style="height: 70px;" onClick="addRoster(event, this.childNodes[1], '.$variables.')">
<select class="form-control">
<option value="4AM">4AM</option>
<option value="5AM">5AM</option>
<option value="6AM" selected>6AM</option>
<option value="7AM">7AM</option>
<option value="8AM">8AM</option>
<option value="9AM">9AM</option>
<option value="10AM">10AM</option>
<option value="11AM">11AM</option>
<option value="12PM">12PM</option>
<option value="1PM">1PM</option>
<option value="2PM">2PM</option>
<option value="3PM">3PM</option>
<option value="4PM">4PM</option>
</select>
</button>
</td>';
}
$html .= '</tr>';
echo $html;
}
?>
</tbody>
<script type="text/javascript">
cRB = '<button class="btn-dark w-100 text-danger" style="height: 70px;" onClick="cancelRoster(this)"><b>CANCEL</b></button>';
<?php
$x = 0;
while($rosters_row = $rosters_stmt->fetch(PDO::FETCH_ASSOC)){
for($i = 0; $i < count($worker); $i++){
if($rosters_row['Worker'] == $worker[$i]){
$x++;
echo 'try{ document.getElementById("'.str_replace(" ", "_", '['.$rosters_row['RosterDate'].']['.$rosters_row['Dealership'].']['.$rosters_row['Yard'].']['.$worker[$i]).']").innerHTML = cRB; } catch { console.log("APPLYING ROSTER '.$x.'...") }';
}
}
}
?>
</script>
The Javascript Function:
function addRoster(event, element, date, dealership, yard, worker){
if(event.path[0].tagName == "SELECT"){
//DO NOTHING.
}else{
datastring = "";
for(i = 0; i < element.childNodes.length; i++){
if(element.childNodes[i].selected){
datastring += "start=" + element.childNodes[i].value + "&";
}
}
datastring += "date=" + date + "&";
datastring += "dealership=" + dealership + "&";
datastring += "yard=" + yard + "&";
datastring += "worker=" + worker;
element.parentNode.parentNode.innerHTML = http("POST", "/admin/formfunctions/add_roster.php", datastring);
}
}
The add_roster.php
<?php
if(isset($_SESSION["admin"]) && $_SESSION["admin"] == true){
$keyval = "[".$_POST['date']."][".$_POST['dealership']."][".$_POST['yard']."][".$_POST['worker']."]";
$date = $_POST['date'];
$dealership = $_POST['dealership'];
$yard = $_POST['yard'];
$worker = $_POST['worker'];
$start = $_POST['start'];
$add_roster_stmt = $db->prepare("INSERT INTO rosters (KeyVal, RosterDate, Dealership, Yard, Worker, Start) VALUES (:keyval, :date, :dealership, :yard, :worker, :start)");
$add_roster_stmt->execute(array(
"keyval" => $keyval,
"date" => $date,
"dealership" => $dealership,
"yard" => $yard,
"worker" => $worker,
"start" => $start
));
$html = '<button class="btn-dark w-100 text-danger" style="height: 70px;" onClick="cancelRoster(this)"><b>CANCEL</b></button>';
echo $html;
}
?>
Based on the fact that you are using CURRENT_DATE()
and the result you are getting from FORMAT
it would appear you are actually using a MySQL server, in which case the function you need to use is STR_TO_DATE
:
SELECT *
FROM rosters
WHERE Worker = 'John Doe'
AND STR_TO_DATE(RosterDate, '%d/%m/%Y') >= CURRENT_DATE()
ORDER BY STR_TO_DATE(RosterDate, '%d/%m/%Y') DESC
You should fix your data model. Storing dates using the correct format will simplify just about everything you do with your data model.
In SQL Server, FORMAT()
converts a value to a string. You want to convert your value to a date . One method uses CONVERT()
:
SELECT r.*
FROM rosters r
WHERE Worker = 'John Doe' AND
CONVERT(DATE, RosterDate, 103) >= CONVERT(DATE, GETDATE())
ORDER BY CONVERT(DATE, RosterDate, 103) DESC;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.