How to select a date and ignor the time - mysql

I made a SQL Statement and I want to use the date but without the time.
My Select is:
SELECT DATEPART(dw, [Order].PerformDate)
And my Group by is:
GROUP BY [Order].PerformDate
Now how can I ignore the time?

You can use CONVERT function of SQL
select datepart(dw, CONVERT(DATE,[Order].PerformDate))
GROUP BY CONVERT(DATE,[Order].PerformDate)

Cast datetime value to date:
select cast(`Order`.PerformDate as date) as PerformDate

GROUP BY says "I want one result row per ____". In your case one row per PerformDate. If PerformDate is a datetime, but you only want to have one result row per date without time, then you must extract the date part:
group by cast(performdate as date)
You also want to display the weekday with datepart(dw, performdate) but this is no longer possible, because PerformDate is no longer available. Only its date part is. So:
select datepart(dw, cast(performdate as date))
from ...
group by cast(performdate as date);

Another one method:
select date_format(`order`.PerformDate, '%Y%m%d')
...
group by 1

Related

Query data between the same date

Is it possible to do something like this?
select * from table where Date BETWEEN '2019-05-29' AND '2019-05-29'
Yes it is possible. If you have time part you could use DATE function to skip it:
SELECT * FROM table WHERE DATE(Date) BETWEEN '2019-05-29' AND '2019-05-29'
-- it may degrade performance, condition is not SARGable
Yes, but the better approach is:
select t.*
from table t
where t.Date >= date('2019-05-29') AND
t.Date < date('2019-05-29') + interval 1 day
Why is this better? It doesn't have a function on the column name, so it can make use of an index on the date column.
Yes you can, if you want to run it in a test window without manually changing the date within the code you can set it as a variable. Use trunc to get rid of time i.e there will be no 29-05-2019 23:59:00. If you want the same date within a time period remove the trunc and then you can set hours-minutes-seconds which makes your query more precise
SELECT t.*
FROM table t
WHERE t.date BETWEEN trunc(to_date(:datefrom, 'dd.mm.yyyy hh24:mi:ss')) AND
trunc(to_date(:dateto, 'dd.mm.yyyy hh24:mi:ss'))

how to calculate expiry date using mysql query?

Calculate expiry date using mysql query.
calculate make_date and expiry_date difference against tblt_name in mysql query.
You can use this below query..
select tblt_name,
(CASE
WHEN datediff(expiry_date,CURDATE()) > 0 then datediff(expiry_date,CURDATE())
ELSE 'Expired'
END) as Remaining_days_expired from tablets;
SQL fiddle
Is this along the lines of what you want:
SELECT tblt_id, tblt_name, make_date, expiry_date, expiry_date - make_date AS shelf_life
FROM medicine
You can use use DATEDIFF() function to do so
SELECT tblt_id , tblt_name , DATEDIFF(s,expiry_date,make_date) FROM TABLE.
First para just put the different date type you wan to compare
s is just display the different in Seconds format.

Find results in certain date range

I am trying to only grab records that fall in a certain date range. The problem is that the timestamp and the date are stored as a string in the same cell. I want to only grab rows with a date that falls betweed 2013-05-01 and 2013-05-03.
date (stored as string)
2013-05-01T23:19:44
2013-05-02T23:19:40
2013-05-06T23:19:46
2013-05-06T23:15:17
mysql
SELECT * FROM table WHERE date BETWEEN 2013-05-01 AND 2013-05-03
Try
SELECT *
FROM table1
WHERE STR_TO_DATE(`date`,'%Y-%m-%d') BETWEEN '2013-05-01' AND '2013-05-03'
SQLFiddle
As #FreshPrinceOfSO absolutely correctly noted no index will be used in that case
SELECT * FROM table1
WHERE STR_TO_DATE(SUBSTRING(`date`,1,10),'%d-%m-%Y')
BETWEEN '2013-05-01' AND '2013-05-03'
The string is almost valid syntax for a datetime. Thus, an alternative, if perhaps slower method is to replace the 'T' with a space and then cast it to a datetime.
SELECT * FROM table1 WHERE
CAST(REPLACE(`date`, 'T', ' ') AS DATETIME)
BETWEEN '2013-05-01' AND '2013-05-03';
SELECT * FROM table
WHERE date('yourdate') BETWEEN date('2013-05-01') AND date('2013-05-03')

How to solve this "cursor" type query in MySQL?

I have the following data in my table. BTW ... this is a DD/MM/YYYY format:
Date
18/09/2012
17/09/2012
13/09/2012
11/09/2012
10/09/2012
09/09/2012
25/08/2012
24/08/2012
The result what I want are:
Date
18/09/2012
13/09/2012
11/09/2012
09/09/2012
25/08/2012
The rule:
It starts from the latest date (18/09/2012) and check the next one down (17/09/2012). If there is a date then removed that from the list because it requires to have 1 day apart. Then goes to 13/09/2012 and then check 12/09/2012 and didn't find and then move to next one so on and so on. Basically you can't have date close each other (min 1 day apart).
Now I can do this on cursor if it's on TSQL however since I'm working on MySQL, is there any such thing in MySQL? Or perhaps any sub-queries approach that can solve this query?
I'm appreciated your feedback.
Try this solution -
SELECT date FROM (
SELECT
date, #d := IF(#d IS NULL OR DATEDIFF(#d, date) > 1, date, #d) start_date
FROM
dates,
(SELECT #d:=null) t
ORDER BY
date DESC
) t
WHERE start_date = date
The subquery finds out start days (18, 13, 11...), then WHERE condition filters records. Try to run the subquery to understand how it works -
SELECT
date, #d := IF(#d IS NULL OR DATEDIFF(#d, date) > 1, date, #d) start_date
FROM
dates,
(SELECT #d:=null) t
ORDER BY
date DESC
SELECT
"MyTable1"."Date"
FROM
"MyTable" AS "MyTable1"
LEFT JOIN "MyTable" AS "MyTable2" ON
ADDDATE("MyTable1"."Date", INTERVAL 1 DAY) = "MyTable2"."Date"
WHERE
"MyTable2"."Date" IS NULL
ORDER BY
"MyTable1"."Date" DESC
As long as I know about mysql query will be quit tricky and buggy if some how you manage to write the one. I suggest go for cursor, here is the syntax of the cursor,
here is the syntax of the cursor

Row count differs when using DATE and DATETIME

As the title says, the row count differs when doing a select using DATE and DATETIME. Please advise.
I'm trying to select rows between 1st and 5th Jan, 2012. The date column datatype is bigint (UNIX timestamp).
select * from table_name
where sample_timestamp between unix_timestamp('2012-01-01')*1000 and unix_timestamp('2012-01-05')*1000
If I include the time in HH:MM:SS, the rows returned are correct i.e.
select * from table_name
where sample_timestamp between unix_timestamp('2012-01-01 00:00:00')*1000 and unix_timestamp('2012-01-05 23:59:59')*1000
Any input will be much appreciated. Thanks.
'2012-01-05' is actually '2012-01-05 00:00:00' which is not what you're writing in the second select.
I suspect what you mean to do is
select * from table_name
where sample_timestamp >= unix_timestamp('2012-01-01')*1000
and sample_timestamp < unix_timestamp('2012-01-06')*1000
which as a bonus handles leap seconds correctly too :)