I have the query below in access and wanted to use it in VBA...
SQL = "UPDATE Sales SET Sales.order_date = CDate(Right([data],2)+" / "+Mid([data],5,2)+" / "+Left([data],4));"
I keep getting error due to the way I am building the string to then execute.
I have tried to put the
& "/" &
to build the string but get an error when executing the SQL...
How should I build the string?
Thanks!!!
You would use DateSerial for this if data is a field of your table:
SQL = "UPDATE Sales SET Sales.order_date = DateSerial(Left([data],4), Mid([data],5,2), Right([data],2))"
just escaped the / as '/' and worked...
SQL = "UPDATE Sales SET Sales.order_date = CDate(Right([data],2)+ '/' + Mid([data],5,2) + '/' + Left([data],4));"
Related
I am trying to execute a query in my Oracle database with a WHERE clause:
java.sql.Date today = new java.sql.Date(System.currentTimeMillis());
String sql = new StringBuilder("SELECT *")
.append("FROM USERNAME.MY_TABLE m")
.append("WHERE m.A_DATE_COLUMN = ?").toString();
PreparedStatement ps = con.prepareStatement(sql);
ps.setDate(1, today);
ResultSet rs = ps.executeQuery();
But I keep getting error:
SQL command not properly ended
when executeQuery() is called. I know it has to do with setting my parameters because the query runs fine and returns the proper data if I don't include the WHERE clause. The table I am querying from has just the one column which is of type DATE.
I tried using setString instead of setDate and adding a semicolon after ? inside my String I use to create my query, but it's my understanding I don't want to include a semicolon.
You need to add spaces:
String sql = new StringBuilder("SELECT * ")
.append("FROM USERNAME.MY_TABLE m ")
.append("WHERE m.A_DATE_COLUMN = ? ").toString();
After the append, your query looks like below.
SELECT *FROM USERNAME.MY_TABLE mWHERE m.A_DATE_COLUMN = ?
Add the spaces properly.
String sql = new StringBuilder("SELECT * ")
.append("FROM USERNAME.MY_TABLE m ")
.append("WHERE m.A_DATE_COLUMN = ?").toString();
Trying to convert a number value to decimal, with two decimals (,00), in vba when selecting value from MS Access table.
The Quantity column is of type number and the code Im using to format it is
Dim rstBody As DAO.Recordset
Dim orderBodyQuery As String
orderBodyQuery = "SELECT distinct CONVERT(Decimal(9,2), Quantity) FROM " + mainTable + " WHERE [" + uniqOrderColumn + "] = """ + order + """"
Set rstBody = CurrentDb.OpenRecordset(orderBodyQuery, dbOpenSnapshot)
This results in the error:
Undefined function 'CONVERT' in expression
As the error describes Im guessing Im using the wrong syntax here (SQL Server) but I can't find how to do this.
Please help
For display (text) it would be:
Format([Quantity], "0.00")
To retrieve numeric values rounded by 4/5 to two decimals, it would be:
CCur(Format([Quantity], "0.00"))
To set the Format property, use:
"0.00", or just "Standard".
Surprise! Access doesn't use T-SQL.
I think you want the FORMAT() function.
What are differences between access sql
techonthenet.com/access/functions/
First time using Access and wanted to make an update query that uses a variable for its table name. Now, I've gotten myself into a web of nothing good. When I get to the part the SQL code is needed for, I get Runtime error 3075 - Missing operator in '(((" + enteredid + ".todayDate)=Format(Now()','""Short Date"")))' I've never coded in SQL, so I have no clue what operators are needed.
My code:
strSQL = "UPDATE " + enteredid + " SET " + enteredid + ".signIn = Format(Now(),""Short Time"") WHERE (((" + enteredid + ".todayDate)=Format(Now()','""Short Date"")));"
My suggestions:
You can avoid the whole Format() issue in the WHERE clause by using the Date() function instead of trying to extract just the date part of Now().
Since you are doing an UPDATE on a single table you can just use the field (column) names without the TableName. prefix.
To make your code more robust, enclose the table name in square brackets so it won't crash if the table name contains spaces or other "funny" characters.
So, the revised code would look more like this:
strSQL = _
"UPDATE [" + enteredid + "] SET " + _
"signIn = Format(Now(),""Short Time"") " + _
"WHERE todayDate = Date()"
I am new to VB.Net 2010. Here is my problem: I have a query that uses a combo box to fetch many items in tblKBA. All IDs in the MS Access database are integers. The combo box display member and value member is set to the asset and ID of tblProducts.
myQuery = "SELECT id, desc, solution FROM tblKBA WHERE tblKBA.product_id = '" + cmbProducts.SelectedValue + "'"
In addition to getting items from the KBA table, I want to fetch the department details from the department table, possibly done in the same query. I am trying to do it in two separate queries.
myQuery = "select telephone, desc, website from tblDepartments where tblDepartments.product_id = tblProducts.id and tblProducts.id = '" + cmbProducts.SelectedValue + "' "
All help will be appreciated!
Change the '+' to a '&' then the compiler would be happy.
try adding .toString to cmbproducts.selectedvalue or do "tblKBA.product_id.equals(" & cmbProducts.selectedValue.toString & ")"
1.) Don't use string concatenation to build your query. Use parameters.
2.) I am guessing that tblKBA.product_id is a double and not a string, so don't put quotes around it.
myQuery = "SELECT id, desc FROM tblKBA WHERE tblKBA.product_id = ?"
3 things. Test your value before building the select statement. Second, Use .SelectedItem.Value instead of .SelectedValue. Third, protect yourself from sql injection attack. Use parameters, or at the very least check for ' values.
If IsNumeric(cmbProducts.SelectedItem.Value) = False Then
'No valid value
Return
End If
myQuery = String.Format("SELECT id, desc FROM tblKBA WHERE tblKBA.product_id = {0}", cmbProducts.SelectedItem.Value.Replace("'", "''"))
code:
string query1 = #"UPDATE global_mapping set escape_id = " +
dataGridView1.Rows[i].Cells[2].Value + ",function_id = " +
dataGridView1.Rows[i].Cells[3].Value + ",function_name = '" +
dataGridView1.Rows[i].Cells[4].Value + "',parameter_name = '" +
dataGridView1.Rows[i].Cells[5].Value + "',parameter_validity = '" +
dataGridView1.Rows[i].Cells[6].Value + "',statusparameter_id = " +
dataGridView1.Rows[i].Cells[7].Value + ",acb_datatype = '" +
dataGridView1.Rows[i].Cells[8].Value + "',data_type_id = " +
dataGridView1.Rows[i].Cells[9].Value + ",bit_size = " +
dataGridView1.Rows[i].Cells[10].Value + ",validity_status ='" +
dataGridView1.Rows[i].Cells[11].Value + "',validity_func = '" +
dataGridView1.Rows[i].Cells[12].Value + "'WHERE global_mapping.parameter_id =" +
dataGridView1.Rows[i].Cells[1].Value + "";
OleDbCommand cmd1 = new OleDbCommand(query1, conn);
cmd1.ExecuteNonQuery();
code ends:
When I execute the above code I get an error stating "Syntax error in Update statement".
Can someone please tell me how to resolve this?
It looks like you need to add a space before your WHERE clause.
Hope this helps,
Bill
Wow. Can we say... SQL Injection?
Try using Parameters. Not only will you protect yourself, but your SQL will become MUCH more readable.
Never use string concatenation for building SQL queries. Use SQL parameters.
Yikes!
Please provide the final query1 value and try to format it so we can get a better picture of it. My guess is a missing ' or something.
I'd say you're missing some quotes in there but your code is such a pig-sty I can't tell. If you won't fix your code then at the minimum give us a dump of query1 so we can read your actual query.
And use parameters or stored procedures like the previous responses said. All it takes is one of your variables to get overwritten with something nasty and your server will be wide open to anyone deleting your tables or worse.
Even if this is a local "safe" database you should unlearn your bad habits now.
Put
Console.WriteLine(query1)
before OleDbCommand cmd1 = new OleDbCommand(query1, conn);
See the value of query1 printed to console window.
Does the SQL Statement look OK? I guess not - you will now be able to find a field which is non-numeric and is blank in the grid.
And, use parameters as others have said.