I have a created field in Access that I am trying to replicate in SQL, but I can't seem to get the CASE statement correct. Here is the Access query:
Missed Deliveries: Sum(IIf([Shipping Notif Calc]="11/30/1999",IIf([PO Delivery Date Calc]-[Confirmation Date Calc]<-5 Or [PO Delivery Date Calc]-[Confirmation Date Calc]>5,1,0),IIf([Confirmation Date Calc]="11/30/1999",IIf([PO Delivery Date Calc]-[Shipping Notif Calc]<-5 Or [PO Delivery Date Calc]-[Shipping Notif Calc]>5,1,0),IIf([Confirmation Date Calc]-[Shipping Notif Calc]<-5 Or [Confirmation Date Calc]-[Shipping Notif Calc]>5,1,0)))))
Use the DATEDIFF function instead of subtracting date columns.
E.g. replace:
[PO Delivery Date Calc]-[Confirmation Date Calc]
with:
DATEDIFF(day, [Confirmation Date Calc], [PO Delivery Date Calc])
(Note that you have to swap the order of the dates.)
Related
Title says it all.
Table looks like this
Table: Time
First Column: startdate (shown as yyyy-mm-dd hh:mm:ss)
Second COlumn: durationminute (shown in minutes)
Since the above doesn't give me an end date, I'm trying to create a query which will do that.
I'm assuming this is going to be a dateadd function but I can't get it to work. The durationminutes is a variable to is dependent on that one row so date_add(minutes, 30, startdate) wouldn't work as there is no constant.
I've tried
date_add(days, durationminutes/1440, startdate)
But am getting a syntax error.
MySQL understands date arithmetics. Assuming that startdate is of date or datetime datatype (as it should be!), you can do:
select t.*,
startdate + interval durationminute minute as enddate
from time t
Im my Mysql db there is a date field called "completed date " ,its data type is datetime. I need to convert into week format. This should be week ending saturday. this week based date should comes in new column also.Please Provide the query
Try
SELECT week("completed date") FROM "myTable";
A brief documentation can be found here
I have date column field it's data look like 2014-05. how i get previous 6th month interval. My mysql query below
SELECT name,grade from users where data_year BETWEEN DATE_FORMAT('2014-04', '%Y-%m') - INTERVAL 12 MONTH AND DATE_FORMAT('2014-04', '%Y-%m')
I got result when i put with days like 2014-04-31
The strings you used aren't valid dates as they don't contain the day. If you want to use a date literal in a SQL statement you should use the form DATE '2014-04-01', eg
SELECT name,grade
from users
where date_column BETWEEN DATE '2014-04-01' - INTERVAL 6 MONTH
AND DATE '2014-04-01'
UPDATE
From the comments it seems the question actually is - how to parse an incomplete date string. You can use STR_TO_DATE for that, but you'll have to append -01 at the end, otherwise you'll get an invalid date with 0 for the day of month, ie 20140-04-00 instead of 2014-04-01.
I have a table that has current date and delivery date. I want to minus due date from current date and want to save only days left in dbtable
SELECT clientId,
curentDate,
deliveryDate DATEDIFF(day,-(deliveryDate) CURRENT_TIMESTAMP) AS Days_Left
FROM dbo.tblorder
e.g:
today date 12/1/2014
delivery date 20/1/2014
days left 8
Did you look in the documentation for examples on how to use DATEDIFF?
SELECT clientId, currentDate, deliveryDate,
Days_Left = DATEDIFF(DAY, CURRENT_TIMESTAMP, deliveryDate)
FROM dbo.tblorder;
I am trying to generate simple report which gives me Failed test count daily for whole month. Is there a way to write a sql query that does that? Rather then running queries by changing dates? This is what i am doing:
select count (*) from students where Message Like 'Failed Test.%'
and Timestamp > '01-01-2013' and Timestamp < '01-02-2013'
So above query gives me count 5. Then i change the date from 01-01 to 01-02 and get next days count and so on. But this seems like very time consuming thing. Just checking if there is a better way to do that.
This gives counts grouped by day for a month's date range:
select cast(timestamp as date) as [Date], count (*) as NumFailedTests
from students
where Message Like 'Failed Test.%'
and Timestamp between '2013-01-01' and '2013-01-31'
group by cast(timestamp as date)
order by cast(timestamp as date)
You can obviously widen the date range...
[Hopefully the 'timestamp' column is not really a timestamp...]