Ive looked around Google and can't find any working code. I want to query my database to return just results from a certain date. But the datetime column in my database has both the Date and the Time.
How would I go about doing this.
today = 15/08/2013
An example of one of the database values = 15/08/2013 02:13:18 PM
And here is my code at the moment
'Connect to database
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.ACE.OLEDB.12.0"
conn.Open Server.MapPath("/nightclub_photography/data/database/jamsnaps.mdb")
'Get the current date
today = Request.Cookies("sdate")
response.write today
sql = "SELECT * FROM sales WHERE saleDate =" & today
Set rs = conn.Execute(sql)
if rs.EOF then
response.write "Error!"
end if
Dates are passed as strings. Wrap them in single quotes.
sql = "SELECT * FROM sales WHERE saleDate = '" & today & "'"
Hmm, I used this in some other code which seemed to work.
DATEVALUE(columnName)
If you know that today consists of only the date, you could concatenate the time and do something like this:
sql = "SELECT * FROM sales WHERE saleDate >= '" & today & " 00:00:00' AND today <= '" & today & " 23:59:59'"
You could use one of VBScript's date functions to guarantee the contents of today are only the date if you want to be sure. Something like:
today = FormatDateTime(today,vbShortDate)
Related
I have an SQL update command that I can't get to work. i want it to update the fields where the date is equal to the current date. It works for my INSERT and SELECT statements. But I get a missing operator error when using it for my Delete statement below.
Cmd.Connection = conn
Cmd.CommandText = "UPDATE tbl_Expenditure SET E_Stock =" & NewEStock & "," & "E_Total =" & ETotal & "WHERE [E_Date] = #" & thisMonth & "/" & Day & "/" & thisYear & "#;"
Cmd.ExecuteNonQuery()
Ive tried searching this site as well as others and can't seem to find an answer.
This is my error
Syntax error (missing operator) in query expression '95WHERE [E_Date] = #4/1/2015#'.
Thanks for any help
Using conn As New SqlConnection("connection string here"), _
cmd As New SqlCommand("UPDATE tbl_Expenditure SET E_Stock = #Stock, E_Total = #Total WHERE [E_Date] = #Date;", conn)
'Guessing at column types here
cmd.Parameters.Add("#Stock", SqlDbType.Int).Value = NewEStock
cmd.Parameters.Add("#Total", SqlDbType.Decimal, 8, 2).Value = ETotal
cmd.Parameters.Add("#Date", SqlDbType.DateTime).Value = New DateTime(thisYear, thisMonth, Day)
cmd.ExecuteNonQuery()
End Using
This fixes a HUGE security issue in the code, it has a performance benefit in allowing Sql Server to cache your query plan, and it solves your problem in that is makes it much easier to spot silly syntax mistakes like the missing space in front of the WHERE clause.
I have FrmDt and ToDt as datetime column in sql server2000. I am comparing dates in these columns with two datetimepicker like this-
SQL query :
SqlCmd = New SqlCommand("SELECT DISTINCT AllwnsDedId FROM vwGetEmpCntrctWisePaymnt
WHERE Type=1 AND Emp_ID='" & txtEmpID.Text & "'
AND FrmDt>='" & dtp1dtp1.Value.Date & "' AND ToDt<='" & dtp2dtp1.Value.Date & "'", Sqlconn)
In Query Analyzer, I tried :
FrmDt>='01/21/2015' AND ToDt<='01/21/2016'
Its giving me correct output. But I am not getting the output in Vb.net? Anything missing?
In order for this to work reliably you need to use proper Date datatypes. Change your SQL to something like;
WHERE FrmDt>=#FrmDt AND ToDt<=#ToDt
then add 2 paramater objects to the command, one for each date. Your code should end up something like this;
SqlCmd = New SqlCommand("SELECT DISTINCT AllwnsDedId FROM vwGetEmpCntrctWisePaymnt
WHERE Type=1 AND Emp_ID='" & txtEmpID.Text & "'
AND FrmDt>=#FrmDt AND ToDt<=#ToDt", Sqlconn)
SqlCmd.Parameters.Add("#FrmDt", SqlDbType.DateTime).Value = dtp1dtp1.Value.Date
SqlCmd.Parameters.Add("#ToDt", SqlDbType.DateTime).Value = dtp2dtp1.Value.Date
I am working on designing a asp form which will query an Access database (mdb). The outcome of the form is to query data results between a date range (date from and date to), and output the results on the asp page below. When I try and run the query I receive the following error:
Microsoft JET Database Engine error '80040e07'
Syntax error in date in query expression 'name = 'firstname.lastname' And date = ##'.
Note: firstname.lastname correspnds to the real person.
The code it points to two lines which appear as follows:
rstDATA.open "Select * From xxx Where name = '" & username & "' And date = #" & request.Form("ddDate") & "#",cnn, adOpenKeyset, ,adLockReadOnly %>
rstDATA.open "Select * From xxx Where name = '" & username & "' And date = #" & request.Form("ddDate") & "#",cnn, adOpenKeyset, ,adLockReadOnly %>
where xxxx corrsponds to the table.
It seems there is no value for ddDate provided. Are you sure you have a html element in your form that has a name attribute with "ddDate" as it's value?
I have a routine to pull data from my database (nothing fancy). I want it to pull rows in which the "StartDate" column is some date in the future. The code below pulls all the rows instead of just the future ones.
I know it must be something silly that I'm doing but I can't figure it out.
Thanks in advance,
Craig
CODE:
Printda = New OleDbDataAdapter("SELECT * FROM tblShows WHERE StartDate > " & Format(Now, "Short Date"), cn)
Updated Code:
Printda = New OleDbDataAdapter("SELECT * FROM tblShows WHERE StartDate > '" & Now.ToString("Short Date") & "'", cn)
Corrected Code:
Printda = New OleDbDataAdapter("SELECT * FROM tblShows WHERE StartDate > Now()", cn)
Try this:
"SELECT * FROM tblShows WHERE StartDate > #" & Now.ToString("MM/dd/yyyy") & "#"
In SQL Server this is the default date format. In Oracle is YYYY-MM-DD. Use the format that fit your needs, or use parameters if you don't want to care about formatting and give your querys security.
Or you could simply use proper function to get the actual date:
"SELECT * FROM tblShows WHERE StartDate > now()"
I have a database (MDB, Access) and I connect it to my program using an OLE object,
now I have in the db a column filled with dates (ddmmyy),
I want to search and view (in Data grid view) all the fields that has a date before a Specific Date that I define .
the code of search that I used is :
SQLstr = "SELECT * FROM tb WHERE anomber = '" & TextBox1.Text & "'"
What do I have to do?. Thanks.
use the parameters to pass the date to the query, it's more saver(no sql injection) and more perfect(it will convert the date format to the correct format)
SQLstr = "SELECT * FROM tb WHERE anomber < ?"
Command.Parameters.Add(New OleDbParameter("#anomber", TextBox1.Text))
Command.CommandText = SQLstr
Edit:
if the anomber field is the date field so the user can use < instead of =.
the OP question not clear about what he wants.
Edit2:
after executing the command you should assign the results to the grid that you are using to display the data.