operation on column in SQL - mysql

I have a table in SQL database, where one column contains the character "C" or "P".
I want to define a new column, where the value is 1, if this character is "C" and zero otherwise.
kind regards,
Daniel

if you want to define through projection, try
SELECT columnName, IF(columnName = 'C', 1, 0) newValue
FROM tableName
As an alternative suggested by Michael Berkowski,
SELECT columnName, (columnName = 'C') newValue
FROM tableName

SELECT
your_fields,
IF(your_column = 'C', 1, 0) AS new_field
FROM your_table

Do you want it to be a permanent column? If so, create a new column in your table with a default value of 0. Then issue an update statement as follows:
UPDATE tableName SET newColumn = 1 WHERE oldColumn = 'C';

ALTER MY_TABLE ADD NEW_COLUMN INT
UPDATE MY_TABLE SET NEW_COLUMN=1 WHERE OTHER_COLUMN='C'
UPDATE MY_TABLE SET NEW_COLUMN=0 WHERE OTHER_COLUMN='P'

INSERT
WHEN Col_name1='c' THEN
INTO your_Table (Col_name2)
VALUES (1)
WHEN Col_name1='P' THEN
INTO your_table (Col_name2)
VALUES (0);
SELECT * FROM your_Table;
It will work.

Related

concatenate a string for all column values in sql update

I have a component table which has id, query columns. I want to concatenate a string type=bug to the query column for all values. Can it be done using update statement?
udpate dbname.tablename set query=query+"some_string", wiil this work?
If i understand correctly, you can do this with CONCAT(value, ' type=bug') at every field like :
UPDATE `col` SET `col1` = CONCAT(col1, ' type=bug'), SET `col2` ...
SELECT CONCAT(Id,' string') AS 'Column' FROM Table.
UPDATE TABLE SET COLUMN = CONCAT(Id, ' string')
OR
SELECT (Id + 'string') AS 'Column' FROM Table.
UPDATE TABLE SET COLUMN = (Id + 'String')

Substring Binary Data in MYSQL

I have a binary column in my table in mysql. I need to update all the rows for example.
Replace 2nd byte with 1. Doesn't matter what the value was.
SELECT HEX(data) FROM table;
Then
UPDATE table SET data[1] = 1; // the idea
But how do you do something like this in mysql?
Here how you can do it. First get the binary representation with hex function.
Then on the returned value use substring and concat function to replace the 2nd value with 1 and finally update.
Here is an example.
CREATE TABLE t (c BINARY(10));
INSERT INTO t SET c = 'b';
select (HEX(c)) from t;
62000000000000000000
SELECT concat(
substring(HEX(c),1,1),
'1',
substring(HEX(c),3,length(HEX(c)))
)
from t
61000000000000000000
Now putting all together in the update command will replace the value
update t set c = unhex(
concat(
substring(HEX(c),1,1),
'1',
substring(HEX(c),3,length(HEX(c)))
)
);
select (HEX(c)) from t;
61000000000000000000

Query Update first string from the left

How Can I update strings which is containing 1 in my column.
#Select column from table
sample column: 100001, 100002, 100003, 100004, 100005, 100006, 100007, 100008
#How to update one at a time query?
update column: E00001, E00002, E00003, E00004, E00005, E00006, E00007, E00008
If your values are not stored as comma separated the you can use substring() to get the string after 1,and left() function in where clause to check the value should have 1 as starting character
update t
set `column` = CONCAT('E',SUBSTRING(`column`,2))
where left(`column` , 1) ='1'
Demo
Edit from comments
update t
set `column` = CONCAT(left(`column` , 1),'E',SUBSTRING(`column`,3))
where right(left(`column` , 2),1) ='1'
Demo 2
Try this
UPDATE table
SET `column` = CONCAT(
REPLACE(
LEFT(`column`,1), '1', 'E'),
SUBSTRING(`column`, 2));

mysql REPLACE INTO and optional values IFNULL

I'm trying to do something like this inside of a stored procedure:
REPLACE INTO mytable
SET myid = `IDvalue`, mytitle = `sMyTitle`, myoptionalvalue = IFNULL(`sMyOptValue`, myoptionalvalue);
But not seems to work, any idea how to do this?
Thanks!
The REPLACE INTO syntax works exactly like INSERT INTO except that any old rows with the same primary or unique key is automaticly deleted before the new row is inserted.
This means that instead of a WHERE clause, you should add the primary key to the values beeing replaced to limit your update.
REPLACE INTO myTable (
myPrimaryKey,
myColumn1,
myColumn2
) VALUES (
100,
'value1',
'value2'
);
...will provide the same result as...
UPDATE myTable
SET myColumn1 = 'value1', myColumn2 = 'value2'
WHERE myPrimaryKey = 100;

Replace in SQL Server 2008

I have a column A in a table called T. The column is of type nvarchar(255).
There is a trigger on this table that updates the values in column A to a column B in table B. The column B is of type float.
Suppose if there is value 35.1 % in column A, I want to strip off the % and update only 35.1 in column B. It is not necessary that there should be a % symbol for the value in column A.
How do I do this in SQL Server 2008? Any ideas and suggestions are much appreciated!
You may try to use the following query:
update B
set B.colB = cast(REPLACE(A.colA, '%', '') as float)
from A
where B.idB = A.idA
select replace('35.3 %','%','') or select replace('35.3 ','%','') will both return 35.3; just change your query accordingly; something like:
update tableb
set b=replace(valuefromcola,'%','')
where condition
If you want to get only numbers in a string try this function:
CREATE Function [dbo].[GetNumbersFromString](#Temp VarChar(1000))
Returns VarChar(1000)
AS
Begin
While PatIndex('%[^0-9]%', #Temp) > 0
Set #Temp = Stuff(#Temp, PatIndex('%[^0-9]%', #Temp), 1, '')
Return #TEmp
End
You should put this inside your trigger. Please note that you'll need to modify the function to accept the . and , characters (due to those being valid in numbers) =-)
--trim trailing non-alphanumeric chars
DECLARE #input nvarchar(4000)
SET #input = ',,, a word $$$'
SELECT REVERSE(SUBSTRING(REVERSE(#input), PATINDEX('%[a-zA-Z0-9]%', REVERSE(#input)), LEN(#input)))
update B
set B.colB = cast(REPLACE(A.colA, '%', '') as float)
from A
where B.idB = A.idA