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
Is it possible to get MySQL to put a row which matches some condition on the first position?
For instance, say that my result is [1, 3, 5, 6, 7, 1987] and I want it to become [6, 1, 3, 5, 7, 1987] (I want the number 6 to be on the first position)
I feel you are searching for this
SELECT * FROM `pctable`order by colName=6 desc, colName
so colName = 6 will be in top and then for others it will be id wise sort.
Try to use CASE expression in your Order by clause. Such as
ORDER BY CASE WHEN COL_VALUE = 'xxx' THEN '1'
WHEN COL_VALUE = 'xxx' THEN '2'
ELSE COL_VALUE END ASC;
Or with IF in the SELECT somehow:
SELECT IF(number = 6, number, ''), number FROM colname
I have some song names and their pre-calculated soundex stored in a mysql table. I want to compare the soundex of user input with the pre-calculated soundex'es. And get the results in ascending order of difference between the user input and song name.
I have tried the following query (in java):
String query="SELECT * FROM song ORDER BY STRCMP(pre_calculated_soundex,SOUNDEX("+user_input+")) ASC ";
But strcmp only returns 1,0 or -1. So ordering is not correct.
Also tried WHERE pre_calculated_soundex=SOUNDEX(user_input), but this just returns exactly matching soundex.
Completely low-tech and assuming that only first four characters of soundex function is being used and also assuming that "aaaa" is the user input
SELECT *
FROM song
ORDER BY Substr(pre_calculated_soundex, 1, 1) =
Substr(Soundex("aaaa"), 1, 1)
+ Substr(pre_calculated_soundex
, 2, 1) =
Substr
(Soundex("aaaa"), 2, 1)
+ Substr(pre_calculated_soundex, 3, 1)
= Substr(Soundex("aaaa"), 3, 1)
+ Substr(pre_calculated_soundex, 4, 1
)
= Substr(Soundex("aaaa"), 4, 1)
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
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