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.
Related
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");
During a database import, I kinda messed things up. So now I have a date column that is of DATE type, but the year and day are in the wrong places: the day ended up appearing in the year column and MySQL has auto formatted it as year further worsening things.
I have values in a table like below:
dates(dddd-mm-yy)
2030-05-12
2021-06-13
2012-12-13
I want them like so:
dates(yyyy-mm-dd)
2012-05-30
2013-06-21
2013-12-12
Please note that I actually want to UPDATE the column, I have seen and tried many examples on SELECT.
Thanks in advance.
You can use substring() and concat() to concat whatever you want. Below is the statement for your reference(table is tb_test, the column name is col_date).
update tb_test set col_date = concat('20',substring(col_date,7,2),'-',substring(col_date,4,2),'-',substring(col_date,1,2));
I dont know if this can be classified a "solution", maybe more of a work around, but it worked flawless for me.
After reading this, i realised that my problem started when i tried to import my dates in the format dd-mm-yy, Hence Mysql assumed my day to be year and presented my dates as yydd-mm-yy
My dates were formatted as yydd-mm-yy and i have severally tried the query
UPDATE table_name
SET DATES = DATE_FORMAT(DATES, '%Y-%m-%d');
This query ran but made no changes to the Table because in the eyes of Mysql it has helped me format my dates as yyyy-mm-dd right from when i imported my DB, but still my dates are in yydd-mm-dd
So i thought, what if i try a query like;
UPDATE table_name
SET DATES = DATE_FORMAT(DATES, '%d-%m-%y');
This way Mysql switched the days and years then provided my dates as yyyy-mm-dd, quite a confusing thing the way Mysql handles dates but the end result of a date like 2030-05-12 is 2012-05-30.
I want to store month like JAN,FEB and soon in database. What datatype should I use? because if I use varchar, when I display it, it won't sort properly. Please advise.
You can use TINYINT is as the month and have another table with columns month_id, Month_name
where you place
1 JAN
2 FEB
etc...
the sorting etc will work fine. when you want to display, have a simple inner join.
Just store it as a complete date (perhaps always using the first as the day) and use the database functions MONTH() if you only need part of it. This makes using that field much easier as you can still do range queries, etc.
Store months number in database.
i.e. For JAN store 01, FEB store 02 and so on..
Take Datatype TINY INT for your field.
OR another solution you can try:
Take datatype DATE for your field.
Use this type of query: SELECT DATE_FORMAT('2014-03-27','%b');
DATE_FORMAT_MYSQL
I want to show date column in DESC order where date is entered as VARCHAR and is in order 20-JUN-2007 I have already used ORDER BY RIGHT(vPublishedDate, 4) but it doesn't effect the month and date
Here is one way to do it using STR_TO_DATE (take into account the other answers about converting the column to date, although you may not have control over the database):
SELECT ...
FROM ...
ORDER BY STR_TO_DATE(vPublishedDate,'%d-%M-%Y')
As an example:
SELECT STR_TO_DATE('20-JUN-2007','%d-%M-%Y') as Date;
+------------+
| Date |
+------------+
| 2007-06-20 |
+------------+
Why are you using a VARCHAR to store a DATE? Use a DATE to store a DATE and then, as if by magic, sorting works all on its own.
You really should be storing dates as dates, not character-type fields. Then you wouldn't need to worry about this sort of "SQL gymnastics" (as I like to call it).
Databases are for storing data, not formatting.
By forcing yourself to manipulate sub-columns, you basically prevent the database from performing any useful optimisations.
In order to do what you want with the data you have you have to do something like:
use substring to extract individual sub-column information to get them in the order you want; and
use some sort of lookup to turn a string like "NOV" into 11 (since the month names will sort as DEC, FEB, AUG, APR, JAN, JUL, JUN, MAR, MAY, NOV, OCT, SEP).
And this would be a serious performance killer. Now there may be a function which can turn that particular date format into a proper date but I urge you: don't use it.
Set up or change your database to use an intelligent schema and all these problems will magically disappear.
It's a lot easier to turn a date column into any sort of output format than to do the same with a character column.
Change that VARCHARto a Date type column, if you can.
You can also try this, although this is NOT the RIGHT approach.
Select STR_TO_DATE(your_date_column,'%d/%m/%Y') AS your_new_date from your_table order by your_new_date DESC
Try converting the varchar to date using str_to_date and then you can apply the sorting logic.
I would suggest you to change the type as Date.
Then run a script which converts your dates to the correct DB format.
Sorting would be then be just as simple as sorting ids in MySql
I want to order by date.
e.g.
table_date
February 2011
January 2011
December 2010
I've already tried:
SELECT distinct(table_date) FROM tables ORDER BY table_date DESC
bur it doesn't work.
I get this instead:
January 2011
February 2011
December 2010
Can you help me please?
If you must store the dates in a varchar which as others pointed out is not recommended, you could use:
SELECT table_date FROM tables ORDER BY STR_TO_DATE(table_date, '%M %Y') DESC;
If you want to order by date, store it as a date, not a string. Unless your date string is of the form yyyy-mm-dd, it will not sort as you want it.
Databases are hard enough work as-is, without people making it harder, and you should be striving as much as possible to avoid what I like to call SQL gymnastics.
Store it as a date then, if you must, use date functions to get it in the form February 2011.
It'll be a lot easier going that way than what you're trying to do.
Even if you can't change any of the current columns due to code restrictions, you can always add another column to the database like TABLE_DATE_AS_DATE and put in an insert/update trigger to populate it based on TABLE-DATE.
Then just do:
update table x set table_date = table_date
or something similar, to fire the trigger for all rows.
Then, your query can still get at table_date but use table_date_as_date for ordering. That's a kludge of course but I've had to use tricks like that in the past when it was imperative the code could not change, so we had to resort to DBMS trickery.
Store dates as DATE, not as VARCHAR, that's a huge mistake. Use STR_TO_DATE() to convert your content. When you're done, you can order by dates without any problems.
Date should be stored as date and not VARCHAR.
Suppose you have table_date in the following format (DD-MM-YYYY)
table_date
2011-01-01
2011-02-01
2010-12-01
Now you can perform order by clause in the following way
SELECT * FROM table_order ORDER BY str_to_date(date, "%Y-%M-%D") ASC
I doubt if the output will be in ordered form