Missing equal sign on simple update statement using oracle - sql

I'm using apex oracle and running into missing equal sign error on this simple update statement. Can someone please point out what's wrong with this query?
update product_t
set qty-onhand = 11000
where productid = 28

qty-onhand is column qty minus column onhand.
You need to 'escape' the column name (or just use a better column name, without a minus sign in it).
update product_t
set "qty-onhand" = 11000
where productid = 28

Related

Using substring in SQL update statement

I'm writing a SQL query that updates the CUST_NAME column by appending the the word "CHECKED to it. The column is only 100 characters long however and I'm going to run into errors if I have an already long customer name and try appending CHECKED to it. So I want to use the Substring function in sql, but kind of stumped. I want to be able to substring the CUST_NAME field if it will go over 100 characters with the appended word. How can I do that?
Thank you
UPDATE CUST_INFO cust
SET CUST_NAME = (CUST_NAME||'_CHECKED')
WHERE process_timestamp = null;
Here is one way:
UPDATE CUST_INFO cust
SET CUST_NAME = SUBSTR(CUST_NAME, 1, 92) || '_CHECKED'
WHERE process_timestamp is null;
Also, if you want to update any records, then use is null rather than = null (the latter never evaluates to true).
Note: Not all databases have the left() function, you can use substr() or the equivalent instead.

Access 2010 SQL - UPDATE query not working

I need to create a query for updating a column in a table with values taken from another table and matching a field.
These are the 2 tables:
tblMain
ID Autonumbering
Key Text
Stat1 Integer
tblStat1
ID Autonumbering
Key Text
Freq Integer
I want to UPDATE the tblMain.Stat1 column with tblStat1.Freq value on each record in which tblMain.Key = tblStat1.Key.
I tried this syntax (found somewhere as an example)
UPDATE tblMain
SET tblMain.Stat1 = tblStat1.Freq
WHERE tblMain.Key = tblStat1.Key;
This doesn't work and returns an error on the 2nd row.
After some trials I found that the correct syntax (built with the Access query generator) is this:
UPDATE (tblMaibn INNER JOIN tblStat1 ON tblMain.Key = tblStat1.Key)
SET tblMain.Stat1 = tblStat1.Freq;
In this 2nd syntax, there is no trace of the WHERE condition.
Can someone help me to understand what's wrong with the 1st syntax.
Since I'm building a new table (the join), how can it work on tblMain?
As I said, I found the wrong syntax as an example of UPDATE statement.
Thank you in advance.
Bye,
Ivano
What is happening in your first query on the 2nd row, is that Access isn't aware of what tblStat1 represents in your query.
The reason your 2nd query is working is because it uses an inner join on the relevant key. In order for SQL to be aware of what record in tblMain relates to which record in tblStat1, you need to use a join.
You can see in the generated code that it is updating your desired table, but joining onto the second table. The where condition is redundant as you're updating every record.
In 1st syntax, you can change:
UPDATE tblMain
SET tblMain.Stat1 = (SELECT Freq
FROM tblStat1
WHERE tblMain.Key = tblStat1.Key)

How to store part of a field value in another column in SQL

So I'm trying to get a part of a value from a column and insert that part into another column - new column. BOTH columns are in the same table. So what i want should look something like this:
id newColumn oldColumn
1 12 123 some text
2 24 246 some text
....
I know how to get 12 and 24 using SUBSTR, but how do i enter the data for each row in the table. Should i be using self-join or something else?
First you have to add new col using following command:-
ALTER TABLE TAB_NAME
ADD COLUMN COL_NAME(VARCHAR(10));
After that execute this command:-
UPDAET TAB_NAME
SET COL_NAME = SUBSTRING(OLDCOLUMN, 1, 2);
I think this might help you.
No need to join, it's just a plain UPDATE:
update tablename set newColumn = substring(oldColumn from 1 for 2)
substring is ANSI SQL, some dbms have substr and other versions.
The question is why you are doing this? What do you expect to find in newColumn if someone later updates oldColumn to another value? Maybe you should have a view instead, where newColumn always has up to date values?
Please get into the habit of ALWAYS specifying the DB engine you are using... It helps us to help you - we can provide more relevant answers.
You might want to consider using a calculated column as opposed to storing the information again.
In SQL Server you could do something like this
ALTER TABLE YourTable
ADD new_column as SUBSTRING(old_column, 1, 2);
This way you don't need to insert or update this column it is always consistent with the original column. and you just use it in your select statement in the usual way.
select new_column from YourTable

update a part of a varchar column in sybase

I have a column in a table which has value
121Z000CH YY03+ W
is it possible using sybase query language to update this column only in the characters 14 to 2 using substring? i.e. i need to replace 03 with some other number say 05.
Currently i'm trying the below statement which is giving me syntax error. Please correct me in this.
update recordtable
set substring(recordtable.column,14,2) = '05'
you can use below query for the same -
update recordtable
set column = substring(recordtable.column,1,13)
||'05'||substring(recordtable.column,16)

Correct syntax for SQL UPDATE where value to be set is a SELECT

Can someone set me straight on the correct syntax for this statement?
I am trying to update a new field in table rounds with an average of a field in table holes where rounds.id is stored as a foreign key.
UPDATE `rounds` SET drivingDistance_avg_per_hole =
SELECT SUM(holes.drivingDistance)/COUNT(holes.drivingDistance) as avg
FROM `holes`, `rounds`
WHERE holes.drivingDistance != ''
AND holes.round = rounds.id
Sure would appreciate the lesson!
Depends on your database. Standard SQL doesn't have the FROM syntax with UPDATE and some databases don't support it. In that case this ought to work:
UPDATE `rounds` SET drivingDistance_avg_per_hole = (
SELECT SUM(holes.drivingDistance)/COUNT(holes.drivingDistance) as avg
FROM `holes`
WHERE holes.drivingDistance != ''
AND holes.round = rounds.id )
The key points are that the subselect has to be in parentheses, and that you don't want to include the destination table in the subselect because then you're selecting all rows of the table whereas you only care about the rows of holes that match the corresponding row from rounds.