Access Runtime Linked Table Query Date Format - sql

I have the following query executed on a Linked table in Access:
Select * From table WHERE DateField = #18-Dec-2016#
This date where clause is created with the following VBA code:
strWHERE = strWHERE & "DateField = #" & VBA.Format(txtDate, "dd-mmm-yyyy") & "#"
When using the full version of Access 2016 it it sent to SQL as:
Select * From table WHERE DateField = {d 18-Dec-2016}
This works! However, when using the Access 2016 runtime, it is sent to SQL as:
Select * From table WHERE DateField = {ts 18-Dec-2016}
and it fails.
I am using SQL Server Profiler to find the exact SQL being sent.
Any ideas?
EDITS:
Tried changing format to yyyy-mm-dd
Tried added "00:00:00" to the VBA code building the where clause, and this does not change what I see sent to SQL through the profiler.
SOLVED:
For some reason, the behaviour of linked table queries is different between the full version of Access and the 2016 Runtime.
In the Runtime version, the data type of the field in the linked table is used to format the query. So because the DateField was DateTime, it passed the where clause as ts. BY creating a view and formatting the date as just a date, this was solved.

Related

SQL DATEDIFF Function returns wrong values

Im executing following sql statement to get the sum values using date difference as a condition. "t_QueryDemand" is the table name and "DESIRED_SHIP_DATE" is the value I compare with current system date. But, I always get a date difference higher than 44100, even though the actual difference is between 0-60. DESIRED_SHIP_DATE data type is datetime2(7). I'm using MS SQL server database. What could be the issue.
Dim curDate As Date = Date.Today()
Dim strSql7DayDemand As date= "SELECT SUM(ORDER_QTY) AS ORDQTY, (TOTAL_SHIPPED_QTY) AS SHIPQTY, DATEDIFF(day," & curDate & ",DESIRED_SHIP_DATE),DESIRED_SHIP_DATE as DIFF from t_QueryDemand where ID='" & Trim(txtPartID.Text) & "' AND DATEDIFF(DAY," & curDate & ",DESIRED_SHIP_DATE) >" &
Instead of using a injection use the SQL function GETDATE()
DATEDIFF(day,GETDATE(),DESIRED_SHIP_DATE)
This will calculate the date on the server instead of locally like you are currently doing.
As has been pointed out you have a serious security vulnerability -- you need to use parameterized queries and not string concatenation or you are vulnerable to an SQL injection attack.
It will also make your code more robust -- right now you have txtPartID.Text and you are just putting that into your query but what if someone enters an non-number in this field -- you have no way of catching that now. With a parameterized query of a numeric type you would catch this problem when you converted the data entry to a number.

Defining the result of a SQL Statement

I want to get the date from this SQL statement and use that as an ExpirationDate in asp classic so I can use that date in an if/then (conditional) statement. Lost on how to retrieve that. Appreciate the help.
strSQL = "SELECT SUM (Credits)[Amount] from TableName where id = '" & id & "' and date >= '4/1/2019' and date >= dateadd(day,-360,getdate())"
You can add another SQL query with dateadd(day,-360,getdate()) and get the expiration date. However, as you are getting the date based on your current date you can get it using ASP instead of SQL (i.e. DateAdd("d",1,Now())) assuming your ASP server and SQL server are in the same time zone.

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()

Change date format in SQL for ASP

I have the following code:
"SELECT top 1 * FROM CensusFacility_Records WHERE Division_Program = 'Division 1' AND JMS_UpdateDateTime = '" & date & "'"
The date format in column JMS_UpdateDateTime is:
8/22/2013 12:00:07 AM
How can I make sure that my "date" in the query is converted to the correct time format?
My date variable in my SQL query is a real/date time. I would like for it to match the format within the JMS_UpdateDateTime field.
If your database table is small enough, you can cast the value to an actual datetime inside your query, and pass the value in as a datetime parameter, so you're comparing dates instead of comparing strings.
If your table has a decent amount of records, don't do this, because it will be a performance hog.
SELECT top 1 *
FROM CensusFacility_Records
WHERE Division_Program = 'Division 1'
AND cast(JMS_UpdateDateTime as datetime) = #dateParam
I believe SQL Server will be able to read the string that's in your database and automatically cast it properly, assuming your server settings are standard.
But in any case, use parameterized SQL instead of passing in a string like you've got.
The format of your SQL DateTime is actually a bit of a red herring here - it can be displayed in any way the front end (e.g. Management Studio) chooses. The important thing here is that your date variable is in an unambiguous format. With this in mind I'd recommend using the ISO 8601 date/time format, e.g. date.ToString("o") (assuming date is a DateTime).

Query in SQL that allows wildcard month/day but specific year

If I have a Date/Time field in Access and I need to find all the entries that are in a specific year... say 2009. If my fields are set up as MM/DD/YYYY, how would I do a query to essentially do //2009
update
Thanks for the help everyone, but I guess the issue is that I'm kind of dynamically constructing my SQL statement in ASP.
"SELECT * FROM database WHERE (datecolumn LIKE " & string & ")"
where the string ends up returning a string I constructed 'DD/MM/YYYY'. If the user inputs nothing into either of the 3 fields, I'll need do a specific query where that part is a wildcard... as in if they don't put in a day, I'd essentially need a
"SELECT * FROM database WHERE (datecolumn LIKE "*/MM/YY")"
.... which doesn't work in Microsoft Access
How about
WHERE MyDate >= '1/1/2009' AND MyDate < '1/1/2010'
You should be able to use the Year() function
Some Examples:
Year (#05/05/1985#) returns 1985
Year (#17/07/2005#) returns 2005
If your field is called "myDate" for example, you can construct a query to select everything in the year 2009 like so:
SELECT * FROM myTable WHERE Year(myDate) = 2009
This function is available in both MS Access and SQL Server, too (if you're interested).
Some links for further information:
Access: Year Function
Year Function
YEAR (Transact-SQL)
WHERE Year(DateColumn) = 2009
OTTOMH
SELECT * FROM Table WHERE DatePart ("yyyy", YourDatetimeColumn) = 2009