I want to change the data format in the mysql database like in this format 'April 11, 1979' this is what i got from the facebook API while retrieving from the facebook
So how to set the date format (April 11, 1979) while creating the mysql table.
You can't change the way that mysql store dates, instead you can change the date format returned from facebook and then store it in MYSQL, you can do something like this:
$fb_date = strtotime($date_returned_from_facebook);
$insert_in_my_sql = date('Y-m-d',$fb_date);
read more about PHP date: http://php.net/manual/en/function.date.php
If you just want to accept something like the specified string as a date imput format then this might help (see here: Parse date in MySQL):
STR_TO_DATE('April 11, 1979', '%M %e, %Y') AS date
With date formats explained here: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format .
You can use STR_TO_DATE function for this.
INSERT INTO test
VALUES (1, STR_TO_DATE('April 11, 1979', '%M %d,%Y') ) ;
Fiddle - http://www.sqlfiddle.com/#!2/db30d/1
Related
I have a dateformat but to display on front end I would like to display the data like so.
SELECT STR_TO_DATE('5,2013','%m,%Y');
The result I would like to generate is 'May 2013'.
Why are you storing dates as string values? Mysql has dedicated data types for date and time values: https://dev.mysql.com/doc/refman/5.7/en/date-and-time-types.html
When using date, you can easily use DATE_FORMAT and set %m,%Y as formatting (second argument).
SELECT replace(date_format(str_to_date('5,2013','%m,%Y'),'%M-%Y'),'-',' ');
As to the format just read the docs: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format. You need an uppercase M for the month name. And use DATE_FORMAT to get from a date to a string.
SELECT DATE_FORMAT(DATE '2013-05-01', '%M %Y');
Assuming that you have 5,2013 stored in your database, you need to use DATE_FORMAT after parsing the string:
SELECT DATE_FORMAT(STR_TO_DATE('5,2013','%m,%Y'), '%b %Y')
Why? Because it seems you don't have a date type stored in the database. So you parse it to date using STR_TO_DATE. This give you the default date format. DATE_FORMAT let you apply other formattings on it.
All avaliable formatting values are documented here: https://www.w3schools.com/sql/func_mysql_str_to_date.asp
In general, I would recommend to think about storing date objects instead of custom date strings like 5,2013. This avoids those castings, because you could directly use DATE_FORMAT. It also has benefits on filtering/ordering and performance. Using date types, you could easily sort by it or filter e.g. everything in month X.
I don't say its not possible with custom strings like 5,2013 but would be more complex and could result in bad performance if the db grows.
You can use the functions:
str_to_date() to convert the string (after concatenating the prefix '1' to your string) to a valid date and then
date_format() to reformat the date:
SELECT date_format(str_to_date(concat('1,', ?),'%d,%m,%Y'), '%b %Y');
Replace ? with your string.
See the demo.
Result:
May 2013
I want to get time from mysql dd/mm/YYYY H:M:S format.
I have tried,
SUBSTRING_INDEX(field, 'delimiter', index)
but am looking for a better solution.
have tried, DATE_FORMAT(field, "%H:%i:%s") but it returns NULL because my date format was not native (YYYY-mm-dd)
it was 02/05/2019 19:38:27
How to get time from this above format in a better way?
NOTE: I am storing date like above.. this fetching form SQL Server
I guess you can first use STR_TO_DATE followed by CAST(... AS time). Casting instead of formatting allows you to use the result in date/time calculations.
SELECT CAST(STR_TO_DATE('02/05/2019 19:38:27', "%d/%m/%Y %H:%i:%s") AS TIME)
Ideally you should teach SQL Server to export dates in yyyy-MM-dd hh:mm:ss format.
This is how i Resolved,
TIME(STR_TO_DATE(d.in_punch, "%d/%m/%Y %H:%i:%s"))
also as per #Salman A
CAST(STR_TO_DATE('02/05/2019 19:38:27', "%d/%m/%Y %H:%i:%s") AS TIME)
this also worked.
I have date format like 20151030
I want to change it 30/10/2015
12:23:43 PM . Can anyone please advise on this.
You can use the following query
select CONVERT(varchar(25), CONVERT(datetime, CAST(20151030 AS CHAR(10)), 113), 103)
The output would be 30/10/2015
You can also visit This URL For a list of date formatting specifications.
This SO Post discusses on the similar lines about date formatting.
I have a webpage that is used to access a database and look up when appointments are set. It's been setup and used for a couple months but now we want to change the column that stores the 'appointment date' from 'varchar' to 'datetime'. The problem is that the varchar date is put into the database as 'Friday, June 10, 2015' for example. I've tried to use 'CONVERT', 'PARSE', 'DECLARE', and 'SELECT PARSE'. I always get a 'incorrect syntax' error. Is this possible? And if so what is the correct syntax?
Use the STR_TO_DATE function like so:
STR_TO_DATE('Friday, June 10, 2015', '%W, %M %e, %Y')
The above example assumes a day format that has NO leading zeros (e.g. '1' and not '01'). For more info on the format specifiers, see here
EDIT: Having re-read your question, I'm not totally clear on how you want to use this conversion... If you're trying to convert the way that the column is stored in your database, then I would suggest that you create a new DATE type column in the table and run an UPDATE to set the the value for each row like so:
UPDATE table SET newDateCol = STR_TO_DATE(oldVarcharCol, '%W, %M %e, %Y');
Then you can drop the old column and rename the new one to whatever you like. Keep in mind that this may impact other parts of your system that are relying on that field being a VARCHAR.
Hope this helps!
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';