I have a table with name and address columns. I need to create two new columns that will hold modified values from these two columns, with special characters cleansed.
Please help with the queries.
EDIT:
you have 2 options:
use triggers:
CREATE TRIGGER MyTableFillCollumns ON MyTable
AFTER INSERT, UPDATE
AS
UPDATE MyTable
SET ColumnWithoudDiacritics) = Replace(Column, 'Á', 'A')
FROM MyTable m
JOIN inserted AS i
ON m.Id = i.Id
RETURN
END;
use computed columns (https://www.mssqltips.com/sqlservertip/2481/getting-creative-with-computed-columns-in-sql-server/)
new column will have a formula like this
(replace(CONVERT(nvarchar,[Column],0),'Á','A'))
Related
I want to insert this query into the new column and below is my query:
INSERT INTO T_SG_WICA_POL_DATA (SubClassGroup)
SELECT M.[Subclass_Main]
FROM [WICA subclass mapping] AS M
LEFT JOIN T_SG_WICA_TRANSACTION_VIEW ON M.[Subclass Code] = Subclass;
But I received this error when trying to run it:
Cannot insert the value NULL into column 'AccountTenure', table 'analytics.dbo.T_SG_WICA_POL_DATA'; column does not allow nulls. INSERT fails.`
I want to insert to SubClassGroup column not AccountTenure column so I'm not sure why I get this error and how can I insert the value to SubClassGroup column?
By starting the query with INSERT INTO you are telling the system to insert an entirely new row in the table. You would need to provide a value for at least every column that does not define a default or auto increment value and that is not nullable.
INSERT INTO T_SG_WICA_POL_DATA (SubClassGroup, AccountTenure, ...)
SELECT M.Subclass_Main, M.AccountTenure, ...
FROM [WICA subclass mapping] AS M
If you want to update the value of one or more columns in one or more existing table rows you can use an update query:
UPDATE T_SG_WICA_POL_DATA SET SubClassGroup = M.Subclass_Main --, AccountTenure = M.AccountTenure, ...
FROM WICA subclass mapping AS M
JOIN T_SG_WICA_TRANSACTION_VIEW AS T ON M.[Subclass Code] = T.Subclass
--WHERE some_other_condition
Note that I changed the left join into a regular join since a left join will not add any constraints to the query, causing the update to apply to all rows instead of only the ones where a record is present in T_SG_WICA_TRANSACTION_VIEW with a matching Subclass code.
I am trying to update one filled with a lot of data.
This is my table
I want to update NameAlias with new names for Major but this is my question if I have a lot of major names and ids how can I write a query that is good and short?
I know it should be something like this for one column.
Update Major Set NameAlias='Mathemathic' where IdMajor=1;
But what should I do if I have a lot of data for one field?
I want my result to be something like this:
You can use case when expression -
Update Major Set NameAlias=
case when namemajor='Math' then 'Mathemathic'
when namemajor='Computer' then 'Software'
when namemajor='Art' then 'Painthing'
when namemajor='History' then 'FranceHistory'
when namemajor='Music' then 'Piano' end
You should create a temp table containing the id, NameAlias accordingly like below.
select Id, NameAlias
into #TempTable
from (
values(1, 'Mathemathic'),
(2, 'Software'),
(3, 'Painthing')
-- The rest of values
)v(Id, NameAlias)
Then you can update the Major table
Update m
SET m.NameAlias = t.NameAlias
From Major m
INNER JOIN #TempTable t on m.IdMajor = t.Id
There is no need to create a temporary table to do this. Just use a derived table in the update query:
update m
set m.NameAlias = v.newAlias
from Major m join
(values(1, 'Mathematic'),
(2, 'Software'),
(3, 'Painting')
) v(idMajor, newAlias)
on m.IdMajor = v.IdMajor;
I have a query saved in column of a table named sqlcode which is maintaining the metadata. For instance following is a query:
select
id, name, address, phone,
.......
......
, upddate
from
bank a
join
bankreg b on a.id = b.id
where
(condition)
I'm trying to update the text of query saved in metadata but I want to only update column names part of the query without making any change to select, from, joins or any other conditions. Any way of only updating column names would be helpful. I've tried using replace function but couldn't got desired output.
I want to update columns of this textual query with the columns present in master table. If new columns are added in master, I want to add them to metadata query as well which is saved in text form. INFORMATION_SCHEMA.COLUMNS gives me the column names now I just want to update these column names to above query
If you wrap the columns you need to update in brackets, you can update them in your REPLACE, without updating the items in the SELECT portion.
select
id,name,address,phone,
.......
......
,upddate
from bank a
join bankreg b
on
a.id = b.id
where ([address] = 'My Street')
Your update would then be:
UPDATE MyTable SET MyCol = REPLACE(MyCol , '[address]', '[some_other_field]')
You wanna rename names of columns in select part?
Use currentNAme AS NewName, or currentName NewName, for each name you wanna rename.
Depends on the DBMS weather one or other or both syntax are supported (sql-server -> go with AS). Test with 1 column name to see which one works then use that one till end
select
id AS myNameForId,
name AS myNmeForName,
.......
......
,upddate
from bank AS a
join bankreg AS b
on
a.id = b.id
where (condition)
OR
select
id myNameForId,
name myNmeForName,
.......
......
,upddate
from bank a
join bankreg b
on
a.id = b.id
where (condition)
I select a list of old and new values for a table with a query:
select new, old from SOME_TABLE;
new old
----------- -----------
1174154 1064267743
1174164 1072037230
1174167 1065180221
1174180 1071828953
1174181 1067402664
1174204 1073143287
1174215 1057480190
1174222 1061816319
1174331 1072011864
1174366 1061275972
now i need to update a table that contains these old values and replace them by the new
ones.
update OTHER_TABLE set some_column = <newvalue> where some_column = <oldvalue>
Is it possible to do this with one query or do i need to loop over the result tuples and update for each row?
I cannot change the database layout or write a trigger that does this automatically...
Try the below:
UPDATE OTHER_TABLE t1
SET some_column = (SELECT t2.new FROM SOME_TABLE t2
WHERE t2.old = t1.old_value_column)
Just replace old_value_column with the column name that hold the old value in OTHER_TABLE, along with the other table and column names.
i would not use a subquery as afaik you will select some_table for each row in the table that is to be updated. should the number of rows in both tables be large, you may run into problems. therfore i suggest the update-from method outlined below.
update t
set yourcolumn = n.new
from yourtable t
join some_table s
on t.id = s.old
I have two databases.
Alarm
TMP
I have a table in Alarm, where in a table there is one empty column with null values.
And I have a single column table in TMP.
I want to copy this single column values to my table in Alarm database.
What I tried so far is,
update [Alarm].[dbo].[AlarmDetails] set Alarm_Message = (select * from [TMP].[dbo].[AlarmDetails$])
where 1=1
The error is
Subquery returned more than 1 value.
Please note this,
NOTE: There is no id column in source table. Only one table & one column, Alarm Message.
I know the cause of error, but how should I modify my SQL.
Thank You.
Here's an example of copying a column:
update dst
set Alarm_Message = src.AlarmMessage
from Alarm.dbo.AlarmDetails dst
join TMP.dbo.AlarmDetails src
on dst.id = src.id
You did not specify how the tables are related, so I assumed they both have an id column.
You need something like this.
update t1
set
t1.<something1> = t2.<something2>
from
[Alarm].[dbo].[AlarmDetails] t1
join [TMP].[dbo].[AlarmDetails] t2 on (t1.<cols1> = t2.<cols2>)
UPDATE results SET results.platform_to_insert = (
SELECT correct_platform
FROM build
WHERE results.BuildID=build.BuildID LIMIT 1
);