I have two columns (Date and Time) in database that I would like to get from SQL query with specific date format. It can be two values from sql query or one. Here are the details.
Display Format:
Sunday 03/31/2013 12:13:27 AM
Database Columns:
EndDate (Date yyyy-mm-dd)
EndTime (Time hh:mm:ss)
This is what you need:
DATE_FORMAT(CONCAT(EndDate, EndTime), '%W %m/%d/%Y %r')
The date_format() function gives you everything you need.
Example. Let's say you have columns aDate and aTime in your table, and you need to show the date like 'dd-mm-yyyy' and the time like 'hh:mm'. So:
select
date_format(aDate, '%d-$m-%Y'), date_format(aTime, '%H:%i')
from myTable;
Check the reference manual (Chapter 12 - Functions and operators for MySQL 5.5)
If you need to concatenate the results, then:
select
concat(date_format(aDate, '%d-$m-%Y'), ' ', date_format(aTime, '%H:%i'))
from myTable;
This seems to work for me:
select date_format(concat(enddate,' ',endtime),'%W %m/%d/%Y %r')
from yourtable
SQL Fiddle Demo
Good documentation for date_format: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
Related
I have next problem:
My table date format was: LIKE 2017-01-08 18:50:25 (with time).
When i use sql query like
'SELECT date FROM table WHERE date = "2017-01-08"'
My row was empty, i need COUNT all row with same (today) date WITHOUT TIME.
Note, i will not change INSERT date time!
Use DATE() to get the date portion of the datetime field and compare it to today. Use COUNT() to get the number of records that match your query.
SELECT count(*) FROM table WHERE DATE(date) = CURDATE()
You can also replace CURDATE() with NOW(), CURRENT_DATE(), and CURRENT_DATE
You can also use it in the following way
'SELECT date FROM table WHERE date_format(date,'%Y-%m-%d') = "2017-01-08"'
the date_format is mysql function which return date according to your pattern the above pattern only return the Y-m-d from the datetime
I hope it will help you
plz change your statement equal operator to greater than
'SELECT date FROM table WHERE date > "2017-01-08"'
as by default if time portion is not present then it is putting 00:00...
In mySQL I have a timestamp column named when
2015-01-07 16:43:21
My question is how using PHP/mySQL
For now I can show the results based on month number like
... where month(`when`) = '1' ...
but what if I want to show the rows of a particular date for example 2015-01-05 ?
I will pass the preferable date through a variable into the sql query.
Just wrap the date() function around your date string which is already in the YYYY-MM-DD format, and also around your timestamp field in the where clause:
select * from table where date(`when`) = date('2015-01-05');
From the documentation for date():
Extracts the date part of the date or datetime expression expr.
Here's how to convert the date via MySQL:
SELECT
DATE_FORMAT(`when`, '%Y-%m-%d') AS my_date
FROM
my_table
WHERE
MONTH(`when`) = 1;
Take a look at the MySQL documentation here for other formats: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
I have a table column named date_created where I store datetime values in VARCHAR e.g. 16-06-2013 10:49:29
I want to get results through mysql query between tow dates. I am using query like this:
... WHERE date_created BETWEEN '06-08-2013 22:30:18' AND '28-08-2013 22:30:22' ...
This query return results but that result also includes older date records that are between 06 and 28 of every month. I also use < and > but these also did not work.
How can I get results that only include records between '06-08-2013 22:30:18' AND '28-08-2013 22:30:22'
You can use MySQL-function STR_TO_DATE to format strings into DATETIME.
SELECT STR_TO_DATE('06-08-2013 22:30:18', '%d-%m-%Y %H:%i:%s');
# 2013-08-06 22:30:18
I would recommend to reformat all VARCHARs to DATETIMEs with a single UPDATE and fix the code.
you need to use the STR_TO_DATE function to convert your date string into a date that can be used for comparisons.
STR_TO_DATE(date_created, '%d-%m-%Y %H:%i:%s') BETWEEN STR_TO_DATE('06-08-2013 22:30:18', '%d-%m-%Y %H:%i:%s') AND STR_TO_DATE('28-08-2013 22:30:22', '%d-%m-%Y %H:%i:%s')
In my sql query I output dates in chronological order.
The dates in my database are stored in d-M-Y format.
What I want to do is sort the results by dates equal to or greater than today to be output first.
In my query I have this sort in my query
...From $db ORDER BY STR_TO_DATE(sortdate, '%d-%M-%Y') ASC
Can anyone tell me if I can do a comparison on todays date as each record is output from the db?
This will give me todays date
$todaysdate = date("d-M-Y", time());
but can anyone tell me if I can build that into my query?
Thanks in advance.
check mysql DATEDIFF in combination with CURRENT_DATE ==>
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_current-date
My guess is that you saved the date in a VARCHAR column. Please don't do that, you make it very complicated for yourself when you want to do stuff (like this) with the date. I'd suggest that you convert the column to a DATE field and then just use:
SELECT * FROM my_table WHERE my_date_field >= CURDATE()
And if you want to output the date in the d-m-Y format, you can use DATE_FORMAT()
You really should be storing the dates in a dateTime format. That will make it much easier to do all sorts of orders, comparisons and plenty of other things. You could for example, then use the mysql now() function to only get the results you need?
...From $db where sortDate>=now() ORDER BYsortdate ASC
Assuming sortdate is datetime field, in order to display dates equal to or greater than today first,could use UNION.
SELECT * FROM my_table WHERE sortdate>= CURDATE()
UNION
SELECT * FROM my_table WHERE sortdate< CURDATE()
You can use WHERE sortdate >= $todaysdate
Just put this condition in where like date_column >= curdate()/$todaysdate
thanks
Is it possible to fetch only the month part from a sql date entry?
like the date is stored like '2011-01-06'. if i want to fetch only the '01' portion how can I do that?
thanxx in advance.
You can use DATE_FORMAT to do it, like this:
SELECT DATE_FORMAT(DateField, '%m') FROM table
you can do it in mysql
like :
select DATE(date_field) from mytable
from manual :
MONTH(date)
Returns the month for date, in the range 1 to 12 for January to December, or 0 for dates such as '0000-00-00' or '2008-00-00' that have a zero month part.
mysql> SELECT MONTH('2008-02-03');
-> 2
in a mysql query, you can just do this:
MONTH(date_field)
mysql> SELECT MONTH('2008-02-03');
Result: 2
You can use the MONTH function of MySQL:
SELECT MONTH('2011-01-06');
Result:
1
To get month name instead, you can use MONTHNAME like this:
SELECT MONTHNAME('2011-01-06');
Result:
January
You may want to try
select substring(`mydate_field`,6,2) from `table_name`;
In ANSI-SQL you can use EXTRACT:
SELECT
EXTRACT('month' FROM fieldname)
FROM
tablename;
This works in many databases, MySQL as well.