MS Access Date() yields no results - sql

I've searched but been unable to figure this out. I used to use MS Access many many years ago but have switched mostly to PHP and MySQL to do my work. But when I"m at work, I have to use those resources.
Trying to build a basic query from an ODBC connection to a SQL Server. One of the columns is a date field (SQL server field type: datetime).
When I build the query and enter Date() for my Where clause should be it yields no results.
SELECT dbo_Order.OrderStatusID,
dbo_Order.FillerOrderNumber,
dbo_Order.RelevantClinicalInfo,
dbo_Order.ReasonForStudy,
dbo_Order.ProcedureDescList,
dbo_Order.PlacerFld2 AS Modality,
dbo_Order.ScheduleDate,
dbo_Order.ExplorerStatus,
dbo_Order.SiteID
FROM dbo_Order
WHERE (
(
(dbo_Order.PlacerFld2) = "CRFL"
OR (dbo_Order.PlacerFld2) = "CT"
OR (dbo_Order.PlacerFld2) = "SAMR"
)
AND ((dbo_Order.ScheduleDate) = DATE ())
AND ((dbo_Order.ExplorerStatus) = "SCHEDULED")
AND ((dbo_Order.SiteID) = 1)
);
I've tried also doing something like Date: Format([ScheduleDate], "dd/mm/yyyy") but this also returns no results.
MS Access 2016
SQL Server 2008

If your ScheduleDate include any time values, Date() returns 1/1/2018 00:00 as a default so if your Scheduled date is 1/1/2018 04:34:00 it won't match.
Try using DateValue([ScheduleDate]) to return just the date

I would prefer:
dbo_Order.ScheduleDate >= Date() AND dbo_Order.ScheduleDate < Date() + 1
DateValue([ScheduleDate])has to be computed on every row and can't use an index (if any) what can affect query performance on huge data. See Access query won't work when dates have times In addition DateValue can't handle NULL values.

Related

Select records of last 12hours vb.net/MS ACCESS

I am vb.net newbie and working on a program which will be used as a daily-worklog. As backend I use MS ACCESS. I store the "datetime.utc.now" time in a field (type:date/time) of MS Access.
This is shown in the database like: dd.mm.yyyy hh:mm:ss
I want to see all entered items of the last 12 hours. I used many different ways, but I´m not able to fix it.
My preferred / logical (for me :-)) way to do this was:
add the parameter:
mycommand.SelectCommand.Parameters.AddWithValue("date12",OleDbType.DBTimeStamp).Value = DateTime.UtcNow.AddHours(-12)
Query the database
select * from complaints where entrydate >= #date12;
But I can't figure out why it doesn't work.
Failure message = "data types in criteria expression incompatible"
I assume the problem is the different time formats, but I'm not sure and I have no clue how I could fix it.
MS Access does not use named parameters. You use a ? for all of the placeholders in the SQL statement. You still can and should give the parameter object a name, but the value is matched to the placeholder based on position in the Parameters collection and query string rather than name.
SQL:
"select * from complaints where entrydate >= ?;"
VB:
mycommand.SelectCommand.Parameters.Add("date12",OleDbType.Date).Value = DateTime.UtcNow.AddHours(-12)
Do NOT settle for hard-coding the parameter value. What you can do is use SQL expressions to determine the date value, and avoid both parameters and string concatentation:
select * from complaints where entrydate >= DATEADD('h', -12, Now())
You can change the type to OleDbType.Date or hardcode the query:
select * from complaints where entrydate >= ( Now() - 0.5 )
Trouble inserting DateTime into Access with OleDb
As a side note, most value types are generally safe to hardcode:
"select * from complaints where entrydate >= " & DateTime.UtcNow.AddHours(-12).ToOADate()

query to_date issue

Below query is giving me two different result for two different oracle database. ( database version I am not aware of )
I want to ask like TO_DATE function varies from database version ?
select * from ACC WHERE CHANGE = TO_DATE('01/02/2015','MM/DD/YY');
Note that I am facing issue in only = but < and > working fine.
In 1 db : this query is giving 1 record
In 2 db : this query is giving 0 record.
In DB : CHANGE value is 02-JAN-14
As far as I know, to_date() hasn't changed. More likely is that you have a time component on your column change, which prevents = from working. Try this:
select *
from ACC
where trunc(CHANGE) = TO_DATE('01/02/2015','MM/DD/YY');
Or, how I would prefer to write this:
where change >= DATE '2015-01-02' and
change < (DATE '2015-01-02') + 1
Using the date keyword, you can use ISO standard formats. This version also allows the use of indexes on the change column.
if they are on the same server is strange. If you are on two different servers verify that the date format of the server is the same.
You can see it in the settings under NLS

Working with dates in SQL query using MDB-Tools

I have been working on a project where I have to fetch data from MS-Access database. I am using mdb-sql tool for running sql queries into that database. But the problem occurs when I try to put where clause on a datetime column. It always throws a syntax error.
Please help me how can I use dates in where column.
I have tried using these queries -
SELECT * FROM table1 WHERE checkintime > '04/22/13 12:15:39'
SELECT * FROM table1 WHERE checkintime > #04/22/13 12:15:39#
mdb-sql displays human-readable dates in results, but requires timestamp values in queries -- calculated as the number of seconds sine 1/1/1970 (the beginning of the UNIX epoch).
So the correct query to extract all records with a date greater than 04/22/13 12:15:39 would be:
SELECT * FROM table1 WHERE checkintime > 1366650939
An easy way to calculate this from the command line is:
date --date='04/22/13 12:15:39' +"%s"
You can use
MS Access: Format Function (with Dates):
where Format([checkintime ],"yyyy-mm-dd hh:nn:ss")>='2013-04-22 12:15:39'

DateDiff in SQL ODBC

I'm using a datediff in SQL. It returns records when run directly in sql server 2008, but when I try and run it through ODBC it doesn't bring up an error, but it returns no rows.
SELECT mc_id, mc_date_entered,
COUNT([mv_value]) total
FROM MarkbookValue t1
RIGHT JOIN MarkbookColumn t2 ON t1.mv_column_id = t2.mc_id
WHERE mc_module_id = '703000026609358'
AND DateDiff(dd, mc_date_entered, '2012-10-05 20:00:00') = 0
AND mc_type = 'KEF'
AND mc_entered_by = 'A.ADMIN'
GROUP BY
mc_id, mc_date_entered;
Getting rid of the DateDiff lets the function run correctly, but I'd obviously like to have it in there. What am I doing wrong?
YYYY-MM-DD HH:MM:SS is not a safe date format to use for a date time literal value in SQL Server. Depending on SET DATEFORMAT your month and day part might be switched.
YYYY-MM-DDTHH:MM:SS and YYYYMMDD HH:MM:SS are safe to use regardless of SET DATEFORMAT.
To get the rows for a specific date I suggest that you do as in the answer provided by #RichardTheKiwi or if you are in SQL Server 2008 you can cast your column to date to remove the time part.
where cast(mc_date_entered as date) = '2012-10-05'
YYYY-MM-DD is safe for data type date.
I would almost always write dates in ISO-8601 format, the one without dashes being YYYYMMDD.
Just would like to also point out that if you want your query to use an index on mc_date_entered and remain SARGABLE, you'll want to rewrite it like this.
SELECT mc_id, mc_date_entered, COUNT([mv_value]) total
FROM MarkbookValue t1
RIGHT JOIN MarkbookColumn t2 ON t1.mv_column_id = t2.mc_id
WHERE mc_module_id = '703000026609358'
AND mc_date_entered >= '20121005'
AND mc_date_entered < '20121006'
AND mc_type = 'KEF'
AND mc_entered_by = 'A.ADMIN'
GROUP BY
mc_id, mc_date_entered;
Are you also aware that DATEDIFF(DD only considers the date portion, so there's really no point including the time (if we were still using DATEDIFF)?

Sql Shorthand For Dates

Is there a way to write a query equivalent to
select * from log_table where dt >= 'nov-27-2009' and dt < 'nov-28-2009';
but where you could specify only 1 date and say you want the results for that entire day until the next one.
I'm just making this up, but something of the form:
select * from log_table where dt = 'nov-27-2009':+1;
I do not believe there is one method that is portable to all RDBMSes.
A check in one of my references (SQL Cookbook) shows that no one RDBMS solves the problem quite the same way. I would recommend checking out Chapter 8 of that book, which covers all of the different methods for DB2, Oracle, PostgreSQL, MySQL.
I've had to deal with this issue in SQLite, though, and SQL Cookbook doesn't address that RDBMS, so I'll mention a bit about it here. SQLite doesn't have a date/time data type; you have to create your own by storing all date/time data as TEXT and ensure that your application enforces its formatting. SQLite does have a set of date/time conversion functions that allow you to add nominal date/times while maintaining the data as strings. If you need to add two time durations (HH:MM:SS) to each other, though, based upon data that you've stored in text columns that you are treating as date/time data, you'll have to write your own functions (search for "Defining SQLite User Functions") and attach them to the database at runtime via a call to sqlite3_create_function(). If you want an example of some user functions that add time values, let me know.
For MS SQL Server, check out DATEPART.
/* dy = Day of Year */
select * from log_table where datepart(dy, dt) = datepart(dy, '2009-nov-27');
With SQL Server, you could
Select * From table
Where dt >= DateAdd(day, DateDiff(day, 0, #ParamDate), 0)
And dt < DateAdd(day, DateDiff(day, 0, #ParamDate), 1)
As long as you are dealing with the date data type for the respective data type, the following will work:
t.date_column + 1
...will add one day to the given date. But I have yet to find a db that allows for implicit data type conversion into a date.
SELECT '12-10-2009' + 1
...will fail on SQL Server because SQL Server only performs the implicit conversion when comparing to a datetime data type column. So you need to use:
SELECT CONVERT(DATETIME, '12-10-2009') + 1
For Oracle, you'd have to use the TO_DATE function; MySQL would use something like STR_TO_DATE, etc.
Have a column that just has the date part (time is 00:00:00.000) and then you can add a where clause: WHERE dt = '2009-11-27'