I'm seeking the most efficient way to make strings like that "abcdefghijklmnop"(longer then 15) to appear like that "abcdefghijklm.." - all of that inside MySQL query. I dont want to deal with it inside my app code.
select if(char_length(thefield) > 15, concat(substr(thefield, 1, 13), '..'), thefield)
SELECT
CASE WHEN LENGTH(str) > 15
THEN CONCAT(SUBSTRING(str, 1, 13), "..")
ELSE str
END
Try:
select if(length(string)> 15,concat(left(string, 15), '..'),string) from your_table
Related
I have a mysql table with some data. In that i have a column(mobile_number) and that column value is like mobile number (ex:1234587920).
my expected out put is : 12-34-58-79-20
I want to add hyphen after every two numbers.
use concat() for joining strings and use substr() for split a string in mysql :
split (column,start,count) select column from start position for count charachter
concat(column1,"-",column2) will be column1-column2
SELECT CONCAT(SUBSTR(phone,1,2),'-',SUBSTR(phone,3,2),'-',SUBSTR(phone,5,2),'-
',SUBSTR(phone,7,2),'-',SUBSTR(phone,9,2)) FROM `table`
I would definitely use concat_ws() for this:
select concat_ws('-',
substr(mobile_number, 1, 2),
substr(mobile_number, 3, 2),
substr(mobile_number, 5, 2),
substr(mobile_number, 7, 2)
)
Here is a db<>fiddle.
this will work:
SELECT concat(SUBSTRING("1234587920", 1, 2),"-",SUBSTRING("1234587920",3,2),"-
",SUBSTRING("1234587920", 5, 2),"-",
SUBSTRING("1234587920", 7, 2),"-",
SUBSTRING("1234587920", 9, 2)) AS ExtractString;
for your table query is like :
SELECT concat(SUBSTRING(mobile_number, 1, 2),"-
",SUBSTRING(mobile_number,3,2),"-",SUBSTRING(mobile_number, 5, 2),"-",
SUBSTRING(mobile_number, 7, 2),"-",
SUBSTRING(mobile_number, 9, 2)) ExtractString from tablename;
you can use substr and contact_ws command in mysql
update tmp_pay SET date_fix = concat_ws('-',substr(date_at,1,4),substr(date_at,5,6),'01') ;
you can try this
select concat( right(phone_number,3) , "-" , mid(phone_number,4,3) , "-", right(phone_number,4)) from table
I have a table with some values like below,
Slno
---------
IFAAA1121
IFAAA1122
IMBBB1121
IMBBB11223
My goal is to reformat the SlNo in to the below format,
Slno
---------
IF-AAA-1121
IF-AAA-1122
IM-BBB-1121
IM-BBB-11223
How is it possible ?
My query is:
UPDATE `certificate_log_uae`
SET `DeviceSerialNumberTemp` = REPLACE(LEFT(DeviceSerialNumberTemp,2),
LEFT(DeviceSerialNumberTemp,2).'-')
You can use the Substr() function to get substrings out from your input string, at various positions and lengths.
Since the length of the last substring is not fixed; we can simply specify the start position to slice the substring, and leave specifying the length parameter. It will consider the substring till the end of the overall string.
Now, just concatenate this substrings back using - appropriately.
Try the following:
UPDATE `certificate_log_uae`
SET `DeviceSerialNumberTemp` = CONCAT(SUBSTR(`DeviceSerialNumberTemp`, 1, 2),
'-',
SUBSTR(`DeviceSerialNumberTemp`, 3, 3),
'-',
SUBSTR(`DeviceSerialNumberTemp`, 6)
)
One approach would be to just build the final string you want using concatenation:
UPDATE certificate_log_uae
SET DeviceSerialNumberTemp = CONCAT(LEFT(DeviceSerialNumberTemp, 2),
'-',
SUBSTRING(DeviceSerialNumberTemp, 3, 3),
'-',
SUBSTRING(DeviceSerialNumberTemp, 6));
Demo
If you are using MySQL 8+ or later, then there is a very simple regex based solution using REGEXP_REPLACE:
SELECT
DeviceSerialNumberTemp,
REGEXP_REPLACE(DeviceSerialNumberTemp, '(.{2})(.{3})(.*)', '$1-$2-$3') AS output
FROM certificate_log_uae;
Demo
Try simple insert function:
select Slno, insert(insert(slno, 3, 0, '-'), 7, 0, '-') from tbl
Demo
To update values try:
update certificate_log_uae set
DeviceSerialNumberTemp = insert(insert(DeviceSerialNumberTemp , 3, 0, '-'), 7, 0, '-')
Any help would be greatly appreciated.. Im quite new to mySQL and figured there has to be an easier way to update my database.
So my issue is that I have a number of records that don't have a leading zero but I need the total number of characters in any record to be 5.
e.g. 6930 should in fact be 06930
I came across this code but I am unable to get it to work correctly, could anyone point me in the right direction.
UPDATE `it_asset_register`.`tab_id_master`
SET tab_id_master.ID = LEFT('00000', 5-LEN(tab_id_master.ID)) + tab_id_master.ID
WHERE ID = '8407' AND LEN(tab_id_master.ID)<5 AND Len(tab_id_master.ID)>0;
Thanks
LPAD should do the job here:
SELECT LPAD ('123', 5, '0');
00123
SELECT LPAD ('12345', 5, '0');
12345
Try:
UPDATE `it_asset_register`.`tab_id_master`
SET tab_id_master.ID = LPAD (tab_id_master.ID, 5, '0')
WHERE ID = '8407' AND LENGTH(tab_id_master.ID)<5 AND LENGTH(tab_id_master.ID)>0;
To concatenate two or more strings in MySQL, you must use the CONCAT() function.
A possible solution can be:
UPDATE tab_id_master
SET id = SUBSTRING(CONCAT('00000', id), -5)
WHERE LENGTH(id) > 0 AND LENGTH(id) < 5;
Alternatively you can use the LPAD() function:
UPDATE tab_id_master
SET id = LPAD(id, 5, '0')
WHERE LENGTH(id) > 0 AND LENGTH(id) < 5;
Regards
UPDATE
table_name
SET
column_name = RIGHT('00000' + CAST(column_name AS CHAR), 5)
This concatenates the 5 zeros and your ID to something like '000001234'. Then it selects the 5 rightmost characters.
You didn't specify tab_id_master.ID type. If it is of a numeric type you wouldn't be able to store a number with leading zeros but otherwise you could use LPAD():
SET tab_id_master.ID = LPAD(tab_id_master.ID, 5, 0)
I'm trying to search and replace mobile numbers with the full international code.
So where rows have 07970000007 to replace the beginning with +447970000007
UPDATE tblMemberImportClub
SET msisdn = REPLACE(msisdn, '07', '+447')
WHERE INSTR(msisdn, '07') = 1;
But this also replaces the other matches:
+4479700000+447
I don't think i can use TRIM as some rows will already start with +447 and will therefore nor require any updates.
Thanks in advance for any assistance.
Use LIKE and INSERT():
UPDATE tblMemberImportClub
SET msisdn = INSERT(msisdn, 1, 2, '+447')
WHERE msisdn LIKE '07%';
INSERT() is a string function that replaces exactly the characters you specify (see here).
SELECT
CONCAT(
REPLACE(
LEFT('07970000007',2), '07', '+447'
),
SUBSTRING('07970000007', 3, CHAR_LENGTH('07970000007'))
)as replaced
I'm transferring over some queries from MySQL to PostgreSQL and I'm stumped on how to rewrite the following query to work in PostgreSQL:
SUM(phoneid IN (1, 2, 6, 8)) AS completedcalls
I originally thought I could just do SUM(SELECT phoneid FROM myTable WHERE phoneid = 1 OR phoneid = 2 etc etc, but I do not believe you can have a SELECT within a sum.
I also tried using a WITH query but had no luck getting that to work.
how about using CASE
SUM(CASE WHEN phoneid IN (1, 2, 6, 8) THEN 1 ELSE 0 END)
count(phoneid in (1,2,6,8) or null)
bool may be cast to integer:
SUM(CAST(phoneid IN (1, 2, 6, 8) AS INTEGER)) AS completedcalls