I am stuck with a query to compare the current date with date stored in access database table in the given format 7/25/2012
I am using this query:
Sql = "SELECT max(token_today)
FROM token
WHERE issue_date = #" & FormatDateTime(Now, DateFormat.ShortDate) & "#"
and I am getting the error below:
conversion from string ** to type 'Integer' is not valid.
Please tell me how to compare the date.
Thank You!
If you wish to compare today's date, there is no need for an external reference:
Sql = "SELECT max(token_today) FROM token WHERE issue_date = Date()"
This saves all sorts of problems with locale, as well.
i think this should work:
"SELECT max(token_today) FROM token WHERE issue_date = #" & FormatDateTime(Now,"YYYY/MM/DD") & "#"
Related
My computer's regional date setting is dd/mm/yyyy. I am using MS Access. I would like to insert records into a database using the SQL INSERT INTO statement. When I try to insert a date using the #dd/mm/yyyy# syntax, and view the resulting record in the table after, it turns out the record displays the date in the format mm/dd/yyyy instead, but ONLY for the first 10 days of the month; if the day is 11 onwards, the record displays dd/mm/yyyy as intended.
For example if in SQL code I input #09/02/2022#, the table will display the record with the date 02/09/2022 instead. However if my SQL code is#11/02/2022#, then the correct order 11/02/2022 is shown in the record.
Please help.
Ok, the way this works?
You don't have to care, know, or think about the users regional format settings.
So, if you drop some control on a form? Just make sure that control is set to a date type format. Your done.
BUT ONE big whopper:
IN ANY AND ALL cases, your string based date format MUST be in USA format. Or you can use ISO date format.
dim MyDate as Date
MyDate = me.InvoiceDate
So, now we have a internal format date variable. How to insert into a table?
dim strSQL as string
strSQL = "INSERT INTO tblInvoice (InvoiceNum, InvoiceDate, InvoiceAmount " & _
"VALUES (1234, " & quDate(MyDate) & ",50)"
So, you ALWAYS format the date value into USA format.
You can type that format command over and over, but that fast becomes tiring.
so, I use a little helper function:
Public Function quDate(dt As Date) As String
quDate = "#" & Format(dt, "mm\/dd\/yyyy") & "#"
End Function
Public Function quDateT(dt As Date) As String
' return formatted date with time
quDateT = "#" & Format(dt, "mm\/dd\/yyyy HH:NN:SS") & "#"
End Function
So, you don't have to care about the date and regional format, but for a in-line SQL insert command that you build in code? Yes, you MUST convert to USA format of mm/dd/yyyy.
So, you can display dates in any format. For forms, for reports - not a problem.
However, the ONLY exception here is your code that builds a insert statement. That date string format must be #mm/dd/yyyy#.
Or, ISO:
#yyyy-mm-dd#
So, either format is fine, but it is a hard and fast rule that you must conform to.
So, from a text box on a form, if not data bound, then you want to ensure that the text box is set as a date type text box (fomrat date).
then in code:
dim strSQL as string
strSQL = "INSERT INTO tblFun (BirthDate) " & _
"VALUES (#" & format(txtDate,"mm/dd/yyyy") & "#)"
currentdb.Execute strSQL
Or, if you have that helper function, then this:
strSQL = "INSERT INTO tblFun (BirthDate) " & _
"VALUES (" & qudate(txtDate) & ")"
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.
Please help me understanding what is happening
I have a visual basic app that returns data from an access database
i want to query a db field with dates with this format "01/12/2015" (Dec 1st)
to do so i have a datetimepicker
I have this record in my DB - 01/12/2015
when i update my code to include it datetimepicker value into a query it won't return nothing, if i put in 12/01/2015 it finds the record with 01/12/2015
my query looks like this:
dim dd as date
dd = datetimepicker.value
qrytxt = "select * from table where [Date] = #" + dd + "#"
if i put a msgbox to return qrytxt it returns
select * from table where [Date] = #01/12/2015#
but it still checks the wrong date against the db...
i also have this Imports System.Globalization and i read something about a CultureInfo but i couldn't put it to work in my query.
Is there anything else i need to include or do ?
why is this happening?
It's happening because the date literal is ambiguous and does not match the format that the Access Database Engine assumes for ambiguous date literals.
Using a date literal with dynamic SQL is the wrong approach anyway. The DateTimePicker returns a true System.DateTime value (which is always unambiguous) so you should just use that as part of a parameterized query:
Using cmd As New OdbcCommand("SELECT COUNT(*) AS n FROM Clients WHERE DOB = ?", conn)
cmd.Parameters.Add("?", OdbcType.DateTime).Value = DateTimePicker1.Value.Date
MessageBox.Show(cmd.ExecuteScalar)
End Using
Just force a format on your date value to create a string expression for the date:
Dim dd As Date
dd = datetimepicker.value
qrytxt = "select * from table where [Date] = #" + dd.ToString("yyyy'/'MM'/'dd") + "#"
I realise this is quite a common problem banded around forums, but I am at a loss as to what I've done wrong here.
I have a 'Current' table something like:
ID(uniqueidentifier)
Name(nvarchar max)
FileName(nvarchar max)
FileVersion(smallint)
CurrentVersionDate(datetime)
The CurrentVersionDate is entered using the GETDATE() function within SQLserver.
When a user hit's 'Create file' on my website, the details of this table are transferred to a 'History' Table. So I use a stored procedure to select * details from 'Current' and save each value as a variable (i'm using vb.net in a visual studio project). I'll show the date variable as this is the problem. I'm saving this as a 'date' variable:
Dim dDate as Date
dDate = dr("CurrentVersionDate")
Then I send these variables to another stored procedure to enter into the 'History' table:
Execute sp_InsertHistory '" & ID & "','" & strName& "','" & strFile & "','" & intVersion & "','" & dDate & "'"
I declare the date variable in the stored procedure as:
#Date datetime,
with the insert statement:
INSERT INTO History
(ID, Name, FileName, FileVersion, VersionDate)
VALUES
(#ID, #Name, #FileName, #FileVersion, #Date)
The 'History' table has the same setup as the 'Current' table. Stepping through the code, this error is caught:
Error converting data type varchar to datetime
Surely the variables isn't varchar? And even if it is, the string is surely in the correct format for SQLserver because I'm just selecting the date value SQL has given!
Any thoughts?
Since you are putting the date between quotes, is a varchar '" & dDate & "'", but I think the problem you have with the conversion could be that you are retrieving the date in your local format and you have to insert the date in the SQL Server in his own format.
Try inserting the date as a string in this format: dDate.ToString("MM/dd/yyyy HH:mm:ss")
the string is surely in the correct format for SQLserver
How do you know that? To be safe, I'd use string in unambiguous format: YYYY-MM-DDTHH:MM:SS, e.g. 2013-05-13T01:01:00.
How to compare a date/time via VBA with a date/time in an Access DB?
The query I use
adoRS.Open "SELECT * FROM currentpositions WHERE ((currentpositions.
[dateLT])=" & "#" & date_from_message & "#" & ")", adoConn, adOpenStatic,
adLockOptimistic
I only achieve to compare a date.
Anybody an idea?
Regards
Camastanta
adoRS.Open "SELECT * FROM currentpositions WHERE DateValue(currentpositions.
[dateLT]) = DateValue(" & "#" & date_from_message & "#)", adoConn, adOpenStatic,
adLockOptimistic
See, if the above helps.
DateValue extracts the date part from a given date/time. So, it can be used to compare date, ignoring the time part.
IMHO you should never build your SQL statements using string concatenation. Instead use parameterized SQL queries. This will save you from problems like the one you are facing with date/time comparison.
Does your system have dates in either yyyy-mm-dd or dd-mm-yyyy format? If so see Return Dates in US #mm/dd/yyyy# format to convert your dates in mm-dd-yyyy so Access can work with them properly.