How to delete datetime value using update? - sql

I have a row with datetime. What I want to achieve is delete the datetime in just a blank. Is that possible with using update? If possible I don't want to delete and re-insert the data because this is what I tried and it so inefficient.
Here is my table:
+++++++++++++++++++++++++++++++++++++
+ ID + Name + AppvlDt +
+++++++++++++++++++++++++++++++++++++
+ 1 + JB + 02/11/2019 00:00:00 +
+++++++++++++++++++++++++++++++++++++
I want to delete the AppvlDt into just a plain blank. Is that possible using Update?

You can certainly null out your datetime field, e.g.
UPDATE yourTable
SET AppvlDt = NULL
WHERE ID = 1;
Whether or not doing this actually makes sense depends on other things though.

Just run this DML script.
update table Set AppvlDt = ''

Related

Processing SQL Updates

I have a table with Table name, Id, Column Name and Value
I need to run these into a Azure SQL Database as updates.
The tables are all different with different columns for the update.
The only way I could think of is RBAR, but there has to be an easier way to process this, either SQL or maybe even data factory?
Example of the data is:
Table Name Id Column Name Value
Table 1 1234 column1 1
Table 1 1234 column2 2022-01-02
Table 2 4321 column6 2144
Table 2 4321 column12 2022-01-02
The column name could be any column in the table with a update value.
As mentioned I have tried Row By Agonising Row but, as you would expect, WAY too painful as I have approx 161K rows that need processing.
You'll need to build an SQL string for each row and run that string as it's own command.
One thing I would do as part of this is pull the table/column information from Information_Schema.Columns. This will give you the same text you already have, but it ensures (in a safe way) that the data in your source table really is a valid object in your database, and not an SQL injection attempt. I would also use sp_executesql for the final query, to include the new value in a safe way.
So the main query would like looks this:
SELECT d.ID, d.Value,
'UPDATE ' + QUOTENAME(c.Table_Name) + ' SET ' + QUOTENAME(c.Column_Name) + ' = #new_value WHERE ID= #RowID' as SQL
FROM [MyData] d
INNER JOIN Information_Schema.Columns c ON c.[Table Name] = d.Table_Name
AND c.[Column Name] = d.Column_Name
And the loop would then take each row and do something like this:
-- #SQL, #ID, and #Value from the row
Execute sp_executeSql #SQL, '#new_value varchar(50), #id int', #new_value = #Value, #RowID = #ID
I skipped over the part where you loop through the rows because you have many options here, and which is best depends on your specific situation: do you want a cursor? Client code? Hard to know.
The trick here is we don't know the data type of the new value... and it certainly looks like you're mixing data types in there. You know that, for example, storing dates in varchar columns is really bad, right?

Get last updated column from an update action in SQL Server

UPDATE sms.customerOtp
SET validateCounts = validateCounts + 1
WHERE customerID = '123'
After this update I need to run a select statement that should return the last updated value of validateCounts.
Is there any functions similar to SCOPE_IDENTITY to get the last updated value of a non-identity column?
You can use the OUTPUT clause:
UPDATE sms.customerOtp
OUTPUT validateCounts
SET validateCounts = validateCounts + 1
WHERE customerID = 123;
Normally, I would put the result into a table variable, but you can use it without a table variable.

SQL Update if null add 1 otherwise add 1 to current value

I have a query that is updating a field in my table. It could be the case that that column to be updated can be NULL if that's the case i'd like to add 1 to that cell. Otherwise i'd like to add 1 to the current value of that field.
UPDATE SET Scheduled = Scheduled + 1
Would work except when cell's have NULL as their value, it does not add the 1 value.
You can use this.
UPDATE table SET Scheduled = ISNULL(Scheduled,0) + 1
You could use CASE expression:
UPDATE table_name
SET Scheduled = CASE WHEN Scheduled IS NULL THEN 1
ELSE Scheduled + 1
END
WHERE ...;
Although you can easily do this in the update:
update t
set scheduled = coalesce(scheduled + 1, 1)
where . . .;
I would suggest removing the need for it, by defaulting the value to 0. I suspect that will make sense in your context. If you have data in the table:
update t
set scheduled = 0
where scheduled is null;
alter table t alter scheduled int not null default 0;
(Note: You can also use with values in the alter, but the update clearly shows the intent.)
Update yourTable set yourColumn=(coalesce(yourColumn,0)+1)
Or you can use
Update yourTable set yourColumn=(nullif(yourColumn,0)+1)

Tsql, is there a way to update row using an existing field in the row as part of a string in another field in the same row?

I am trying to update rows in my sql server 2008 r2 web edition database. I need to use a field that is already in the row and concat? that field into a string in another field in the same row.
This is for a real estate database where i have a column called mlsnumber, i need to use the mlsnumber field as part of a string in another column, same row called photoname.
In this case mlsnumber =1234567, i need to add/update row so photoname=photo+mlsnumber+'-01.jpg' (photo1234567-01.jpg)
Any help is greatly appreciated.
Ken
assuming table is called photos
update photos set photoname = 'photo' + mIsnumber + '-01.jpg'
if misnumber is not a varchar you could do
update photos set photoname = 'photo' + cast(mIsnumber as varchar(10)) + '-01.jpg'
if misnumber is null then the result is null so it could be
update photos set photoname = 'photo' + coalesce(cast(mIsnumber as varchar(10)),'') + '-01.jpg'
You can also add a where clause if you wish to filter which records you update.
Additionally if this is always going to be the case you could look at using a view to retrieve the data rather than storing it in the table or alternatively using a calculated field in the table.
update table1 set
photoname = 'photo' + cast(mlsnumber as nvarchar(max)) + '-01.jpg'
where mlsnumber = 1234567
you also could just select this in the view:
create view vw_table1
as
select
*,
'photo' + cast(mlsnumber as nvarchar(max)) + '-01.jpg' as photoname
from table1
Update tablename set photoname = 'photo'+mlsnumber+ '-01.jpg'
This will update every row, so be careful with the update. You may want to use begin transaction, run the query, check results and then commit or rollback, if you want to test your update statement in isolation.
If you need to convert mlsnumber, see link http://msdn.microsoft.com/en-us/library/ms187928.aspx
Is this what you want?
UPDATE t
SET photoname = 'photo' + CAST(misnumber AS VARCHAR(255)) + '-1.jpg'
WHERE misnumber = 1234567;
If you want to do it for all rows, then just remove the where clause.

Update SQL for a 100 column row in a table based on another table with the name column names

One row in a table got mistakenly overwritten. I have a backup of the table and need to update a single row in the main table with the backup data. The trouble is that the table has almost 100 columns. Is there an easy way to do this in SQL Server so that I do not have to manually write out this:
set maintable.a = backuptable.a,
maintable.b = backuptable.b,......etc.
all the way up to the 100th column. I would think that SQL Server Management Studio could know that I want to update the fields with the same-named fields in the other table. Is there a way to do this?
Thanks in advance.
INSERT INTO MainTable
SELECT *
FROM BackUpTable
WHERE RowID = GoodRow
DELETE FROM MainTable
WHERE RowId = BadRow
Just insert the "good" row and delete the "bad" row.
If there is a PK constraint you can delete/insert in the opposite order.
It would also be wise to enclose this in a TRANSACTION.
In SSMS you can drag-n-drop the columns node in the Object Explorer to the Query window. It will generate a comma-separated string of all the columns.
> declare #aa varchar (max) set #aa = ''
>
> select #aa =
> (case when #aa = ''
> then column_name
> else #aa + coalesce(', ' + column_name+'= '+column_name, '')
> end) from amicus.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'People' print #aa