Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a column which stores datetime like 2011-01-01 01:01:01 .
I need to get list of date:
2011-01-01
2011-02-01
Is there any way I can list down date from datetime in a table?
If the column is really a datetime, just use the date() function:
select date(column)
from table t;
Actually, this also works if the column is a string, assuming it is in YYYY-MM-DD format.
If you want a unique list of dates:
select distinct date(column)
from table t;
select substr(field,1,10)
For just a list of the dates.
SELECT DATE_FORMAT(field, '%Y-%m-%d')
SQL
select to_char(date_column,'YYYY-MM-DD') from table;
MySql
SELECT STR_TO_DATE(date_column,'%Y-%m-%d') from table;
You can use DATE_FORMAT().
Syntax:
SELECT DATE_FORMAT('2011-10-10 19:46:00', '%M %d, %Y');
Try this code
SELECT CONVERT(date,Column_Name) As Date FROM Table_Name
thank you for all answers.
what i really need is year. sorry for the misleading question.
the correct query for that is:
select distinct YEAR(created_on) as years from contacts
thank you
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I want to get lastupdate datetime order by updateTime but prevent for redundant date in my output
desired result
2021-06-25 15:46:57
2021-06-26 15:48:52
2021-06-27 17:11:52
2021-06-28 17:17:33
2021-06-29 15:16:29
I tried this
SELECT t.updateHistoryID, t.updateTime
FROM web_historyupdate t
INNER JOIN ( SELECT updateHistoryID, max(updateTime) as maxdate
FROM web_historyupdate
GROUP BY updateHistoryID ) tm
ON t.updateHistoryID =tm.updateHistoryID
AND t=tm.maxdate
You want the maximum datetime per date. As mentioned, "Maximum" translates to MAX in SQL and "per" transates to GROUP BY. One way to apply this to your data:
select *
from web_historyupdate
where updateTime in
(
select max(updateTime)
from web_historyupdate
group by date(updateTime)
)
order by updateTime;
There exist of course other ways to do this. You could for instance select every row for which NOT EXISTS a row on the same day at a later time.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have two DateTime columns like start_datetime and end_datetime. these fields contain DateTime value.
Here itself I need three different outputs. while you search, "15-09-2020 00:00:00" to "26-09-2020 23:59:59" it will display the first two lines. if you search like "26-09-2020 00:00:00" to "13-10-2020 23:59:59", it should result in three rows.
Instead of if you search, "10-09-2020 00:00:00" to "18-09-2020 23:59:59", it will show the first two rows.
In this case, I am stuck at the query. if I am using between some results won't come. if I am using greater than a symbol, some results won't come.
store the date time format in YYYY-MM-DD H:I:S, so you can easily
SELECT *
FROM table_name
WHERE start_datetime BETWEEN value1 AND value2 AND end_datetime BETWEEN value1 AND value2;
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
The sum of the acquisition price of works of art for each year (for example, if there were two works of art purchased for $1500 and $1000 in 2007, and one work of art purchased for $500 in 2008, then the sums would be $2500 and $500, for 2007 and 2008 respectively).
Assuming your table contains a field containing the year, and a field containing the price, you would simply use:
SELECT AcquisitionYear, SUM(Price) AS TotalPrice
FROM MyTable
GROUP BY AcquisitionYear
If your table contains a date field, you'd need to extract the year from this field using the YEAR() function:
SELECT YEAR(AcquisitionDate), SUM(Price) AS TotalPrice
FROM MyTable
GROUP BY YEAR(AcquisitionDate)
The keyword you're looking for is GROUP BY.
So your query will look something like
SELECT YEAR(date), SUM(price)
FROM tableName
GROUP BY YEAR(date)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have the following table structure:
car_id number PK, rent_date date, return_date date.
The table is very inconsistent and I would like to retrieve the car_id,max(return_date) but only if the max(return_date) is duplicated. Is it possible?
For example:
2 12/12/12 13/12/12
2 11/12/12 13/12/12
The return_date is duplicated for the car_id 2 so it should be returned by the query.
Thanks !!
SELECT T.car_id, max(T.return_date) From TableName T
Group by T.car_id, T.return_date
Having COUNT(*) > 1
If Time part of Date is not desired in comparison, you may use:
In Oracle:
SELECT T.car_id, max(T.return_date) From TableName T
Group by T.car_id, TRUNC(T.return_date)
Having COUNT(*) > 1
In MySql:
SELECT T.car_id, max(T.return_date) From TableName T
Group by T.car_id, DATE(T.return_date)
Having COUNT(*) > 1
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In my database in table_a every row has a date_created like "2011-04-17"
Now some of these dates are in the past, but my question is how can I retrieve the latest date that has not yet passed?
Try this one
SELECT * FROM table_a WHERE CURDATE() <= date_created
CURDATE()
Returns the current date as a value in 'YYYY-MM-DD' or YYYYMMDD
format, depending on whether the function is used in a string or
numeric context.
Can you try this,
SELECT * FROM table_a WHERE date_created >= CURDATE()
IF date_created is a date datatype then you can use
SELECT *
FROM table_a
WHERE date_created >= NOW()
LIMIT 1