How can i do a count of two columns in mysql? - mysql

i want to do a count of two columns in mysql. One of the columns is a string but another is a date like 06/08/2017 and when i do my query i get 0 results.
SELECT count(*) FROM `castigos` WHERE inicio_normal=05/06/2017 AND cod_emplazamiento=1
I have entries of that data but its dont show me anything. Maybe the type of data in the date is wrong?
What should i do?

Add the date field to your select and group by it. Otherwise mysql extensions doesn't recognize you want to group by the date and will aggregrate all the results into 1 column. And since you are getting 0 count, you're where clause must not be working.
Your date format seems malformed. usually YYYY/MM/DD format (standard format);
or specify a format using SELECT STR_TO_DATE('17/09/2010','%d/%m/%Y');
MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The supported range is '1000-01-01' to '9999-12-31'.
the below uses the implicit casting and default date format to convert the string date to a valid date.
SELECT inicio_normal, count(*)
FROM `castigos`
WHERE inicio_normal='2017/05/06'
AND cod_emplazamiento=1
GROUP BY inicio_normal
Otherwise its doing math and comparing that date to the number stored for the date.
Understand dates should be stored in a date datatype and when you query dates you're passing in a string that is being cast to a date datatype for comparison. So you need to use the standard format, or cast your string to a date so the db engine knows how to convert your format to a date.

Try this :
SELECT count(*) FROM `castigos` WHERE inicio_normal="05/06/2017" AND cod_emplazamiento=1 GROUP BY inicio_normal

WHERE inicio_normal=05/06/2017
If you divide 3 by 6 then by 2017 you get a very small value indeed. OTOH if you reformat this as a date (e.g. 20170605, if you gave us a European formatted date - dd/mm/yyyy) then your query will find the rows you showed us.

Related

SSRS how to sort date in MM/YY format?

I'm stuck at this point, I get my data from a stored procedure with the date format as MM/YY, but SSRS sorted my date in a wrong way: 01/2019, 02/2019,..., 12/2019;01/2018, 02/2018,...
So I'd like to do to have my data in the right order.
Since your date is converted to MM/YYYY format, the data is text and not numerical so it's sorted one character at a time rather than by the value.
If you want to sort by year and then by month, you would need a separate SORT option for each that parses the text into separate month and year values.
=RIGHT(Fields!DOB.Value, 4)
This gets the 4 characters from the right of the text which is the year in the data.
=LEFT(Fields!DOB.Value, 2)
LEFT , 2 gets the first two characters of the string - the month in the field.
Create a new column in the query where you convert the string date to a date data-type and sort on that e.g.
select
convert(date, '01/'+ MyDateStringColumn) MyDateColumn
from MyTable
order by MyDateColumn

MySQL Query Group By Date

I have tried various recommendations based off of other posts with no avail.
I have a database scheme of records with a Created_Date Key, and Value would be 01/01/2017
I am trying to query the database records to give a returned count of How many records per month and which month those fall in line with.
With the following
SELECT SQL_NO_CACHE MONTH(`Created_Date`), COUNT(*)
FROM `CRM_Leads`
GROUP BY MONTH(`Created_Date`)
I return
MONTH(`Created_Date`) COUNT(*)
NULL 872
I have also tried almost all the variations on the following post
Count records for every month in a year
Any help would be appreciated.
assuming your created_date is a string of format ('dd-mm-yyyy') the you should convert as date with str_to_date
SELECT SQL_NO_CACHE MONTH(str_to_date(`Created_Date`, '%d/%m/%Y')), COUNT(*)
FROM `CRM_Leads`
GROUP BY MONTH(str_to_date(`Created_Date`, '%d/%m/%Y'))
For as long as you store date/time information as strings, you will have great difficulty using any date/time specific functions and features. If you are getting NULL from MONTH(str_to_date(Created_Date, '%d/%m/%Y')) then the str_to_date isn't converting the strings to dates and the most likely reason for this is the d m y "pattern" is not corrrect.
All you have old us about your "strings that might be dates" is that one of them looks like this: 01/01/2017. Now that could be DD/MM/YYYY or MM/DD/YYYY and we simply cannot tell which one is correct from the single value you have chosen to share with us. Look for any day value greater then 12 in your data e.g. 17/01/2017 ==> DD/MM/YYYY or 01/17/2017 ==> MM/DD/YYYY
Once you have made the choice of which pattern your "strings that might be dates" follow; apply that pattern in the str_to_date() function. You migh want to try a few different patterns to get the best one (and these are just 3 of many you could try):
# which pattern is best for you?
SELECT Created_Date
, str_to_date(`Created_Date`, '%d/%m/%Y') "d/m/y"
, str_to_date(`Created_Date`, '%m/%d/%Y') "m/d/y"
, str_to_date(`Created_Date`, '%Y-%m-%d') "y-m-d"
FROM `CRM_Leads`
You will not have success with your group by query until you choose the most appropriate d m y pattern to apply in teh str_to_date function. Note here that you might also have a variety of patterns in your data, in which case you have an even bigger problem to solve.
Once you have made the choice of which pattern your "strings that might be dates" follow; apply that pattern in the str_to_date() function and ONLY THEN your group by query will work.

How to select latest date from database in MySQL

I have list of date in MySQL in the format of "MM-DD-YYYY" and When I was trying to fetch the latest date from table it just return the last date of a Year like 12-01-2014 instead of return latest date 03-16-2016.
Payment history table:
to_date
03-16-2016
12-01-2014
11-07-2014
10-03-2014
01-09-2014
I used following query:
SELECT MAX(to_date) FROM paymenthistory WHERE empid=59;
Result : 12-01-2014
Related post: Get the latest date from grouped MySQL data
Thanks in advance
You're working with strings, not native dates, so you're getting the maximum date.
Either convert those strings to ACTUAL mysql date/datetime values, or you'll have to go with ugly hacks, like
SELECT MAX(STR_TO_DATE(to_date, '%m-%d-%Y'))
and performance will be massively bad. MySQL's native date format is yyyy-mm-dd hh:mm:ss, which is a natural "most significant first" format. If your date strings were formatted like that, then even a max(string) would work.
It sounds like your date column is actually a VARCHAR format since it is seeing 12-01-2014 as the last date which is only true if stored as a VARCHAR.
Be sure your to_date column is a DATE type.
have you tried this?
SELECT TOP 1 * FROM paymenthistory WHERE empid = 29 ORDER BY to_date DESC;
For mysql try this
SELECT * FROM paymenthistory WHERE empid=59 ORDER BY to_date DESC LIMIT 1;

MySQL: Modifying output of a field for calculation

I know calculating age from DOB is relatively simple but I have an issue with different data entry formats in the database. Also, I know this can be easier using PHP, but I don't know PHP and only have MySQL to work with.
The DOB entered into the DB is entered as "month/day/year" or "00/00/0000". But when calculating against today's date, the date would be formatted as "year-month-day" or "0000-00-00". Furthermore, the month placed in the DOB field can have either a one number month (1/01/1999) or a two number month (01/01/1999), so it's not consistent.
I am trying to use the below to utilize CONCAT, SUBSTRING and LOCATE to output the DOB in a better suited format for the age calculation. I think I'm close but not quite there. Any help would be very much appreciated.
SELECT
CONCAT(SUBSTRING(APPU_DOB,-4,4),'-', SUBSTRING(APPU_DOB,LOCATE('/', APPU_DOB),1),'-',SUBSTRING(APPU_DOB,4,2))
FROM APPU_APP_USER
JOIN APPL_APP ON APPU_APPL_ID = APPL_ID
WHERE DATE_FORMAT(APPL_CREATE_DT, '%Y-%M-%D') >= '2014-01-01';
Instead of Concat use str_to_date function.
select str_to_date( appu_dob, '%m/%d/%Y' ) as 'dob';
on 1/01/1999 it returns a valid date formatted object with value 1999-01-01.
You can use it on other date strings that have single or two digit day or month numbers.
Note: To represent or refer a month, use small case m but not capital M, in the format pattern string.
And you should better redefine the data type of appu_dob field to date. So that you can easily apply date specific functions on it for any calculations.

Convert different varchar dates to date

I'm having some troubles with converting varchar types to date types.
In SQL-server 2008 I'm having a table with different types of dates in type VARCHAR.
For example:
DDMMYYYY (05032013)<br/>
MMDDYYYY (03052013)<br/>
YYYYMMDD (20130305)<br/>
...
I have to convert these different string types to type "date" using a SQL-query.
Any suggestions how I can do this?
These are my records:
TYPE || FORMAT
____________________
DDMMYYYY||05032013
MMDDYYYY||03052013
YYYYMMDD||20130305
Try this
SELECT case
when type='DDMMYYYY' then convert(date,STUFF(STUFF('05032013',3,0,'-'),6,0,'-'),105)
when type='MMDDYYYY' then cast(substring('03052013',5,4)+'-'+substring('03052013',0,3)+'-'+substring('03052013',3,2) as date)
when type='YYYYMMDD' then cast(STUFF(STUFF('20130305',5,0,'-'),8,0,'-') as date)
end
If you can identify every row which date format is, you can convert them to date type. Otherwise i don't think that it will be completely correct. For example you can't implement logic for the following record - (03052013) can be 2013-05-03 or 2013-03-05.