mysql insert concatenate a constant value - mysql

I want to insert a constant called TUC and concatenate it with the initials of FirstName and LastName id of table.
id - 1
fname - James
lname - Bond
id - 2
fname - Daniel
lname - Edgar
eg. TUCJB001, TUCDE002,....

something like this
update tab
SET newid=CONCAT('TUC', LEFT(fname, 1), LEFT(lname, 1), LPAD(id,03, '0'));
Demo
Note If you want 4 digit number you will need do changes as highlighted below.
update tab
SET newid=CONCAT('TUC', LEFT(fname, 1), LEFT(lname, 1), LPAD(id,digit_range, '0'));
^^^^^^^^^^^

You can use something like this to get the value:
select
concat('TUC', left(firstname, 1), left(lastname, 1), '00', cast(id as char)) newValue
from yourtable
Then to update:
update yourtable
set yourColumn = concat('TUC', left(firstname, 1), left(lastname, 1), '00', cast(id as char))
See SQL Fiddle with Demo

UPDATE [table] SET [columnname] = CONCAT( UPPER(LEFT(fname, 1)), UPPER(LEFT(lname, 1)), LPAD(CONVERT(id, VARCHAR), 3, '0') );

Related

MySQL query error "Subquery returns more than 1 row" where update into the same table

How to update to the same table? i tried to do fraction conversion and update 1 of the rows in the same table but the error "Subquery returns more than 1 row"
Below are the code, (State1 is the table itself, Share field consists of those fraction data like 1/2,1/5 ..)
UPDATE State1 set shareresult =(SELECT
x.Multiplier / x.Divider as Result
from
(select
cast( substr( t.share,
1,
locate('/', t.share) - 1)
as decimal)
as Multiplier,
cast( substr( t.Share,
locate('/', t.Share) + 1,
locate( ' ',
concat(t.Share, ' ')))
as decimal)
as Divider
from
State1 t ) x)
thanks
Finally, i managed to solve my problem with the code below
CREATE TEMPORARY TABLE activity_product_ids AS SELECT x.NumberID, x.Multiplier / x.Divider as Result from (select cast( substr( t.share, 1, locate('/', t.share) - 1) as decimal) as Multiplier, cast( substr( t.Share, locate('/', t.Share) + 1, locate( ' ', concat(t.Share, ' '))) as decimal) as Divider, t.ID as numberID from State1 t) x;
UPDATE State1 a
JOIN activity_product_ids b
ON a.ID=b.NumberID
SET a.shareresult=b.Result;
Thanks for all the replies.
Neither your update statement nor the sub-query has a WHERE clause so it looks like you are trying to update every row with fraction to decimal conversion of every row.
This is a much simpler approach -
UPDATE State1
SET shareresult = ROUND(SUBSTRING_INDEX(share, '/', 1) / SUBSTRING_INDEX(share, '/', -1), 2)
WHERE id = 1;

Find multiple ids stored in a string in Mysql

I have a string that looks like this:
pi_18944000_780345308_54210001000_345900_text
How do I extract all the Ids located in it and store it?
I tried something like this:
set #a = (SELECT
SUBSTRING(SUBSTRING(SUBSTRING('pi_18944000_780345308_54210001000_345900_text',
(LOCATE('_', 'pi_18944000_780345308_54210001000_345900_text'))), 2), 1,
LOCATE('_', SUBSTRING(SUBSTRING('pi_18944000_780345308_54210001000_345900_text',
(LOCATE('_', 'pi_18944000_780345308_54210001000_345900_text'))), 2)) - 1)
);
select #a;
set #b = (SELECT
SUBSTRING(SUBSTRING(SUBSTRING(SUBSTRING(SUBSTRING(
'pi_18944000_780345308_54210001000_345900_text' , ( LOCATE('_', 'pi_18944000_780345308_54210001000_345900_text' )) ), 2),
LOCATE('_', SUBSTRING(SUBSTRING( 'pi_18944000_780345308_54210001000_345900_text', ( LOCATE('_', 'pi_18944000_780345308_54210001000_345900_text'
))), 2))), 2), 1, LOCATE('_', SUBSTRING(SUBSTRING(SUBSTRING(SUBSTRING( 'pi_18944000_780345308_54210001000_345900_text' , (
LOCATE( '_', 'pi_18944000_780345308_54210001000_345900_text' ))), 2),
LOCATE('_', SUBSTRING(SUBSTRING( 'pi_18944000_780345308_54210001000_345900_text', (
LOCATE('_', 'pi_18944000_780345308_54210001000_345900_text' ))) , 2))), 2)) - 1) );
select #b;
This works but its really complex.
Just wanted to see if there is a better way to do this?
Here's a somewhat simpler solution. It uses the SUBSTRING_INDEX() built-in function. It only requires referencing the string expression once.
mysql> set #str = 'pi_18944000_780345308_54210001000_345900_text';
mysql> SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(#str, '_', 3), '_', -1) AS n;
+-----------+
| n |
+-----------+
| 780345308 |
+-----------+
See https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_substring-index
Next time you should consider not storing multiple id's in a string if you need to reference the individual id's in an SQL expression.

mysql replace last group of digits in IP address

I would like to replace the last digits of all IP:port's with a *.
For example: 192.168.1.1:2000 should become 192.168.1.*:2000
set #ip = '192.168.1.1:2000';
select concat(
substring_index(substring_index(#ip, ':', 1), '.', 3),
'.*:',
substring_index(#ip, ':', -1)
);
If you want to understand how it works run the following query:
set #ip = '192.168.1.1:2000';
select #ip
, substring_index(#ip, ':', 1)
, substring_index(#ip, ':', -1)
, substring_index(substring_index(#ip, ':', 1), '.', 3);
It will return
192.168.1.1 2000 192.168.1
You just need to concatenate the last two columns and the middle part (.*:)

How to select string between characters?

I made this table:
Table (Websites)
WebsiteID | WebsiteName
2324442 'http://www.samsung.com/us/'
2342343 'https://www.microsoft.com/en-au/windows/'
3242343 'http://www.apple.com/au/iphone/'
And I want to be able to SELECT the domain names from this table.
Something like this:
WebsiteName
'www.samsung.com'
'www.microsoft.com'
'www.apple.com'
Is there a string method I can use for this? Like splitting the string between // and /.
You can use SUBSTRING_INDEX() :
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(websiteName, '//', -1),
'/', 1)
FROM table
You can even use the following:
SELECT WebsiteID , WebsiteName
(CHARINDEX ( '//', WebsiteName, 1 ) + 1), -- Position of the double slashes
CHARINDEX( '/', REVERSE (WebsiteName ), 1), -- Position of the last single slash
SUBSTRING(WebsiteName, (CHARINDEX ( '//' , WebsiteName, 1 ) + 2), CHARINDEX( '/', REVERSE (WebsiteName ), 1) ) -- Final string
FROM Table

Extract surname with initial in SQL Server

In SQL Server, I have a column NAME with data like this:
1, CHAN Tai Man
2, CHAN Ting Ting
I would like to have a query that extract the NAME2 to
1, CHAN T M
2, CHAN T T
As you may observe that only initial of the given name is required. How can I achieve that in SQL Server 2008? Many thanks for your gentle help.
Like Gordon said,the first one is easy and here's my take on it
DECLARE #table TABLE(id INT,name nvarchar(50))
Insert into #table
Values(1,'CHAN Tai Man')
Insert into #table
Values(2, 'CHAN Ting Ting')
select name
, LEFT(name, (PATINDEX('%[ _-]%',name))) +substring(name,(PATINDEX('%[ _-]%',name)+1),1) +' '+substring(reverse(rtrim(substring(reverse(name), 1, charindex(' ', reverse(name))))),(PATINDEX('%[ _-]%',reverse(rtrim(substring(reverse(name), 1, charindex(' ', reverse(name))))))+1),1) as Fullname
from #table
The first initial is pretty easy:
select left(name, charindex(' ', name) + 1)
The second is a bit tougher. I think the logic is like this
select (left(name, charindex(' ', name) + 1) +
substring(reverse(name), charindex(' ', reverse(name)) - 1, 1)
)
You might need to include case statements to be sure that the name has two spaces.