I currently have a table in my database keeping track of a start date and end date for a service. I need to compare a date entered by the user to see if it is between the start and end dates. My issue right now is that in the table, access stores that start date as DD/MM/YYYY, the textbox in my form that the user puts their date in is formated as DD/MM/YYYY. However, once I hit VBA and run an SQL query, it reads the date as MM/DD/YYYY.
My query is:
SELECT * FROM table WHERE #09/01/2018# BETWEEN startDate AND endDate
My test entry is:
table:
startDate endDate service
08/01/2018 02/02/2018 ABC
This should return this entry, however as it reads it as MM/DD/YYYY it does not. If I enter #13/01/2018# it returns the entry as it detects that 13 is the date and cannot be a month. How could I correct this so that it takes 09/01/2018 and returns the entry as well?
If this is an VBA query then your observations are correct. In the Query designer these will work as expected.
Review Allen Browne's excellent explanation here; http://allenbrowne.com/ser-36.html
with solutions as well.
Your query should use either the "reverse" US sequence or the ISO sequence:
SELECT * FROM table WHERE #2018/01/09# BETWEEN startDate AND endDate
or, if you build it from code:
SearchDate = SomeDateValue
SQL = "SELECT * FROM table WHERE #" & Format(SearchDate, "yyyy\/mm\/dd") & "# BETWEEN startDate AND endDate"
or, if you always search for today:
SQL = "SELECT * FROM table WHERE Date() BETWEEN startDate AND endDate"
Related
I am trying to run a query from a linked table in MS SQL Server that has a datetime field. I'm trying to run a simple query to search for records for a specific date (#03/24/2018#), but since the datetime field has the time as well, I am not getting any records unless I specify the time range using BETWEEN with the time (Between #03/24/2018 00:00:00 AM# And #03/24/2018 11:59:59 PM#).
Original query which does not return desired output:
SELECT *
WHERE MyDateTimeField) = #3/24/2018#;
Query
SELECT *
WHERE MyDateTimeField) Between #3/24/2018 00:00:00 AM# And #3/24/2018 23:59:59#);
Is there a workaround to this as to not have to use a BETWEEN operator with the time?
To avoid time portion, check forMyDateTimeFieldequal/greater than searched day and less than next day:
SELECT *
FROM MyTable
WHERE
MyDateTimeField >= #3/24/2018#
AND
MyDateTimeField < DateAdd("d",1,#3/24/2018#);
In opposite of convertingMyDateTimeFieldto date, this does not prevent index usage and handlesNullValues onMyDateTimeField.
I have a basic SQL knowledge and I am trying to retrieve data from my database based on the date a user chooses. So, if the user selects 2018-03-01 I want to display the data for the date value 2018-03-02.
I can search data for a specified date in the following way:
select * from FLIGHTS where DEPARTURE_DATE = '2018-03-01';
Now I want to search the data for the date next to the specified so for '2018-03-02' in this case. I cant directly search for 2nd March because I don't know what date the user will choose
So is there any way to query data for the date next to the one specified?
I have already tried looking up everywhere but couldn't find anything that makes sense to me.
Thx
I think this works well. By the documentation for date/time functions, you should be able to do something like,
select * from FLIGHTS where DEPARTURE_DATE = '2018-03-01' + INTERVAL 1 DAY
What sort of element are you using for the user to be able to get their date and what is your codebase? We have mysql for the DB but we need to know if your codebase is c#, PHP etc.
Most languages generally have a function to allow you to add days to a datetime, if for instance you used C# and had a datetime selector posting its value you can do:
DateTime dateField = postedDateTime.AddDays(1);
from there you can easily escape the value and push it through your db call.
or PHP:
$dateField = date('Y-m-d', strtotime($postedDateTime. ' + 1 days'));
If you would like a date range (date selected and the next day) then you would adjust your php code to have:
$dateFrom = $postedDateTime;
$dateTo = date('Y-m-d', strtotime($postedDateTime. ' + 2 days'));
$dateTo = date('Y-m-d', strtotime($dateTo. ' - 1 seconds'));
//DB Query
$query = "select * from FLIGHT where DEPARTURE_DATE >= :dateFrom AND DEPARTURE_DATE <= :dateTo";
The above assuming you're using pdo, but you can see how to adjust for your situation. I am also assuming the datetime is based on midnight for each day so you may need to adjust your dateTo to take from 1 second before midnight the day after just so you can get the entire days data
You can make use of interval as day as mentioned in the answer. Along with day you can also have day_minute, day_hour,day_second but be sure when you are adding decimals on these.
I need a small validation in Sql for from date and to date columns i have.
suppose I have given 01/08/2014 and 30/08/2014
Again the front end user should not give this date period.
I can restrict by using below validation like,
select DocEntry from [#PR_OTIMESHEET] where U_frmdate >='20140801' and U_todate <='20140830'
but user may be give like
from date = 15/08/2014 and to date = 30/08/2014.
In this case above query is become false.
between 01/08/2014 and 30/08/2014 should not give again .
May I know how can I restrict the users ?
The way to query based on a date tends to change based on your database.
For Microsoft SQL Server you need to use CONVERT
Example:
CONVERT(datetime, '20140801', 112)
The 112 is a specified date format. See other date formats here. http://msdn.microsoft.com/en-GB/library/ms187928.aspx
so your example should be:
select DocEntry from [#PR_OTIMESHEET] where U_frmdate >= CONVERT(datetime, '20140801', 112) and U_todate <= CONVERT(datetime, '20140801', 112)
NOTE: Your select range doesn't make any sense as the dates are the same!! So this will only return stuff where the date is exactly 2014-08-01 00:00:00
I'm having some problem with this query, it works on my localhost (IIS 6) but when i uploaded the same code to web server (IIS/7.5) it returns no data from the Database.
Here is the query:
Set RsTdL = Con.Execute("Select * From COURSE1 Where DUE_DATE Like '"&Date&"%' and CLASS = '"&CCode&"' ORDER BY SUBJECT, LECT_NO, SUBTOPIC")
'" & Date & "%' matches the system date in the database so it'll show current date record.
And in the DUE_DATE column the date is in this format 9/3/2013 3:42:03 PM
i don't know how to debug this query because its working on my local machine and the WEB SERVER doesn't return any errors so im stuck ...
Please help,
thanks
DUE_DATE is Date/Time type and you want to match all values for today's date ignoring the time of day. So ask for DUE_DATE greater than or equal to the earliest time of today (midnite) and less than tomorrow's date.
"SELECT * From COURSE1" & vbCrLf & _
"WHERE DUE_DATE >= Date() AND DUE_DATE < DateAdd('d', 1, Date())" & vbCrLf & _
"AND [CLASS] = '" & CCode & "' ORDER BY SUBJECT, LECT_NO, SUBTOPIC"
Also CLASS is a reserved word, so enclose that name in square brackets.
The Database may be using the date format set on the server.
Which could be different than your locale machine. Do a simple select on the date fields in each environment to see how they look.
For safe date comparisons use the datediff function and use d ( which counts the number of days ) as the parameter. Select rows with a datediff of zero to retrieve all courses on the same date. This will work even if the datestamp includes the time.
This is my first question, please be kind. I am writing a macro in access and I want a series of reports to run one after another. the problem is all report have a date range that needs to be entered. Some are for the previous week some are for the previous month.
Is there a way in VBA or the macro creator to automatically calculate a date range for the previous month or week and populate the field to fully automate the process without manually entering the date range each time.
I am a new to VBA. Any help would be great, just point me in the right direction. Have a good day.
This query is created using the query design window in MS Access and then cut from SQL view. It will show records for last week, where ww is week number, in table1
SELECT Table1.AKey, Table1.atext, Table1.ADate,
Format([ADate],"ww") AS Week, Month([ADate]) AS [Month],
Year([ADate]) AS [Year]
FROM Table1
WHERE (((Format([ADate],"ww"))=Format(Date(),"ww")-1)
AND ((Year([ADate]))=Year(Date())));
You will notice that one column is called Month. You can use this to set a previous month in a similar way to setting the previous week. For example, both last week and last month:
SELECT Table1.AKey, Table1.atext, Table1.ADate,
Format([ADate],"ww") AS Week, Month([ADate]) AS [Month],
Year([ADate]) AS [Year]
FROM Table1
WHERE (((Format([ADate],"ww"))=Format(Date(),"ww")-1)
AND ((Year([ADate]))=Year(Date())))
OR (((Month([ADate]))=Month(Date())-1)
AND ((Year([ADate]))=Year(Date())));
The SQL could be written much more neatly, but you may as well start with the query design window.
i guess the date has a own field in the database you open
then you can do something like this
strSQL = "SELECT * FROM reports WHERE Date >= " & now() -7
rs.open(strSQL)
' for the last week
strSQL = "Select * FROM reports WHERE Date >= " & now() - 30
rs.open(strSQL)
' for the last month
but you will need to format now() to the same format as it is in your Table
and that is just kinda the rawest code. i had to handle something similar and this worked out quite well