Updating table in SQL Server - sql

I am running my query, but it does not work. The cause is the value of column is null and I'm trying to update it with a integer value.
This is my query.
The default value of sdoc is null
update expenese
set sdoc = sdoc + '200'
where expenese.date = '2016-03-26';

UPDATE EXPENESE
SET SDOC=ISNULL(SDOC,0)+200
WHERE DATE='2016-03-26'

Related

How to update value to be the same one with addition?

Is there a way in SQL to update an existing value to be himself with a new value ?
For example :
I have a value "992", and I want to add "135" so it will be "992135".
I tried doing this :
string sql1 = "UPDATE [Table1] SET OldValue = OldValue " + ValueToAdd;
But it just sums it up (ends up to be 992+135 = 1127).
Thanks
You are looking either for string concatenation or arithmetic, depending on the type of the column. The standard concatenation operator is ||, although many databases support the concat() function:
UPDATE [Table1]
SET OldValue = CONCAT(OldValue, ValueToAdd);
Or, as arithmetic:
UPDATE [Table1]
SET OldValue = OldValue * 1000 + ValueToAdd;

UPDATE same column multiple times in query

Is there a specified behavior for updating the same column 2+ times in the same UPDATE query, as follows?
UPDATE tbl SET a = 5, b = 'something', a = 6 WHERE c = 'whatever';
Is there a standardized behavior for this, or might it vary between flavors of SQL (e.g. it is "undefined behavior")? A cursory test with sqlite seems to indicate they are executed left-to-right, so the last column value will be the resulting one, but that doesn't imply that will always be the case.
Edit: The reason I'm trying to do this is I'm testing some SQL injection for a class project. One of the fields in an UPDATE is unsafely injected, and I'm trying to use it to overwrite previously SET fields from the same query.
This isn't exactly the answer you're looking for but assuming that the text "something" is a field you are passing in and it isn't parameterized or escaped you may be able to do this. This all depends on how the query is being built and what database it is being run against.
UPDATE tbl SET a = 5, b = 'something'; UPDATE tbl set a = 6;--' WHERE c = 'whatever';
by entering the following in the user input
something'; UPDATE tbl set a = 6;--
This assumes that the query is built dynamically something like this
var query = "UPDATE tbl set a = 5, b = '" + userInput + "' WHERE c = 'whatever'";
Here is a relevant question: How does the SQL injection from the "Bobby Tables" XKCD comic work?

Can't get update statement in SQL Server to work

I'm attempting to execute this SQL Update statement and it's not working. Does anyone know why ?
update dbo.EBSTable
set CommandField = replace(CommandField, '%APPL.mbm_aging_file', '%APPL.mbm_aging_file)')
where Command like '[%]APPL.mbm_aging_file'
Basically, I'm just trying to add a ")" to the end of the data appearing in the CommandField field where the value is %APPL.mbm_aging_file (The "%" actually appears in the data).
I discovered my where clause was inadequate (like me with SQL). It should read
update dbo.EBSTable set CommandField = replace(CommandField, '%APPL.mbm_aging_file', '%APPL.mbm_aging_file)') where Command like '%[%]APPL.mbm_aging_file%'
That statement worked.
update dbo.EBSTable
set CommandField = '%APPL.mbm_aging_file' + ')' -- or set CommandField = '%APPL.mbm_aging_file)'
where Command = '%APPL.mbm_aging_file'
You can do this, as you only need to add ) at the end only for this specific case.

Update Based on Select

I am attempting to update one column of a table based on data present in other records of the same table. All records either have the same date in the "CurrentDate" field or are null. I want to change those with null values to be the same as the rest of the fields.
Here is my code, but I am getting a syntax error:
Public Sub RiskVisual()
Dim db As DAO.Database
Set db = CurrentDb
---
DoCmd.RunSQL "UPDATE Hold3 SET CurrentDate = (SELECT CurrentDate FROM Hold3 LIMIT 1) WHERE CurrentDate IS NULL;"
End Sub
Thanks in advance for your help.
In MS Access the "TOP 1" works better than "LIMIT 1". You will also want to specify when seeking for the top 1 that the top 1 that is not null. Try something like this:
UPDATE Hold3 SET Hold3.CurrentDate = (SELECT TOP 1 Hold3.CurrentDate FROM Hold3 WHERE (((Hold3.CurrentDate) Is Not Null))) WHERE (((Hold3.CurrentDate) Is Null));

Updating Table with System Date

My question is very simple:
I have a column named "DateProcessed". Whenever User Clicks a Button, the column should be updated for each row with the current System.Date.
Here is my code:
update dbo.JobStatus SET DateShipTransmitProcessed = ???? WHERE JobTableId = #JobTableId
What should go in ????. Thanks for your help!
The ANSI standard would be to use current_timestamp, which should work for MySql, SQL Server, and any other ANSI compliant RDBMS.
update dbo.JobStatus SET DateShipTransmitProcessed = current_timestamp WHERE JobTableId = #JobTableId
If you are on SQL server..
update dbo.JobStatus SET DateShipTransmitProcessed = GetDate() WHERE JobTableId = #JobTableId