I want to retrieve the data between a 6 day time period.
The output I want is:
Date
--------
2019-05-01
2019-05-04
2019-06-01
2019-06-06
2019-07-01
This is my query so far:
select date from data d
where CAST(d.createdate as Date) between CAST('2019-05-01' as Date)
AND DATEADD(CAST(dd,6,'2016-07-01') as Date)
Why is this not retrieving the results I want?
You have several problems with your query.
The first is with your DATEADD statement which is all mixed up. You are not nesting the casted date into the statement properly. This is the corrected version:
DATEADD(dd, 6, CAST('2016-07-01' as Date))
The second is that your select projection refers to the column date which does not exist. Instead, you probably want your createdate column.
The third is that your between clause is back to front. You are saying between 2019-05-01 and 2016-07-01 but the smaller date must come first.
In fact, your given example is incorrect. In your question, you say "want to retrieve the data between two dates only for 6 days." So, why would you start with a date in 2016 and then jump to a date in 2019 and add 6 days to the date in 2019? If you want to use the DATEADD approach, you need to use the same date in both positions.
So here is your corrected query:
select d.createdate from data d
where CAST(d.createdate as Date) between CAST('2019-05-01' as Date)
AND DATEADD(dd, 6, CAST('2019-05-01' as Date))
Related
I'm looking to create a recurring date calculator that will correspond to EVENTS on a specific calendar date. Each event has its own rule.
For example, for EVENT A rule is "Occurs Tuesday monthly with the start date Tuesday 9-17-2019. I'm using a time dimension table with every date populated until 2025. Using these dates I was using in the WHERE clause
WHERE dayname = 'tuesday' and ( DATEDIFF(DAY, '2019-09-17', Calendar_Date) % 28 ) = 0 to get monthly tuesdays.
But I'm running into issues when there are two 5 Tuesdays in a month. 3-3-2020 and 3-31-2020. Need date populated to be 4-7-2020 instead of 3-31-2020.
Can anyone help with a solution?
With your query, you're pretty close. All you need to do is to then sub-select
SELECT MIN(CalendarDate) AS CalendarDate
FROM (your query goes here) AS d
GROUP BY YEAR(CalendarDate), MONTH(CalendarDate);
I need to get the difference between 2 date time in minutes(Time difference in minutes). And the last difference will be calculated based on 6 PM of every date.
Sample data: need result of last column
User_Name Date Time difference in minutes
User 1 1/1/06 12:00 PM 30
user 2 1/1/06 12:30 PM 315
user 3 1/1/06 5:45 PM 15
Here the date will be always in same date and the last user date difference calculated based on default value 6PM. Assuming the dates of any user will not cross 6PM time.
Please suggest how to write the query for the same.
You could use the lead window function.
I assume your table is called mytable and the date column is mydate (it is a bad idea to call a column Date as it is a reserved word).
select user_name,
round((lead(mydate, 1, trunc(mydate)+18/24)
over (partition by trunc(mydate) order by mydate)
- mydate) *24*60) as difference
from mytable
I found the solution.. if its not correct let me know
SELECT User_name,created_date,
trunc(to_number((cast(nvl(lead (created_date,1) OVER (ORDER BY created_date),TRUNC(SYSDATE) + (19/24)) as date) - cast(created_date as date)))*24*60) as difference
FROM users;
I would like to fetch data for records for a given date/ 1 day interval.
This means from 00:00:00 to 23:59:59 of that day. The date value comes from another table in a join statement... i.e, it's not a parameter.
Suppose I have a Transactions table with a DateOccured (Datetime) field
From my research, I have two possible options(amongst others).
Calculate the datetime range values for that day
From 00:00:00
DateAdd(dd, DateDiff(dd,0,Transaction.DateOccured,0)
To 23:59:59
DateAdd(dd, 1, DateAdd(ss, -1, CONVERT(DATE,Transaction.DateOccured))) -- 1 second before midnight of following day
(several options of doing this, e.g placing this in the SELECT list or placing it in the WHERE clause)
Convert Datetime to date then append appropriate strings(for the time portion) to create the range (does not seem efficient)
Which would be the best strategy?
Would I place this calculation in the SELECT list and use as a field in the WHERE? or is it better of in the WHERE clause
The simplest method is:
on cast(t.dateoccurred as date) = t2.otherdate
This could use an index on the first table (see Martin Smith's comment). It can definitely take advantage of an index on the second table.
The following version could also use an index on the first table but not the second:
on t.dateocurred >= t2.otherdate and
t.dateoccurred < dateadd(day, 1, t2.otherdate)
I'm executing the next query in sql server 2012.
select *
from table
where date > convert(date, '2015/02/12')
order by date asc
but I'm getting the next set:
2015-02-12 06:40:42.000
2015-02-12 06:45:44.000
2015-02-12 06:48:15.000
2015-02-12 07:06:28.000
2015-02-12 07:26:46.000
...
I can fix this by changing the date to '2015/02/13', but I have the doubt about this behavior, why am I getting dates from feb 12 when I am specifying that I need only later dates ?. I also tried using cast('2015/02/12' as date), but I could not have the answer I was looking for
Because dates without times are intepreted as 12:00 midnight on that date. If you want only dates after February 12, 2015, then select all dates greater than or equal to February 13, 2015 (which again will be interpreted as midnight on February 13th).
select *
from table
where date >= convert(date, '2015/02/13')
order by date asc
why am I getting dates from feb 12 when I am specifying that I need only later dates ?
You're not specifying that you need only dates after Feb 12. You're asking for every row in which the value in the "date" column is greater than '2015-02-12'.
The value '2015-02-12 06:40:42.000' is greater than '2015-02-12'.
When comparing the date 2015/02/12 with your datetime data, this will implicitly compare the converted date 2015-02-12 00:00:000, the date at the beginning of the day with all of your data in column date.
But you are actually comparing datetime data, which has a time part as well, which gets compared.
And because you're comparing the beginning of the day (2015-02-12 00:00:000) with a value which is after it, for example 2015-02-12 06:40:42, all of the dates from will be displayed, because 6:40 AM is after (greater than) 0:00 AM.
Try this:
SELECT *
FROM TABLE
WHERE DATE >= DATEADD(SECOND, -1, '2015/02/13')
jarlh is right, though I'll clarify a little. Each of the "dates" you show above fall after 12:00 midnight starting 2015-02-12. They are actually timestamps.
If you don't want to see anything for the day specified in the filter, you add a day and use the greater-than-or-equal-to (>=) operator.
SELECT *
FROM table
WHERE (date >= DATEADD(d, 1, CONVERT(date, '2015/02/12')))
ORDER BY date ASC
I am trying to create a 'Due Date' column where it takes the date from the 'DATE_OF' column and adds the dynamic variable, "DAYS".
SELECT DATEADD('DAY',TABLE1.DAYS,TABLE1.DATE_OF) AS DUE_DATE
FROM TABLE1
This is the format of the date from 'DATE_OF'
04/16/14 12:00 AM
06/24/14 12:00 AM
04/01/14 12:00 AM
DAYS uses only integers (Values between 10 and 90).
But I keep getting this for every result
12/30/99 12:00 AM
What am I doing wrong?
UPDATE
I referenced mssqltips.com and found that the abbreviation for 'DAY' is 'd'. When I changed this, it worked. I do not know why.
As you've come to figure out, there are dissimilarities between many other RDMS' SQL/functions and Access SQL/functions.
Unfortunately, 'DAY' is not a proper interval when using the DateAdd function in MS Access. It is 'd'.
For further note: If you were going to hardcode in a date, the # on either side of the date are required. More details can be found here
Access does not accept not accept the value 'DAY' in the ADDDATE() function.
A complete list of accepted values can be found here
Here are the details of the accepted values.
Value Explanation
yyyy Year
q Quarter
m Month
y Day of the year
d Day
w Weekday
ww Week
h Hour
n Minute
s Second