Substring Binary Data in MYSQL - 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

Related

insert and remove multiple values in a table created with SET sql

I created a SET sql table, so a row can contain multiple values: Foo_1 Foo_2 Foo_3.
The above query run but it will update the the row and set just one value.
QueryType valid values are 1 2 or 3
"UPDATE Foo_status
SET type = 'Foo_%s',
status = %d
WHERE name ='%s'
", queryType.c_str(),status,GetName()
When i run the query: Let's assume that QueryType is 1, it's working.. my type row will have Foo_1, but when i run again the query and Foo has QueryType 2, it will remove Foo_1 and add Foo_2
How i can change my query so when i run the query it will continue to update values ?
Example:
Run Query
-Add Foo_1
Run Query
-Add Foo_2
Table struct
CREATE TABLE `Foo_status` (
`name` text NOT NULL,
`type` set('Foo_1','Foo_2','Foo_3') CHARACTER SET latin2 NOT NULL DEFAULT '',
`status` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I tried to search for an solution but without succes, that's i asked here.
Thanks!
Multiple set elements are specified using a comma-separated string. To add a new value, you have to concatenate a string beginning with comma.
"UPDATE Foo_status
SET type = CONCAT(type, ',Foo_%s'),
status = %d
WHERE name ='%s'
", queryType.c_str(),status,GetName()
To remove a value from a SET, use REPLACE():
"UPDATE Foo_status
SET type = REPLACE(type, 'Foo_%s', ''),
status = %d
WHERE name ='%s'
", queryType.c_str(),status,GetName()
It would be easier if you normalized your schema and used a table with a separate row for each type, instead of using the SET datatype. This would also allow you to add an index that would make searching for specific set values more efficient.
You can remove a set wirh this. Here written for select.
You can it also use for UPDATE.
You only must change the string to remove
select t,
trim( BOTH ',' FROM
replace (concat (',',t,','),',Foo_2,',',')) as new_t
from F;
Sample: http://www.sqlfiddle.com/#!9/a8b041/2

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')

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));

operation on column in SQL

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.

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