updating a column by a constant value - sql

I'm having difficulty with a simple issue.
I have a number column a typical value is say 192.123.
I want to add/subtract a single value to it, say -100.123.
I assume it is
Update table_name
SET column_name = (the original value - 100.123)
How should I go about this?

You need to use '+' instead of '-'. So that it will add or subtract based on the input value.
Update table_name SET column_name = (column_name + Value_to_add_subtract)

use the column_name in original_value
Update table_name SET column_name = (column_name - 100.123)

use columnName instead of original value
update table_name
SET column_name = (column_name - 100.123)

Related

Update value in DB with comma [,]

I would like to update the column value as '12345,123456'. Earlier it was '12345123456'. I am using the following query
UPDATE TABLE_NAME
SET COLUMN_NAME = '12345,123456'
WHERE TC_ID = 'ABCD'
But it is showing error as Error converting data type varchar to float.
Please help.
Unless your internationalization settings say otherwise, commas are not allowed in numeric values. And apparently, they are not on your machine.
Use a period:
UPDATE TABLE_NAME
SET COLUMN_NAME = 12345.123456
WHERE TC_ID = 'ABCD';
Note that no single quotes are needed for a number.
If you are passing in a string, you can replace the comma with a .:
UPDATE TABLE_NAME
SET COLUMN_NAME = REPLACE(:val, ',', '.')
WHERE TC_ID = 'ABCD';
The database should convert the value automatically.
Or, divide by 1,000,000:
UPDATE TABLE_NAME
SET COLUMN_NAME = COLUMN_NAME / 1000000
WHERE TC_ID = 'ABCD';

SQL Server : getting certain table names from query

Is it possible to display just certain table names in the query:
USE [WebContact]
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'memberEmails'
Most of the time I would need all the table names but there are curtain situations where I need just certain row names.
When I try doing the following:
USE [WebContact]
SELECT COLUMN_NAME, ContactDateTime
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'memberEmails'
It tell me that
Invalid column name 'ContactDateTime'
even though that is one of the row names.
Is this possible to do?
The column ContactDateTime may be a column in your table but it is not a column in the INFORMATION_SCHEMA.COLUMNS view.
Since it is not a column there, SQL Server is going to error out saying that it is invalid.
I think what you're trying to do is add another WHERE clause to your statement:
USE [WebContact]
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'memberEmails'
AND [COLUMN_NAME] = 'ContactDateTime'; -- Here!
Or if you want to add multiple columns...
USE [WebContact]
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'memberEmails'
AND [COLUMN_NAME]
IN ('ContactDateTime', 'column2', 'column3', ... 'column(n)'); -- Here!
Also see here for the case against using INFORMATION_SCHEMAS.
if ContactDateTime is a column that you are looking for in table memberEmails then you can do this
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'memberEmails'
and COLUMN_NAME='ContactDateTime'
The view INFORMATION_SCHEMA.COLUMNS don't have column name ContactDateTime. Please read document first

Oracle Update Column LTRIM and RTRIM

I am wish to update my database data in SQL Developer using
UPDATE TABLE_NAME SET COLUMN_NAME = LTRIM(RTRIM(COLUMN_NAME))
But it does not take any effect even the msg of "rows updated" is displayed. The leading and trailing white spaces still exist behind every strings.
Any ideas?
Do you commit after update?
update tableName set col1 = trim(col1);
commit;
I had the same problem on two different tables. One the first table trim was ok, on the second no effect, the spaces were still there!
The difference was that in the first table I was using varchar2(30) and in the second table (where trim didn't work) I had char(30)
After modifying the second table from char(30) to varchar2(30) trim worked as expected.
Try this
UPDATE TABLE_NAME SET COLUMN_NAME = TRIM(COLUMN_NAME);
Trim Reference
Have you tried REGEXP_REPLACE(your column name, '\s*', '') ?
ex: UPDATE TABLE_NAME SET COLUMN_NAME = REGEXP_REPLACE(COLUMN_NAME, '\s*', '')
You get a message saying that n rows are updated because there are n rows in your table and you are applying the update to all of them.
To limit the update to only those rows where the update will have an effect, use:
UPDATE TABLE_NAME
SET COLUMN_NAME = LTRIM(RTRIM(COLUMN_NAME))
WHERE COLUMN_NAME != LTRIM(RTRIM(COLUMN_NAME));

How can a column be renamed in results when getting columns from INFORMATION_SCHEMA?

So I have this:
CREATE PROCEDURE getBattingColumnNames
AS
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Batting'
AND COLUMN_NAME NOT IN ('playerID','yearID','stint','teamID','lgID', 'G_batting','GIDP','G_old');
GO
And it works great. I get all the column names that I want, in c# I use this to populate a drop down with the column names. however, one of my column names, "Doub" I would like to change. So playing around with it I tried:
SELECT COLUMN_NAME.Doub AS 'DB'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE (TABLE_NAME = 'Batting')
and a variation of that, and the error is the mulitplart identifier could not be bound. How can i change that column name in this query?
You could use a case to translate the column name:
select case COLUMN_NAME
when 'Doub' then 'DB'
else COLUMN_NAME
end
from ...

Trigger UPDATE() and COLUMNS_UPDATED() functions

I have AFTER UPDATE trigger on table.
I need to get the name of changing colomn and it's old and new values.
To do statement UPDATE(column_name) with each column in code - bad solution. But I can't get all table colomns names via query
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.Columns
WHERE TABLE_NAME = 'smth'
and in cursor dynamically get UPDATE(#column_name) value.
At same time when I try to use COLUMNS_UPDATED() function when I update only one column in table, I have results (value converted to int):
64 (Updated column with ORDINAL_POSITION = 31)
32 (Updated column with ORDINAL_POSITION = 30)
8 (Updated column with ORDINAL_POSITION = 29)
4 (Updated column with ORDINAL_POSITION = 28)
2 (Updated column with ORDINAL_POSITION = 27)
1 (Updated column with ORDINAL_POSITION = 26)
32768 (Updated column with ORDINAL_POSITION = 25)
I think it is very strange and ask your help.
Whether it is strange or not, this is at least documented:
Caution
In SQL Server 2008, the ORDINAL_POSITION column of the INFORMATION_SCHEMA.COLUMNS view is not compatible with the bit pattern of columns returned by COLUMNS_UPDATED. To obtain a bit pattern compatible with COLUMNS_UPDATED, reference the ColumnID property of the COLUMNPROPERTY system function when you query the INFORMATION_SCHEMA.COLUMNS view, as shown in the following example.
SELECT TABLE_NAME, COLUMN_NAME,
COLUMNPROPERTY(OBJECT_ID(TABLE_SCHEMA + '.' + TABLE_NAME),
COLUMN_NAME, 'ColumnID') AS COLUMN_ID
FROM AdventureWorks2008R2.INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Contact';
UPDATE() will return true if the column is referenced, it doesn't matter if the value hasn't changed
so if you do
update table BLa
set Col1 = Col1
in the trigger UPDATE() will return true for that column
join inserted and deleted pseudo-tables in the trigger and check that the values haven't changed also make sure to account for NULLs of course....