I'm trying to convert 0's and 1's in my table 'collection' to '0' being 'no' and '1' being 'yes' within SQL Server, I've researched various places and i still haven't found a concrete answer. Any help would be greatly appreciated
select
case
when Yourfield = 0 then 'no'
when Yourfield = 1 then 'yes'
end as GotIt
from
collection
Create another field and update it with your new values.
ALTER TABLE table1 ADD newfield VARCHAR(10);
UPDATE TABLE1 SET newfield = if(oldfield = 1, 'YES','NO');
ALTER TABLE table1 drop field oldfield;
alter table table1 rename newfield to oldfield;
Related
I want to do bulk update on my database. So I want to know how to insert value to the new column based on the value from other column. E.g.
If value in columnA = 15, then insert 'text' in columnB.
Thanks.
Thats not insert, thats update,and you can do it as
update table_name
set
columnB =
case when columnA = 15 then 'text' end
I've found the answer from almost similar question.
UPDATE table
SET columnB='text' where (columnA='15')
This is much more straight forward.
UPDATE table SET columnB = IF(columnA = 15, 'text',NULL);
I need to make a update query that can allow me to update in trimming leading '0' in a particular column. I know I can write a select query like this to select trimmed value below:
SELECT TRIM(LEADING '0' FROM SkuCode) FROM MyTable WHERE Id=1;
But how can implement this is in update query. Please help me.
if the column is a text or varchar type try
UPDATE MyTable set SkuCode = SUBSTRING(SkuCode,2) WHERE Id = 1
If this SELECT statement is returning the value you want to assign to the skucode column:
SELECT t.skucode AS current_skucode
, TRIM(LEADING '0' FROM t.skucode) AS proposed_skucode
FROM MyTable t
WHERE t.id=1 ;
Then you can convert that into an UPDATE statement, by replacing SELECT ... FROM with UPDATE, and adding a SET clause before the WHERE clause, to assign the expression that returns the proposed value for sku code to the sku code column, e.g.
UPDATE MyTable t
SET t.skucode = TRIM(LEADING '0' FROM t.skucode)
WHERE t.id=1 ;
I need of add character after a value sql field in auto mode. Example i have numeryc type fields 01 and i wanna add after the number PC so trasform the value in 01PC.
help me?
This query add the letter A at the end of field1
select concat(field1, 'A') from table1
And if you wanna update the table you can do..
update table1 SET field1 = concat(field1, 'A')
The last query add letter A at the end of all field1
Use the convert sql function like so
select concat(convert(nvarchar([length]), field1), 'pc') from table1
Can I combine two queries like this
first: UPDATE table SET col1=1 WHERE id='x';
second: UPDATE table SET col1=0 WHERE id='y';
can I join these queries in one?
UPDATE table
SET col1 = CASE id WHEN 'x' THEN 1 ELSE 0 END
WHERE id IN ('x','y')
Use this sql query:
UPDATE table
SET col1 =
CASE id
WHEN 'x' THEN 1
WHEN 'y' THEN 0
END
WHERE id IN ('x','y');
Also see Multiple Updates in MySQL
UPDATE table
SET col1 = (id = 'x')
WHERE id IN ('x','y');
I have 100 columns in my table and I want to update only columns that have NULL value.
I am comparing master table columns with temp table and trying to update master table column value with temp table column value.
Please help me in this regards.
Thanks in advance
Something like:
Update t1
set field1 = coalesce(t1.field1, 'test')
, field2 = coalesce(t1.field2, t1.field1)
, field3 = coalesce(t1.field3, t2.field1)
, field4 = coalesce(t1.field4, t2.field1, t2.field3)
FROM table1 t1
join table2 t2
on t1.someid = t2.someId
I have given you three examples of differnt ways you might update if the field is null. The first shows how to set it to a text value, the second how to set it to another field in the same table and third is the one where you get the value from a different table. The forth shows what to do if the value you are setting it to is also nul and thus want to use still another value in its place. You will need to write a coalesce update for each of the 100 columns.
The following should update a row in the master table with either its previous value (if it is not null) or the value from the temp table if the value of the master table is null.
This requires that master and temp table can be joined on a key field named Key in my case.
UPDATE m
SET
m.field1 = ISNULL(m.field1, t.field1),
...
FROM
MasterTable m
INNER JOIN TempTable t ON t.Key = m.Key
UPDATE B SET B.value = T.value
FROM
tblMaster B
INNER JOIN tblTemp T ON B.ID = T.ID
WHERE B.value IS NULL