Excuse me for bad English. I have a table with a field called Subject . The value of this field is any kind of string. is there a way to find how many space char in per field . for example for "mme mme" is one and for "mme" is zero and for "mme mme mme" is two.
From MySQL List:
select length(Subject) - length(replace(Subject, ' ', ''))
Or this:
SELECT CHAR_LENGTH(Subject) - CHAR_LENGTH(REPLACE(Subject, ' ', '')) as
num_spaces FROM my_table;
If you want to do it with PHP, you can use substr_count.
substr_count(STRING_HERE, " ");
Docs to substr_count(); (PHP.NET)
It will return the number of spaces as you needed.
You can try this:
SELECT length(Subject) - length(replace(Subject, ' ', '')) FROM table;
Related
I have a very specific question.
I have to build a SQL statement that builds a table where some columns are merged together. These columns shall be formatted with delimiters like '\n' or ' ' or ' - '. These delimiters shall be added only if the column before is not empty or null. This should prevent empty lines or unneeded delimiters.
Here is how I started:
SELECT
any_table.table_id,
CONCAT(any_table.text1, '\n', any_table.text2) AS text1_2,
FROM
any_table
WHERE any_table.use = 'true'
This code concats text1 and text2 as a new column text1_2 and uses a line feed as delimiter. The missing part is that line feed shall just be added if any_table.text1 is not null or empty.
Is there an elegant way in doing this with SQL?
thx
Some databases support a very handy function called concat_ws() which does exactly what you want:
CONCAT_WS('\n', NULLIF(any_table.text1, ''), NULLIF(any_table.text2, '')) AS text1_2,
In standard SQL, you can do:
TRIM(LEADING '\n' FROM CONCAT( '\n', || NULLIF(any_table.text1, ''),
'\n' || NULLIF(any_table.text2, '')
)
)
It is possible that your database supports neither of these constructs.
if you'r under SQL SERVER you can use,
SELECT id, CONCAT(colonne1 + ' - ', colonne2) FROM "table"
if you'r under Oracle : you shoul use || for concaténation like
SELECT id, CONCAT(colonne1 || ' - ', colonne2) FROM "table"
I have a mysql table with data like this.
Fullname
Adarna, Neil D.
David, Jundemil P.
Greg, Dart .
I want to remove middle-intials at the end of fullname
so the data looks like this.
Adarna, Neil
David, Jundemil
Greg, Dart
Try This
Its give only string before second space all string after 2nd space will remove
SUBSTRING_INDEX('column_name', ' ', 2);
SELECT SUBSTRING_INDEX('Adarna, Neil D.', ' ', 2);
SELECT SUBSTRING_INDEX('David, Jundemil P.', ' ', 2);
SELECT SUBSTRING_INDEX('Greg, Dart .', ' ', 2);
Hopefully this will work for your case
I wanted to add 3 periods to a result string if the string is over 20 characters. The result is using Group_Concat which works fine, I just don't know the best way to modify the result if over 20 chars.
query
LEFT(GROUP_CONCAT(employee.firstname, ' ', employee.lastname), 20) as employeenames
Totally untested:
CASE
WHEN CHAR_LENGTH(GROUP_CONCAT(employee.firstname, ' ', employee.lastname))>20
THEN CONCAT(LEFT(GROUP_CONCAT(employee.firstname, ' ', employee.lastname), 20) '...')
ELSE GROUP_CONCAT(employee.firstname, ' ', employee.lastname)
END AS employeenames
It's not likely that this performs decently, though. This is the kind of stuff your client-side language will possibly do way better.
I got an sql question. I have a table containing a column named title that store a string like this in all raws.
"Prenom - Nom (85)".
I would like to know if there is a way in sql to change that string like this :
"Nom - Prenom"
It means, I would like to reverse it and then delete the " (85).
Thank you in advance.
Yes:
select concat(substring_index(left(col, length(col) - instr(reverse(col), ' ')), ' - ', -1),
' - ',
substring_index(col, ' - ', 1)
)
This assume that you want something a bit more general than getting rid of the '(85)'; it removes the final word.
Ugly as hell and almost certainly won't work for all of your cases. But this works for the example you have posted:
select concat(
substring_index(substring_index("Prenom - Nom (85)"," (",1)," - ",-1),
" - ",
substring_index(substring_index("Prenom - Nom (85)"," (",1)," - ",1)
);
I have a column in MySql that contains a description of a products. Sometimes the description contain more the one space between words and I would like to turn it into one space so I've found it with this query:
SELECT * FROM `database`.`PRODUCTS`
WHERE `PRODUCTS`.`description` LIKE '% %'
and then repaired it by:
UPDATE `database`.`PRODUCTS`
SET `PRODUCTS`.`description` = REPLACE(`PRODUCTS`.`description`,' ',' ')
But it doesn't remove all the double spaces! There are some kind of "special" spaces with (I suspect) different ascii code - 0xA0,0xC2
How can I SELECT it and remove it?
Thanks
These are not 'special' spaces - they are not ascii codes - if they are in your database then you've got bugs in the code which put them there.
Why not just replace them?
....
SET PRODUCTS.description = REPLACE(
REPLACE(
REPLACE(PRODUCTS.description
,CHAR(160),' ')
, CHAR(194), ' ')
, ' ', ' ');