MySQL: converting date formats inside of strings - mysql

I have a list of file paths and dates stored in a database:
path | date
_____________________________|___________
C:\folder\file1 %Y-%m-%d.csv | 2016-09-14
C:\folder\file2_%M %d %Y.csv | 2016-09-13
C:\folder\file3 %y%m%d.csv | 2016-08-31
The dates in the file paths are according to the STR_TO_DATE format convention.
The dates will change everyday.
I need to write a SELECT query that will return:
result
_________________________________
C:\folder\file1 2016-09-14.csv
C:\folder\file2_Sep 14 2016.csv
C:\folder\file3 160831.csv
I don't want to end up writing a never-ending REPLACE query with all the possible scenarios:
REPLACE(... REPLACE(REPLACE(path,'%Y',YEAR(date)),'%d',DAY(date))...)
Is there a way to do this with a MySQL built-in function?

You want DATE_FORMAT() here. It should replace format strings it recognizes with their values and ignore everything else.
SELECT DATE_FORMAT(date, path) AS result;
NOTE: %M Will give the full month ("September"), for the abbreviated month ("Sept") use %b.
DEMO: http://sqlfiddle.com/#!9/77b6f7/1

Related

sql incorrect date output incorrect

i have a table like this
|id| date |name|
1 23/11/20 jake
2 01/07/20 jhon
3 23/05/20 blake
4 11/02/20 drake
5 1/03/14 crake
i ran a query like this
WHERE date >= '1/07/20' AND date <= '23/11/20'
i expected a result where i would get only the results between those dates
but i got some results which were from 2014
the data type for the date column is varchar
#note i can not change the datatype
how can i only get dates between the two ?
String-wise comparison is the problem: typically, '10/01/19' (Janurary 10th, 2019) is greater than '01/01/20' (January 1st, 2020), because the former starts with 1, and the later with 0.
You need to turn these strings to dates before you can compare them:
where str_to_date(date, '%d/%m/%y') between '2020-07-01' and '2020-66-23'
This is inefficient, because the entire column needs to be converted before the filtering can happen. I would warmly recommend fixing your data model, and store dates as dates.
Side note: your strings need to be consistently formatted as mm/dd/yy for this to work; if you have varying formats - or strings that do not map to valid dates - then you have a bigger problem than what you have asked here.

TO_DATE to YYYYMMDD from DD-MON-YY

I have some data in the following date format.
'27-SEP-97' i.e DD-MON-YY
Now I want to convert this to YYYYMMDD. I am using the following script to convert this.
TO_CHAR(TO_DATE(CHD_DATE_FIRST_ACT,'DD-MON-YY'),'YYYYMMDD')
but this is giving me the following output.
20970927
I want this data to be in YYYYMMDD format, such that the output looks like this- 19970927
If '27-SEP-97' is a string (which is what your words suggest), then such a combination of TO_this and TO_that might do the job:
SQL> with test as (select '27-SEP-97' datum from dual)
2 select to_char(to_date(datum, 'dd-mon-rr', 'nls_date_language = english'), 'yyyymmdd') result
3 from test;
RESULT
--------------------------------------------------------------------------------
19970927
SQL>
--USE RR instead of YY in your query.
select TO_CHAR(TO_DATE('27-SEP-97','DD-MON-RR'),'YYYYMMDD') from dual;
You can make use of mysql replace and str_to_date functions
replace takes 3 arguments as show below
replace(string,find_string,replace_with)
you should simply replace - with a blank string
if your str is '27-SEP-97' to get the output as 19970927 execute the following in mysql
select replace(str_to_date('27-SEP-97','%d-%b-%Y'),'-','') as date;
output
+----------+
| date |
+----------+
| 19970927 |
+----------+
%b is used to mention abbreviated month name
click the below links to know more about mysql date and time functions
Mysql Data and Time functions

Format the date and time in MySQL default format

I have a MySQL table with forex tick data
pair tick_date_time price_low price_high
------- --------------------- --------- ------------
USD/JPY 20091201 00:00:00.628 86.280000 86.286000
USD/JPY 20091201 00:00:00.722 86.280000 86.289000
USD/JPY 20091201 00:00:00.741 86.281000 86.289000
USD/JPY 20091201 00:00:01.130 86.283000 86.289000
USD/JPY 20091201 00:00:01.131 86.283000 86.289000
The date/time in this table is not formatted in MySQL default format. I use the query to format the data
SELECT DATE_FORMAT(t.tick_date_time, "%Y%m%d %H:%i:%s") FROM table AS t
This returns NULL. Any idea where I'm wrong? I also like to extract milliseconds if possible.
You are looking for the function str_to_date(), not date_format():
SELECT str_to_date(t.tick_date_time, '%Y%m%d %H:%i:%s')
date_format() takes a date and produces a string. str_to_date() takes a string and converts it to a date.

reformat date into a numeric MDY value in MySQL

I have a column in a table that has dates, as such:
2010-01-15 00:00:00
2002-10-24 09:00:00
2015-04-29 10:00:00
and now I need to generate a new column as such (month, date, year):
01152010
10242002
04292015
I can't find an easy way to do this. All the solutions I've looked at are either for changing date type or getting seconds since epoch!
I've also tried all sorts of substring operations
You could try using the MySQL DateFormat Function.
The documentation is pretty a good resource for this sort of thing.
SELECT DATE_FORMAT(dateColumn, '%m%d%Y') As FormattedDate FROM <table>;
Here is an example I made using SQLFiddle.

mySQL format number output , thousands separator

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,',','.');