To change the date format inside a mysql datatable only for matching the term passing to it there are DATE_FORMAT option available in mysql documentation.
one of the popular function is use here is DATE_FORMAT. An example shown below will explain the proper use of this function inside the mysql datatable
suppose if you have table
here in this table you can see that there are different formats of date mention in the date_added column. so if we want to match here the time according to a specific time format so how do we do it.
like if we want a row to be selected with a date format (20-01-2013). so for doing this we only need to pass this query
SELECT * FROM `table` WHERE DATE_FORMAT(date_added, '%Y-%m-%d')='matching_date';
if the above query will not work so you can try STR_TO_DATE also. this function make a string to a date format
SELECT * FROM `table` WHERE DATE_FORMAT(STR_TO_DATE(date_added,'%d-%b-%Y %l:%i %p'), '%Y-%m-%d')='matching_date'
in the above query we convert the saved date_added column into a date format with STR_TO_DATE function.
for some of the more useful date function you can refer the following table also
the above reference with the source of http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
posted by honeyonsys
one of the popular function is use here is DATE_FORMAT. An example shown below will explain the proper use of this function inside the mysql datatable
suppose if you have table
id | name | occupation | date_added | city |
---|---|---|---|---|
1 | harish | web developer | 13 feb 2013, 12:56 PM | New Delhi |
2 | john | web developer | 16 feb 2013, 12:14 AM | New Jersey |
3 | Venus | web designer | 15-1-2013, 11:09 AM | New Delhi |
4 | Billy | animator | febuary 19 2013, 16:08 PM | New York |
here in this table you can see that there are different formats of date mention in the date_added column. so if we want to match here the time according to a specific time format so how do we do it.
like if we want a row to be selected with a date format (20-01-2013). so for doing this we only need to pass this query
SELECT * FROM `table` WHERE DATE_FORMAT(date_added, '%Y-%m-%d')='matching_date';
if the above query will not work so you can try STR_TO_DATE also. this function make a string to a date format
SELECT * FROM `table` WHERE DATE_FORMAT(STR_TO_DATE(date_added,'%d-%b-%Y %l:%i %p'), '%Y-%m-%d')='matching_date'
in the above query we convert the saved date_added column into a date format with STR_TO_DATE function.
for some of the more useful date function you can refer the following table also
Name | Description |
---|---|
ADDDATE() | Add time values (intervals) to a date value |
ADDTIME() | Add time |
CONVERT_TZ() | Convert from one timezone to another |
CURDATE() | Return the current date |
CURRENT_DATE() ,CURRENT_DATE | Synonyms for CURDATE() |
CURRENT_TIME() ,CURRENT_TIME | Synonyms for CURTIME() |
CURRENT_TIMESTAMP() ,CURRENT_TIMESTAMP | Synonyms for NOW() |
CURTIME() | Return the current time |
DATE_ADD() | Add time values (intervals) to a date value |
DATE_FORMAT() | Format date as specified |
DATE_SUB() | Subtract a time value (interval) from a date |
DATE() | Extract the date part of a date or datetime expression |
DATEDIFF() | Subtract two dates |
DAY() | Synonym for DAYOFMONTH() |
DAYNAME() | Return the name of the weekday |
DAYOFMONTH() | Return the day of the month (0-31) |
DAYOFWEEK() | Return the weekday index of the argument |
DAYOFYEAR() | Return the day of the year (1-366) |
EXTRACT() | Extract part of a date |
FROM_DAYS() | Convert a day number to a date |
FROM_UNIXTIME() | Format UNIX timestamp as a date |
GET_FORMAT() | Return a date format string |
HOUR() | Extract the hour |
LAST_DAY | Return the last day of the month for the argument |
LOCALTIME() , LOCALTIME | Synonym for NOW() |
LOCALTIMESTAMP ,LOCALTIMESTAMP() | Synonym for NOW() |
MAKEDATE() | Create a date from the year and day of year |
MAKETIME | MAKETIME() |
MICROSECOND() | Return the microseconds from argument |
MINUTE() | Return the minute from the argument |
MONTH() | Return the month from the date passed |
MONTHNAME() | Return the name of the month |
NOW() | Return the current date and time |
PERIOD_ADD() | Add a period to a year-month |
PERIOD_DIFF() | Return the number of months between periods |
QUARTER() | Return the quarter from a date argument |
SEC_TO_TIME() | Converts seconds to 'HH:MM:SS' format |
SECOND() | Return the second (0-59) |
STR_TO_DATE() | Convert a string to a date |
SUBDATE() | A synonym for DATE_SUB() when invoked with three arguments |
SUBTIME() | Subtract times |
SYSDATE() | Return the time at which the function executes |
TIME_FORMAT() | Format as time |
TIME_TO_SEC() | Return the argument converted to seconds |
TIME() | Extract the time portion of the expression passed |
TIMEDIFF() | Subtract time |
TIMESTAMP() | With a single argument, this function returns the date or datetime expression; with two arguments, the sum of the arguments |
TIMESTAMPADD() | Add an interval to a datetime expression |
TIMESTAMPDIFF() | Subtract an interval from a datetime expression |
TO_DAYS() | Return the date argument converted to days |
UNIX_TIMESTAMP() | Return a UNIX timestamp |
UTC_DATE() | Return the current UTC date |
UTC_TIME() | Return the current UTC time |
UTC_TIMESTAMP() | Return the current UTC date and time |
WEEK() | Return the week number |
WEEKDAY() | Return the weekday index |
WEEKOFYEAR() | Return the calendar week of the date (0-53) |
YEAR() | Return the year |
YEARWEEK() | Return the year and week |
Formats the
date
value according to the format
string.
The following specifiers may be used in the
format
string. The “%
” character is required before format specifier characters.Specifier | Description |
---|---|
%a | Abbreviated weekday name (Sun ..Sat ) |
%b | Abbreviated month name (Jan ..Dec ) |
%c | Month, numeric (0 ..12 ) |
%D | Day of the month with English suffix (0th , 1st , 2nd , 3rd , …) |
%d | Day of the month, numeric (00 ..31 ) |
%e | Day of the month, numeric (0 ..31 ) |
%f | Microseconds (000000 ..999999 ) |
%H | Hour (00 ..23 ) |
%h | Hour (01 ..12 ) |
%I | Hour (01 ..12 ) |
%i | Minutes, numeric (00 ..59 ) |
%j | Day of year (001 ..366 ) |
%k | Hour (0 ..23 ) |
%l | Hour (1 ..12 ) |
%M | Month name (January ..December ) |
%m | Month, numeric (00 ..12 ) |
%p | AM or PM |
%r | Time, 12-hour (hh:mm:ss followed by AM or PM ) |
%S | Seconds (00 ..59 ) |
%s | Seconds (00 ..59 ) |
%T | Time, 24-hour (hh:mm:ss ) |
%U | Week (00 ..53 ), where Sunday is the first day of the week |
%u | Week (00 ..53 ), where Monday is the first day of the week |
%V | Week (01 ..53 ), where Sunday is the first day of the week; used with %X |
%v | Week (01 ..53 ), where Monday is the first day of the week; used with %x |
%W | Weekday name (Sunday ..Saturday ) |
%w | Day of the week (0 =Sunday..6 =Saturday) |
%X | Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V |
%x | Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v |
%Y | Year, numeric, four digits |
%y | Year, numeric (two digits) |
%% | A literal “% ” character |
% | x , for any “x ” not listed above |
the above reference with the source of http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
posted by honeyonsys
No comments:
Post a Comment