sql: how to select all records with field set to 0 and set the value of that field to one - sql

Is this possible in one SQL statement?
I pointer to a nice SQL tutorial on the subject is also appreciated.
I know I can use a command like SELECT * FROM MYTAB WHERE MYFIELD = 0
and then use a server script to go through the result and UPDATE MYFIELD
of the result rows.

Wouldn't this work?
UPDATE MYTAB SET MYFIELD=1 WHERE MYFIELD=0
Edited to Add:
If you are interested in SQL tutorials, a lightweight one that has the nice feature of allowing you edit and run SQL commands in the webpage to see how they work is from w3schools here: http://www.w3schools.com/sql/

If you can execute a query to read the data you should also be able to write the data;
UPDATE MYTAB SET MYFIELD = 99 WHERE MYFIELD = 0
UPDATE (Transact-SQL)
UPDATE Statement (Oracle)
UPDATE Syntax (MySQL)
UPDATE Statement (PostgreSQL)

UPDATE query..
UPDATE TABLE
SET FIELD = 1
WHERE FIELD = 0
I think it should be as simple as:
UPDATE MYTAB
SET MYFIELD = 1
WHERE MYFIELD = 0
Hope this helps!

Why do you want to execute a SELECT query when your purpose can be solved by a simple UPDATE query? It seems that you want to switch the flag column of your table.
how to select all records with field set to 0 and set the value of
that field to one
Use below query.
Update MYTAB
set MYFIELD=1
Where MYFIELD=0
This query will find all records where MYFIELD is set to 0 and will update all those records with value 1.
You can browse thru following links to learn advance SQL.
SQLCourse2.com
SQL Tutorial
Tutorials Point

Related

Limit a value in SQL

Not that familiar with SQL.
I have the follow SQL :
UPDATE my_Table
SET num = num + 1
WHERE id = 1
I would like limit that num column that it won't exceed a threshold, assuming 100. If num will reach 100, I would like it to stay 100 and will not increase.
I assume if statement should be included here somehow. but cant figure out how.
I'm seeing answers that would solve your problem from an update statement, all fine whatever way it is done.
Until someone creates a statement that doesn't adhere to your constraint, like a new co-worker, someone sleepy that forgets it or whatever reason. You should probably know about other options to force this rule.
You could create an SQL constraint that checks that num is never set to more than 100. That way any update or insert statement that makes this value more than 100 would result in an error rather than doing the insert or update.
Another way to force this rule "under the hood", not really beginner level, would be to create an update and insert trigger that would check for num being more than 100 and reset it to 100 in that case. That wouldn't result in an error when you run the insert or update statement. I don't have a good link for ANSI-SQL triggers, but each RDBMS has good documentation.
You can use a CASE expression:
UPDATE my_Table
SET num = CASE WHEN num+1 >= 100 THEN 100 ELSE num+1 END
WHERE id=1
try:
UPDATE my_Table SET num=num+1 WHERE id=1 AND num<=99
in that case you can use IIF for shorter version
UPDATE my_Table SET num=(IIF((num+1)>100,num,num+1)) WHERE id=1

SQL query formulation

I am trying to edit a database using phpMyAdmin.
The table is 'jos_k2_items' and where 'CatID' = '1616' I need to set all 'created_by' values to '97'.
Im just not sure how I would write that in SQL!
Many thanks
UPDATE jos_k2_items set created_by='97'
WHERE CATID = '1616'
There is execute sql in phpmyadmin so you can execute this there.
UPDATE jos_k2_items SET created_by="97" WHERE CatID="1616"
That should do what you want

Set a column equal to a value in Oracle SELECT

I want to Select a column and set it equal to 0. In SQL I just do this: SELECT Dealer_Fee = 0.
Do I have to use an update? When I try the same thing in Oracle I get "FROM keyword not found where expected."
I am not sure what do you want to do and in which SQL dialect the construction you mentioned works. If you just want to retrieve some value and place it in a named column in Oracle you have to use DUAL table. Try this:
SELECT 0 AS dealer_fee FROM dual;
On the other hand, if you ment T-SQL and placing a value into variable you need to use PL/SQL SELECT INTO clause, like that:
SELECT 0 INTO dealer_fee from dual;
If not try to explain in more detail what are you trying to achieve.
1) In Oracle you must specify FROM clause within SELECT, is not like MS sqlserver where you can omit a FROM clause.
2) If you want to update one specific value, you must use UPDATE clause instead SELECT.
hth
You should use the Update statement. Please visit this oracle reference which explains the Update statement in detail: Update Statement
Take a look at the reference for UPDATE in Oracle:
UPDATE <table_name>
SET <column_name> = <value>

How to write a update SQL using a select SQL

I want to write a update SQL statement, but one conidtion of this statement is the result from a select SQL statement, and I also want to return the result of the select SQL statement.
Like this: update ... set ... where id = (select id from ...)
I want to return the value of id back.
Does anybody know how should I do this?
Thanks in advance!
I don't believe that's possible in one statement. Update then query (select) the new value, or query the value first, and then submit an update.
Alternative would be a stored procedure on the database, which executes the multiple queries and returns the result for you.
This is not possible in all Java database frameworks that I know. Probably you need to separate your query and update in Java.
I don't see any problem in using a subselect in a WHERE clause of an update statement.
For the second request, getting back the value of id, I know this is possible in DB2, and maybe others implement that syntax too:
SELECT id FROM FINAL TABLE (
update ... set ... where id = (select id from ...)
)
This works also for INSERT and DELETE statements. (See the documentation.)
Update statements won't return the updated datasets. The select in that case would be a subselect that isn't directly accessible.
You'd thus have to use at least two queries:
select the ids you want
call the update query passing the previously selected ids as a parameter

SQL Update a field by removing a part of it

In my work, I am stuck with a SQL problem.
I have a field in one of my table which contains strings of the form "%_abc". I want to update this column by removing "_abc" at end of every entry. Is there a nice way to get this done using SQL?
Thanks,
Anil.
update table1 set field1 = substr(field,1,length(field1)-4) where ...
HTH
If your database is ANSI SQL-92 compliant, you can use:
UPDATE myTable SET myColumn = TRIM(trailing '_abc' FROM myColumn);