What is the process for sorting non-integer records in MySql?

I need help sorting MySQL records in descending order to retrieve the most recent entry.

Here are the records stored as strings:

1/2017
1/2018
2/2017
2/2018
3/2017

I specifically want to retrieve the value 1/2018. The entries correspond to each year and the latest one added was 1/2018, followed by 2/2018, and so forth.

This is the query I am using:

SELECT NoControl FROM cita ORDER BY NoControl DESC LIMIT 1

Unfortunately, instead of 1/2018, I am currently getting 3/2017 as the last record returned.

Answer №1

If you come across the pattern m/yyyy, you can manipulate strings using a function like this:

SELECT NoControl 
FROM cita 
ORDER BY right(NoControl,4) DESC, left(NoControl,1) DESC LIMIT 1

It's best to avoid storing date values as strings...

Instead, consider converting the string into a proper date format like this:

SELECT NoControl 
FROM cita 
ORDER BY str_to_date(NoControl,'%m/%Y') DESC LIMIT 1

Answer №2

Utilize the SUBSTRING_INDEX function for extracting the first and second parts, then convert them to integers and sort:

SELECT str
FROM testdata
ORDER BY 
    CAST(SUBSTRING_INDEX(str, '/', -1) AS UNSIGNED) DESC,
    CAST(SUBSTRING_INDEX(str, '/',  1) AS UNSIGNED) DESC
LIMIT 0, 1

SQL Fiddle

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to retrieve file name with Jquery Ajax and PHP due to error

Trying to display the file name "echo $_FILES['userfile']['name'];" on browser console resulted in "function File() { [native code] }". Below is the jQuery code snippet: <?= form_open_multipart('',' id="importform" m ...

Place an image on top of adsense

Is there a way to overlay an image on top of Google AdSense ads in order to track user clicks? I am trying to implement an onclick method for when someone clicks on the ads. .ad-alert { width: 728px; height: 400px; background: #cccccc; mar ...

Update the content of a div element with the data retrieved through an Ajax response

I am attempting to update the inner HTML of a div after a certain interval. I am receiving the correct response using Ajax, but I am struggling to replace the inner HTML of the selected element with the Ajax response. What could be wrong with my code? HTM ...

Tips on avoiding quotation marks in a Less variable containing a color identifier

I'm currently working on an HTML/CSS project where I aim to establish classes for labels and texts based on their color. For instance: .text-red{ color: red; } .label-white{ color: white; } In order to achieve this, my approach involves cr ...

What are the best methods for ensuring a website maintains consistent visibility across all different screen sizes?

Recently, I created a website tailored for a viewport size of 1366px by 768px. My goal is to maintain the same design regardless of the browser window size without implementing responsive design techniques. For instance: When viewing the website on a 360 ...

Issue with overlay functionality after updating to jQuery version 1.12.1

Hey there! I'm currently in the process of upgrading my web application system's jQuery to version 1.12.1 and I've run into an issue with the overlay not functioning properly in the new jQuery version. My setup involves using ajax to displ ...

What is the best way to make a CSS element move with Javascript?

Currently working on a JavaScript game where I am in need of a CSS object to replace the original JavaScript object. Specifically, I want my "sword" CSS object to move along with my player object when it is Unsheathead. All the examples I find only show wh ...

JavaScript Summation Calculation

I am currently working on calculating the sum of three scores and displaying the total as "Total:". However, I am facing an issue in dynamically updating the total whenever a score value changes. Is there a way to utilize the "onchange" event to achieve th ...

Troubleshooting: Android compatibility issues with dynamic source for HTML 5 video

My HTML5 video with dynamic source loaded using JavaScript is functioning properly in a web browser but encountering issues within an Android PhoneGap build application. Take a look at the code snippet below: JavaScript code: $('#video_player' ...

The display of website content across various screens

I'm relatively new to creating websites using scripts, CSS, etc. But I feel like I'm getting the hang of it pretty well... Now I've reached a point where I want my site to look good on different screen resolutions. Currently, I have somethin ...

What is the best way to customize the appearance of only one of two identical tables using HTML and CSS?

I have two tables that look identical: <html> <head> <style> td, th { text-align: left; padding: 28px; } </style> </head> <body> <table> <tr> ...

Unable to change the DateTime back to the current time

Recently, I encountered an issue with a DateTime object that I created. I modified it to go back 14 days ago, but when I attempted to revert it back to today's date, it did not update accordingly. If anyone has any insights or solutions to offer, I w ...

Position a div container to be aligned at the bottom of an image

I'm having trouble aligning a div container at the bottom of an image. I attempted to modify the position attribute of the image and set its value to "relative." It's a bit challenging to explain, but here are snippets of HTML and CSS code with ...

Adjust prices of multiple specific products selectively in Woocommerce

After finding code on Stack Overflow to increase the price of a specific product by $10, I encountered an issue when trying to apply this to multiple products. I attempted modifying line 7 of the code snippet: if( $product->get_id() != 87 ) return $pri ...

How come flex won't let me rearrange an image?

.nav { height: 500px; } .navphoto { height: 500px; display: flex; justify-content: flex-end; } <div class="nav"> <img class="navphoto" src="images/navphoto.jpg"> </div> Just wondering why I can't seem to move an image, bu ...

What are the steps to integrating databases with HTML5?

I currently have SPA applications developed with angularjs, but I am in need of creating an installer that allows users to work offline using a secure local database. I experimented with IndexedDB and converted my SPA into a Chrome extension, however, I di ...

Troubles with aligning and floating three divs in the center of a container div – a CSS conundrum illustrated with code snippets and a basic diagram

I'm struggling to center 3 divs within a "container." Here's a rough idea of what I'm going for: ______________________ | ___ ___ ___ | | |___| |___| |___| | |______________________| The issue I'm facing is g ...

What is the best way to direct visitors who are not logged in to the Login page in Wordpress?

I have been struggling to find an answer to this question for my specific case with the current version of Wordpress. I have a website running on Wordpress with Buddypress installed. My goal is to restrict non-logged in users to only see the wp-login.php ...

What is the method for modifying the chosen color using a select option tag instead of a list?

element, I have a Vue component that features images which change color upon clicking a list item associated with that color. <div class="product__machine-info__colors"> <ul> <li v-for="(color, index) in machine.content[0] ...

Using ReactJS to strip HTML tags from JSON response

I'm having trouble figuring out how to strip HTML tags from a JSON response in reactjs. Here's the JSON response: { "price": "26,800.98", "diff": "<!--daily_changing-->+13.44 (+0.05%)&nbsp;& ...