I have a mySQL query which is outputting decimal fields with a comma.
SELECT Metals.Metal, FORMAT(Fixes.GBPam, 3) AS AM, FORMAT(Fixes.GBPpm, 3) AS PM,
DATE_FORMAT(Fixes.DateTime, '%d-%m-%y') AS Date
FROM Fixes, Metals
WHERE Metals.Id = Fixes.Metals_Id
Fields GBPam and GBPpm are both of type decimal(10,5)
Now I want columns AM and PM to be formatted to 3 decimal places in my sql query - Correct
I want values in the thousands to be formatted as xxxx.xxx and not x,xxx.xxx - Incorrect
Example output from mysql query:
Metal AM PM Date
Gold 1,081.334 NULL 11-09-12
Silver 21.009 NULL 10-09-12
Platinum 995.650 NULL 11-09-12
Palladium 416.700 NULL 11-09-12
Can you see that output for Gold AM is 1,081.334? How can I get it to output 1081.334?
This is a pain in the ass for me because I have to then muck about in PHP to remove the comma. I would prefer to just get mysql to format it correctly.
Just use ROUND, this is a numeric function. FORMAT is a string function
ROUND(Fixes.GBPam, 3)
you can use replace command for this purpose.
REPLACE(Fixes.GBPam,',','')
EDIT:
With respect to your question you could do something like this:
SELECT Metals.Metal, ROUND(REPLACE(Fixes.GBPam,',',''),3) AS AM,
ROUND(REPLACE(Fixes.GBPpm,',',''),3) AS PM,
DATE_FORMAT(Fixes.DateTime, '%d-%m-%y') AS Date
FROM Fixes, Metals
WHERE Metals.Id = Fixes.Metals_Id
Use replace function. Whether the field is integer or varchar, it will work.
select replace(Fixes.GBPam,',','.');
Related
As title, I'm trying to convert a VARCHAR column in a DATE column, and data is populated in that format "DDMMYYYY" ex. XMAS is "25122022" and in this case the correct formula should be STR_TO_DATE(column, '%d%m%Y')Well, when I execute this query I get an error since in some cases I have values with a "missing" char, I mean, for example, "1012023" when the day is <10 the query fails, cause it checks for "01122023" instead.I could solve this easily by adding a 0 to all fields having length 7, but I'd like to make it more clean.Reading better the usage of STR_TO_DATE I noticed that I could replace %d with %e since the second choice should theorically consider days from 0 to 31 instead of 01 to 31.Unexpectedly the query didn't work and gave me the same erorr at the first instance of a length 7 string.Am I doing something wrong?Thanks in advance.
We can try left padding your date string with zero to a length of 8:
WITH yourTable AS (
SELECT '1012023' AS dt
)
SELECT STR_TO_DATE(LPAD(dt, 8, '0'), '%d%m%Y') AS dt_out -- 2023-01-01
FROM yourTable;
Demo
In my database table, there is a date column i.e. EXPECTED DATE which is in dd-mm-yyyy format, and the datatype of the column is text. Now, I want to convert the format to yyyy-mm-dd. But the date is not changing at all and also when I tried to get the timestamp for the expected date column . I am getting some errors. For date coming I have used this STR_TO_DATE. But the year is not coming like what I expect and the timestamp also.
For example:
select STR_TO_DATE ('30-11-2011', '%d,%m,%y') as date ;
returns a result as
2020-11-30
And for timestamp
select STR_TO_DATE ('2011,11,30 12,30,45', '%y,%m,%d, %H,%I,%S');
I am not getting errors.
Please help me get the correct answers for this problem.
For the first query you need to use the %Y. Remember that it is always better to use "Y" for the years when you are writing a query for year.
SELECT STR_TO_DATE("30,11,2011", "%d,%m,%Y");
For the second one also, you can use '%Y' in the place of '%y'. For minutes, use '%i' not '%I'. For hours and minutes, you can use whatever you like.
SELECT STR_TO_DATE("2011,11,30 12,30,45", "%Y,%m,%d %h,%i,%s");
Refer to the below documentation for more clarification on SQL commands.
You need %Y (capital Y) for the 4 digit year, when using MySQL's STR_TO_DATE. Also, minutes is represented by %i, not %I, the latter which is hours on a 0 to 12 scale. So use:
SELECT STR_TO_DATE('30-11-2011', '%d-%m-%Y');
SELECT STR_TO_DATE('2011,11,30 12,30,45', '%Y,%m,%d %H,%i,%S');
For the first query you need to use the %Y'.
SELECT STR_TO_DATE("30,11,2011", "%d,%m,%Y");
For minutes, use this one only '%i'.
SELECT STR_TO_DATE("2011,11,30 12,30,45", "%Y,%m,%d %h,%i,%s");
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.
I have created MySQL table :
CREATE TABLE EMP(
EMPID INTEGER NOT NULL (5),
SURNAME VARCHAR(25),
SAL INTEGER(5),
JON VARCHAR(25),
START_DATE DATE,
END_DATE DATE,
DEPNO INTEGER(5)
);
with following records:
INSERT INTO EMP
(EMPID,SURNAME,SALARY,JOB,START_DATE,END_DATE,DEPNO)
VALUES
('1','Vorosila','500000','COO','20150101',null,'1');
however I need to change date format from 2015 01 01 to 01 01 2015
Can anybody show me or tell me how to do that ?
THANK YOU IN ADVANCE
DATE values do not have a "format", they are objects that represent instants in time (or entire days, but still independent of formatting).
Formats are applied on input and output, so you just need to apply the correct format, which you can find in the MySQL manual, to the SELECT statement.
You cannot change the default date format in mysql.
I once hoped for the default date to be editable so I wouldn't have to jump through these hoops to get the date I actually wanted, mysql even has a date format system variable, but it is unused. Date Format Mysql - link
What you should really do is store it as the default format Year-Month-Date and then convert it on select.
The first thing I'd suggest is having your date columns as date types, which would give your dates the following format '2015-01-01'.
If you do this then you can use DATE_FORMAT - link - the second value in the DATE_FORMAT function allows you to customise the returned date, and there are many different thing you can do with this if you look at the link:
SELECT
DATE_FORMAT(`START_DATE`,'%d-%m-%Y')
AS `START_DATE`
FROM ...
The other option you have is to store your dates in the format that you already want as a char or varchar column.
HOWEVER, as should be obvious, this column will not be treated as storing dates, and so will not give you the correct comparisons in a where clause when using > < BETWEEN or the correct ordering in an order by clause. It is after all just a string of numbers in this case.
However you can then use STR_TO_DATE - link if you did need to use a where or order by on this column to change it back to a date within the query - in this case the second value is the custom format of your 'dates' in the column. Keep in mind with a where you will need to compare it with the correct mysql format as shown below:
SELECT
`START_DATE`
FROM table
WHERE STR_TO_DATE(`START_DATE`,'%d-%m-%Y') BETWEEN '2015-01-01' and '2016-01-01'
In MySQL you can change the format of a date using DATE_FORMAT method which is similar to to_char in Oracle.
DATE_FORMAT(SYSDATE(), '%DD-%MM-%YYYY');
For more information about specifiers check this thread http://www.sqlines.com/oracle-to-mysql/to_char_datetime
You can do what you probably want by creating a view and referring to that instead of the (underlying) table.
CREATE VIEW emp_view AS
SELECT empid,
surname,
sal,
jon,
date_format(start_date, '%d-%m-%Y') as start_date,
date_format(end_date, '%d-%m-%Y') as end_date,
depno
FROM emp;
Note that this changes the type of the date columns to varchar, so comparisons will no longer work as expected:
SELECT * FROM emp_view WHERE start_date > '01-12-1924'; // fails!
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.