This question already has answers here:
How to convert a string to date in MySQL?
(5 answers)
Closed 5 years ago.
I have a column(string (8)) that contains data like 12032008 and I have to display it as 12/03/2008. What should I do in retrieve query?
In mysql first convert it to date object using STR_TO_DATE then to format use DATE_FORMAT
SELECT DATE_FORMAT(STR_TO_DATE('12032008','%d%m%Y'),'%d/%m/%Y')
But ideally you should not save the date or date time as strings in table it will make things complex for mysql and for you also
Demo
The most appropriate thing for you to do here would be to stop storing date information in text columns. Instead, put 12032008 into a bona fide datetime column. This will make your life easiest in the long run. If you can't do that in the short term, here are two options for you to consider.
The short way, just using base string functions:
SELECT
CONCAT(LEFT(col, 2), '/', SUBSTRING(col, 3, 2), '/', RIGHT(col, 4)) AS output
FROM yourTable;
The longer way, and probably the better long term solution, would be to first convert your date string into a date, then convert it back to a string using the formatting you want:
SELECT
DATE_FORMAT(STR_TO_DATE(col, '%d%m%Y'), '%d/%m/%Y') AS output
FROM yourTable;
Demo
Related
I'm storing some data in a field called published. I need to use a regular expression to match the format of Y-m-d. Y and d can be excluded, however i need to any value in array of 3 months like $months = ['01,'02','03]. i need to match any of the 3 months in my regular expression this is my regex so far:
$regex = '^([0-9]{4}\-'.$months.'\-[0-9]{2})$';
I'm unsure on what the syntax would be for the months. would it be like 01|02|03? i'm not very good with regular expressions yet.
Updated
I found the following regex which will match:
^([0-9]{4}\-{1}(05|01|03){1}\-{1}[0-9]{2})$ not sure if its compatible with mysql but i'm going to try and see what happens. http://regexr.com/3c440
Why don't you just use:
WHERE MONTH(`datefield`) IN(1, 2, 3)
It's much easier to work with datetime fields, as it's easier to lookup and parse the format into a readable format MySQL can use.
Searching ranges becomes easy:
WHERE `datefield` >= '2015-01-01' AND `datefield` < '2015-04-01'
The full list of functions can be found here, and you can use functions such as STR_TO_DATE to convert to a dateformat.
#FrankerZ answer works, however for our purposes the field can store any type of text, not just a Y-m-d formatted date. This query is intended to target only records with a pattern that matches Y-m-d format and all others are simply not cared about for this exercise.
I was able to accomplish it myself with:
SELECT * FROM test WHERE published REGEXP '^([0-9]{4}\-{1}(05|01|03){1}\-{1}[0-9]{2})$'
http://sqlfiddle.com/#!2/7e311/1
I need to convert dates of varying strings. They come in 3 different ways.
yyyy/mm/dd
mm/dd/yyyy
or blank (can fill in some default)
What is a good way to handle this situation for an INSERT statement?
Use STR_TO_DATE function in combination with COALESCE - something like:
set #strdate := '2014/05/10';
select COALESCE(STR_TO_DATE(#strdate,'%m/%d/%Y'),STR_TO_DATE(#strdate,"%Y/%m/%d"))
You can use MySQL str_to_date() function. It requires one format and will return NULL if the data doesnt match the format. UseCoalesce with str_to_date with str_to_date in descending order of likelihood of the format.
Example if yyyy/mm/dd is more common than mm/dd/yyyy then use
COALESCE(STR_TO_DATE(your_date_here,'%Y/%m/%d'),STR_TO_DATE(#strdate,"%m/%d/%Y"))
Edit: This is the same as Ondřej Šotek's answer but with a possible performance improvement
Here's the thing - I'm saving date in database as string in format dd/mm/yyyy. I want to get rows in which date is between two dates - let's say 11/07/2009 and 29/08/2014, how to do that?
I tried
SELECT * FROM attr WHERE time_added between '11/7/2009' AND '29/8/2014'
but it's not working correctly. Any great would be great?
First of all, it is recommended to use the MySQL's DATE type for dates, selecting the date range would be easy and efficient. But if you have your own reason to use string type (like you are working with a specific calender and you don't have the converter), then you should consider followings:
you told that you are using the dd/mm/yyyy format for dates but in your code you wrote 11/7/2009 which should be 11/07/2009
In order to select range you should save your date like yyyy/mm/dd, specially when you put index on this filed, it will be high performance.
You need not to save format charterers like '/' in database. you can format the output later and show the date in any order and format you want.
As the result I offer you the following solution:
Use the YYYYMMDD format to save the date. the select query will be something like:
SELECT *, DATE_FORMAT(time_added, '%d/%m/%Y') AS time_added2 FROM attr
WHERE time_added between '20090711' AND '20140829';
As and alternative if you can not change the database, then the following query will work on the existing database (date saved in dd/mm/yyyy format):
SELECT * FROM attr WHERE
CONCAT(SUBSTR(time_added, 7, 4), SUBSTR(time_added, 4, 2), SUBSTR(time_added, 1, 2))
BETWEEN '20090711' AND '20140829';
This question already has answers here:
Natural Sort in MySQL
(22 answers)
Closed 8 years ago.
I write ...
ORDER BY column ASC
but my column is VARCHAR and it sorts wrong like
I want to sorting numeric value but its datatype is varchar then how
value like this
1.2.840.113619.2.55.3.163578213.42.1355218116.691.1
1.2.840.113619.2.55.3.163578213.42.1355218116.691.10
1.2.840.113619.2.55.3.163578213.42.1355218116.691.100
1.2.840.113619.2.55.3.163578213.42.1355218116.691.101
1.2.840.113619.2.55.3.163578213.42.1355218116.691.2
1.2.840.113619.2.55.3.163578213.42.1355218116.691.20
but i want to in sequence last
1.2.840.113619.2.55.3.163578213.42.1355218116.691.1
1.2.840.113619.2.55.3.163578213.42.1355218116.691.2
1.2.840.113619.2.55.3.163578213.42.1355218116.691.10
1.2.840.113619.2.55.3.163578213.42.1355218116.691.20
1.2.840.113619.2.55.3.163578213.42.1355218116.691.100
1.2.840.113619.2.55.3.163578213.42.1355218116.691.101
and also I have string like this
1.2.840.114257.0.10325113632288210457800001002296133400001
1.2.840.114257.0.10379710976288210457800000002296491200000
1.2.840.114257.0.10328923264288210457800000002296158400001
I also want to sort this ...
For your specific example, you can do:
ORDER BY length(col), col
If you have other examples, you might need to "parse" the values using substring_index().
I have tried the following and it is working fine.
First Remove the "." From the String so you have only numeric and then sort by using ABC() Function so, It will not truncated.
SELECT yourcol AS v FROM test ORDER BY ABS(REPLACE(yourcol, '.', '')), yourcol;
I think you should redesign your schema,
If you have only digits in ur input than go for #Gordon Linoff answer
Or may your input also contain character string(eg: ankit123) and you want sort in order to that char and number also than you may go for this
You have to implementing any Algorithm like this
Lets take eg
1 >1.2.1
2 >1.2.10
3 >1.2.2
Now make all digits equal, Decide any max length(eg: 5) so attach 0 ahead to make all digits with same length
now above string look like
1 >00001.00002.00001
2 >00001.00002.00010
3 >00001.00002.00002
Now if you you fire ORDER BY on this string you get your output in sorted order.
I am fetching the current date & time using NOW() in mysql. I want to convert the date value into a varchar and concat it with another string. How do I do it?
Use DATE_FORMAT()
SELECT
DATE_FORMAT(NOW(), '%d %m %Y') AS your_date;
This is super old, but I figured I'd add my 2c. DATE_FORMAT does indeed return a string, but I was looking for the CAST function, in the situation that I already had a datetime string in the database and needed to pattern match against it:
http://dev.mysql.com/doc/refman/5.0/en/cast-functions.html
In this case, you'd use:
CAST(date_value AS char)
This answers a slightly different question, but the question title seems ambiguous enough that this might help someone searching.
Try this:
concat(left(datefield,10),left(timefield,8))
10 char on date field based on full date yyyy-MM-dd.
8 char on time field based on full time hh:mm:ss.
It depends on the format you want it. normally you can use script above and you can concat another field or string as you want it.
Because actually date and time field tread as string if you read it. But of course you will got error while update or insert it.