Sorting Dates and serial number - mysql

Date SrNo
SEL/2016APR01/002/000001 01
SEL/2016APR02/002/000001 04
SEL/2016APR03/002/000001 03
SEL/2016JAN01/002/000001 02
I need to sort the first column Date part(2016JAN01) in Descending order and if there are two same dates then I need to sort the second column in Descending order.
substr('SEL/2016APR01/002/000001', 4,12) gives me 2016APR01 but I dont know how to sort dates of that format.
My query looks something like this
SELECT *
FROM tablename
ORDER BY substr(Date) DES, SrNo DESC

Update:
This first answer covers SQL Server, while the answer belows it is for MySQL. You originally tagged your question MySQL, and so everyone answered for that.
For SQL Server you can use CONVERT:
ORDER BY CONVERT(DATETIME,
SUBSTRING(Date, 12, 2) + ' ' +
SUBSTRING(Date, 9, 3) + ' ' +
SUBSTRING(Date, 5, 4),
106) DESC,
SrNo DESC
If you were actually using MySQL, then STR_TO_DATE would be the way to go:
ORDER BY STR_TO_DATE(SUBSTR(Date, 4, 12), '%Y%b%d') DESC,
SrNo DESC
Here is what the formatting parameters mean:
%Y - four digit year
%b - three letter month (e.g. JAN, APR)
%d - two digit day of month
If the upper case format of the month names results in STR_TO_DATE not working, you can try the following (which is admittedly a bit ugly):
ORDER BY STR_TO_DATE(
CONCAT(SUBSTRING(Date, 5, 4),
SUBSTRING(Date, 9, 1),
LOWER(SUBSTRING(Date, 10, 2)),
SUBSTRING(Date, 12, 2)),
'%Y%b%d') DESC,
SrNo DESC
As #RiggsFolly mentioned, your life would made easier if you stored your dates in a standard MySQL format.

Have you tried ORDER BY DATE_FORMAT(substr(Date, 4,12)), '%Y%b%d') DESC, SrNo DESC

use STR_TO_DATE FUNCTION
SELECT *
FROM tablename
ORDER BY STR_TO_DATE(SUBSTR(Date, 4, 12), '%Y%b%d') , SrNo DESC

Related

MySQL - VARCHAR to DATE returns null

Using Local Storage Connection within Toad Data Point. It's set up to use MYSQL.
EXTRACT_DATE is varchar(20) in the format of: 08282020
Trying to change that format so it appears as 08/28/2020 in my results set. However, I kept getting {null} returned.
I've tried a variety of different methods (see the code below) but keep getting the same result.
SELECT
EXTRACT_DATE,
DATE_FORMAT(EXTRACT_DATE, '%m/%d/%Y') AS NEW_DT,
STR_TO_DATE(EXTRACT_DATE, '%m/%d/%Y') AS NEW_DT2,
CONVERT (EXTRACT_DATE, DATE) AS NEW_DT3
FROM USERTABLE
GROUP BY EXTRACT_DATE
STR_TO_DATE turns a string into a date. To format the result, apply DATE_FORMAT like so:
select DATE_FORMAT( STR_TO_DATE('08282020', '%m%d%Y'),'%m/%d/%Y') AS NEW_DT from dual
It think it is simpler and more efficient to use sring functions:
concat_ws(
'/',
substring(extract_date, 1, 2),
substring(extract_date, 3, 2),
substring(extract_date, 5, 4)
) as new_dt
Or:
concat_ws(
'/',
left(extract_date, 2),
substring(extract_date, 3, 2),
right(extract_date, 4)
) as new_dt
As for the query you wanted to write: you need to convert to a date first, then back to a string. So:
date_format(str_to_date(extract_date, '%m%d%Y'), '%m/%d/%Y')

group by based on part of a column value in mySql

I have a mySql table call details. details table is having a column call date which is having date like 2015-02-01. There can be multiple rows from a same date.
I want to find how many rows COUNT() it contains for each month of the year.
for example,
'568', '192.168.1.100', '790', '1.00', '2014-11-14'
'569', '192.168.1.100', '780', '1.00', '2014-11-14'
'699', '192.168.1.100', '780', '1.00', '2014-11-16'
'767', '192.168.1.102', '780', '1.00', '2014-12-15'
'768', '192.168.1.102', '780', '1.00', '2014-12-15'
this should give COUNT like:
'2014-11-' as 3
'2014-12-' as 2
How can i do it using a sql select query.
Select DATE_FORMAT(My_Call_Date_Field, '%Y-%m') as MyYearAndMonth, Count(*) as MyCount
From My_Call_Details_Table Group By DATE_FORMAT(My_Call_Date_Field, '%Y-%m')
You can also use the CAST function of MySql.
Select substr(convert(My_Call_Date_Field, CHAR), 1, 7) as MyYearAndMonth, Count(*) From My_Call_Details_Table Group By substr(convert(My_Call_Date_Field, CHAR), 1, 7) Order By substr(convert(My_Call_Date_Field, CHAR), 1, 7)
In response to additional info specific to your case, as seen in your comment to the previous answer by Ed King: here it is :
SELECT DATE_FORMAT(detailtable.date, '%Y-%m') , AVG(detailtable.loadstate) as "load",count(detailtable.loadstate) as "count" from ruby_snmp.detailtable where ipaddtress='192.168.1.102' and date LIKE '2014-12-%' GROUP BY DATE_FORMAT(detailtable.date, '%Y-%m')
Convert to month and year using mysql functions, group and count:
SELECT month(calldate) as m, year(calldate) as y, count(*)
FROM calldetails
GROUP by m, y;
Given the additional information, I would do the following:
SELECT YEAR(detailtable.date) as y,
MONTH(detailtable.date) as m,
AVG(detailtable.loadstate) as "load",
count(detailtable.loadstate) as "count"
FROM ruby_snmp.detailtable
WHERE ipaddtress='192.168.1.102' and date LIKE '2014-12-%'
GROUP BY y, m;
The format variables y and m would be disregarded, but need to be there for the group to work. You can add the FORMAT function to give the specific YYYY-MM as was mentioned in the second post, or you can handle data presentation in the program / report writer.

How do I match a month range in the database to two dates?

Assume I have a table that contains a from_month (int, 1-12) and a to_month (int, 1-12) column, and I then have another table that contains a from (int, unix timestamp) and to (int, unix timestamp) column.
How would I match rows where the from_month and to_month columns contain my from and to timestamps, taking into account year boundaries on either side?
Scenario:
With from_month as 11 and to_month as 2, is there a way where I can get a from value of 1383264000 and a to value of 1391212800 to match?
At the same time however:
With from_month as 3 and to_month 8, is there a way where I can get a from value of 1362096000 and a to value of 1375315200 to match?
Clarifications:
I need records from the second table where the months from both timestamps fall between the range set out from the first table.
Also, I can obtain the month from the timestamp, so it's not necessary for MySQL to parse it, but the solution is likely unaffected.
Sorry but i'm not sure how you want to compare with the year
But if only month, you can use FROM_UNIXTIME to get date from a unixtime, and MONTH to get month from a date
for example
SELECT FROM_UNIXTIME(1391212800)
> February, 01 2014 00:00:00+0000
SELECT MONTH(FROM_UNIXTIME(1391212800))
> 2
with the month get from unix time, I'm sure you can do the rest or help explain more detail what you want to.
Hope this help
Here is the SQL how to create unix timestamp from your input :
SELECT
#year:= YEAR(CURDATE()),
#from:= CONCAT(#year, '-', 11, '-1') AS from_date,
#to:= CONCAT(IF(11<=2, #year, #year+1), '-', 2, '-1') AS to_date,
UNIX_TIMESTAMP(#from) AS from_timestamp,
UNIX_TIMESTAMP(#to) AS to_timestamp;
Month 11 & 2 are hard-coded. I used variables, so the sql is shorter.
SQL Fiddle.
Your scenario is :
With from_month as 11 and to_month as 2, is there a way where I can get a from value of 1383264000 and a to value of 1391212800 to match?
Which is from 2013-11-01 00:00:00 to 2014-02-01 00:00:00. Are you sure you need TO date to be from first of the month? or the last day of the Februar?
A solution was found that does an inclusive month check using months as integers from 0-11.
# Filter
SET #Fs = 2; # Mar
SET #Fe = 7; # Aug
#
SET #Ps = 2; # Mar
SET #Pe = 4; # May
SELECT
(
(
MOD(#Pe - #Fs + 24, 12) <= MOD(#Fe - #Fs + 24, 12)
)
AND (
MOD(#Ps - #Fs + 24, 12) <= MOD(#Pe - #Fs + 24, 12)
)
)
OR MOD(#Fe - #Fs + 24, 12)=11;

mysql sorting alphanumeric transaction code using date indicator

I have some data
SMMP0220113042212
SMMP0220113042211
SMMP0220113042210
SMMP022011304231
SMMP022011304229
SMMP022011304228
How query i must create to make if, "SMMP02201" is store code, "130422" is date (yymmdd), after that code is increment transaction for a day
SMMP022011304231
SMMP0220113042212
SMMP0220113042211
SMMP0220113042210
SMMP022011304229
SMMP022011304228
SELECT code
FROM table
ORDER BY substr(code, 1, 9), SUBSTR(code, 10, 6) DESC, CAST(substr(code, 16) AS DECIMAL) DESC;
FIDDLE

Group by month and year

I am currently updating an old site where the date used in the database is formated dd/mm/yyyy.
What I am trying to do is group the rows by month and year. Is this possible with this formatting?
You can always call STR_TO_DATE(value,'%d/%m/%Y').
Then you'll be able to group rows using ordinary date-extracting functions like YEAR() and MONTH().
Whats the column type for the column where date is stored?
if its DateTime then you can do this:
group by YEAR(date_col), MONTH(date_col)
Or you can GROUP BY with substr like
SELECT *
FROM table
GROUP BY substr(date, 4, 2)
where substr(date, 4, 2) = MM and if you want dd let's try substr(date, 1, 2)