updating a field where field cannot be less than zero - ms-access-2007

im using the following query to update my field
UPDATE tableName SET fieldName = fieldName-10
WHERE id=1;
and this is working fine in my case but i want that if the result is less than zero than query should not execute.
i've tried this
UPDATE tableName SET fieldname = fieldName-10
WHERE id=1 and fieldName>0;
but it returns with an error syntax error in your UPDATE statement.
thanx in advance

did you mean this? if pqty is the field of your table.
UPDATE tableName SET fieldname = pqty-10 WHERE id=1 and pqty>10;

Related

How to add text in the beginning of each record in a row in sql

i have the following data set in the leads column:
**leads**
1234567
1234534
1234543
i want it to be updated as
**leads**
LEA-1234567
LEA-1234534
LEA-1234543
You can use concat.
select
concat('LEA-', leads) as lead
from yourTable
Maybe you can try to use update script directly
update table set table.leads = concat('LEA-', table.leads)
UPDATE yourTable
SET leads = "LEA-" || leads
Update tablename
set leads='LEA-'+leads

SQL UPDATE with database values

It is possible to UPDATE sql column with the actual values ?
EXAMPLE :
UPDATE TEST SET example = {actual_value}, other = 'test'
I want to replace {actual_value} with the actual value without ask sql server before update.
Ok, found the answer thanks to #muasif80.
IF you want to update with actual value :
UPDATE TEST SET column_name = column_name

Where Clause is not working in Update but working in Select in Oracle DB

I am trying to execute the below update query
update custom_field cfe set cfe.field_value =:valueId where cp_entity_id = :cId
0 rows updated.
This is not updating any row but same where clause is working fine with select query and returns 1 row
select * from custom_field where cp_entity_id = :cId
Also, if i hardcode the value of cId parameter then update works fine but I am executing it from java program so it's not possible for me to hardcode the value
Also cp_entity_id column is a foreign key.
Try this, I faced similar issue.
Use this
select primary_key from custom_field where cp_entity_id = :cId query to find out primary key and Then use that primary key in your where clause of update query.
One of the ways to set parameter is explained here.
PreparedStatement ps = conn.prepareStatement(
"UPDATE Messages SET description = ?, author = ? WHERE id = ? AND seq_num = ?");
// set the preparedstatement parameters
ps.setString(1,description);
ps.setString(2,author);
ps.setInt(3,id);
ps.setInt(4,seqNum);
// call executeUpdate to execute our sql update statement
ps.executeUpdate();
ps.close();

Selecting rows based on a SQL Server "bit" column

In a SQL Server table, I have a BIT column and based its value, I need to update that table's other columns with some values. I tried this
UPDATE tablename SET Completed = GETDATE() WHERE CheckTaskAvailable = TRUE
but I get the error
Msg 207, Level 16, State 1, Server SQLDEV01, Line 1
Invalid column name 'TRUE'.
How to do this in a T-SQL query?
if you want to set as true try
Update table set columnName = 1 where ...
if you want to set as false try
Update table set columnName = 0 where ...
In addition to using the values 0 and 1, the T-SQL documentation for bit says that
The string values TRUE and FALSE can be converted to bit values: TRUE is converted to 1 and FALSE is converted to 0.
so these work, too:
UPDATE tablename SET bitcolumn = 'TRUE' WHERE ...
UPDATE tablename SET othercolumn = 'something' WHERE bitcolumn = 'TRUE'
I had a need to do something similar where I was looking to update a field based on whether a record existed or not existed in another table so I used above code (thank you RezaRahmati) and added:
Update table set columnName = 1 where ID IN (SELECT ID FROM other table)
or for false
Update table set columnName = 0 where ID NOT IN (SELECT ID FROM other table)
I really enjoy Stack Overflow it has really helped me learn.

Can I check if a variable is NULL within an Update's SET?

I have a stored procedure that uses a simple UPDATE with some variables passed to it. But I don't want to update those fields when their variables aren't null. This is essentially what my statement looks like.
UPDATE myTable
SET myColumn = #myColumn,
mColumn1 = #myColumn1
WHERE myColumn2 = #myColumn2
Is there anyway to apply some conditional logic within the SET? I have around 10 fields that need to be checked, so I wouldn't want to do an update per field or something like that.
Any ideas?
COALESCE is your friend. It returns its first non-NULL argument. I'm not actually sure from your narrative which way around you want things, it's either:
UPDATE myTable
SET myColumn = COALESCE(myColumn,#myColumn),
mColumn1 = COALESCE(myColumn1,#myColumn1)
WHERE myColumn2 = #myColumn2
Which keeps the current column's value if the column's not null, or
UPDATE myTable
SET myColumn = COALESCE(#myColumn,myColumn),
mColumn1 = COALESCE(#myColumn1,myColumn1)
WHERE myColumn2 = #myColumn2
Which keeps the current column's value if the variable is null.
Try to use coalesce function as below
UPDATE myTable
SET myColumn = coalesce(myColumn,#myColumn),
mColumn1 = coalesce(mColumn1,#myColumn1)
WHERE myColumn2 = #myColumn2
Above code updates your columns only when they are null. If they are not null the code sets the same value stored in the columns.
ISNULL ( variable , in case of null default value)
INFO