I have a query where I am using GROUP_CONCAT and a custom separator as my results may contain commas: '----'
This all works well, however it is still comma separated, so my output is:
Result A----,Result B----,Result C----
How can I make it so the output is:
Result A----Result B----Result C----
I thought this was the idea of a custom separator!
Failing that, can you escape commas in your results, so I can explode in PHP by the GROUP_CONCAT commas?
Looks like you're missing the SEPARATOR keyword in the GROUP_CONCAT function.
GROUP_CONCAT(artists.artistname SEPARATOR '----')
The way you've written it, you're concatenating artists.artistname with the '----' string using the default comma separator.
Query to achieve your requirment
SELECT id,GROUP_CONCAT(text SEPARATOR ' ') AS text FROM table_name group by id;
Or, if you are doing a split - join:
GROUP_CONCAT(split(thing, " "), '----') AS thing_name,
You may want to inclue WITHIN RECORD, like this:
GROUP_CONCAT(split(thing, " "), '----') WITHIN RECORD AS thing_name,
from BigQuery API page
Related
I can't get the REPLACE function to work on my SQL command shown here. I want it to take my search term (55512) and search for this in the field using the modified field from the regexp to remove all non-alphanumeric fields.
SELECT
`Customers`.`id` AS `Customers__id`,
`Contact`.`id` AS `Contact__id`,
`Contact`.`customer_id` AS `Contact__customer_id`,
`Contact`.`full_name` AS `Contact__full_name`,
`Contact`.`phones` AS `Contact__phones`
FROM
`customers` `Customers`
INNER JOIN `contacts` `Contact` ON `Contact`.`id` = (`Customers`.`contact_id`)
WHERE
REPLACE(
`Contact`.`phones`, "[^a-zA-Z0-9]",
""
) LIKE '%55512%'
LIMIT
20 OFFSET 0
So what I want to do is be able to search "55512" and have it match if the Contact.phones field contains "999-555-1212" or "(999) 555-1212". The Contact.phones field is stored as a JSON array in case this is relevant, but I would expect the above SQL command to remove all brackets, curly braces, etc and just search for the string.
When I do the above search in MySQL, it returns zero results, but there is a result that contains: [{"value":"999-555-1212","label":"Primary"}]
The problem is that the REPLACE function does not work with regex, but attempts to match strings.
You can solve this problem in two ways:
adopting REGEXP_REPLACE function, that effectively uses regex:
WHERE
REGEXP_REPLACE(
`Contact`.`phones`, "[^a-zA-Z0-9]",
""
) LIKE '%55512%'
keeping the REPLACE function, but replacing dashes only with the empty string.
WHERE
REPLACE(
`Contact`.`phones`, "-",
""
) LIKE '%55512%'
Check the demo here.
WHERE phones RLIKE '555-?12'
Would also work since the only extraneous characters would be a dash in one spot.
Both this and the answer from lemon will require a full table scan. That is, either will be slow if the table is big.
I have read the forums to find a solution for my issue but I am stuck with a MySQL error when I use the query.
I want to extract part of a field, everything that is after \\nt4\applications\prod\hde\atn\ in the FILE_NAME column
Here is the query:
SELECT FILE_NAME,
REPLACE (FILE_NAME,'\\nt4\applications\prod\hde\atn\','') as newfilename
from atn_documents
It always return me a
syntax error near ''\
It looks like the string to look into can not contains \ character??
Can anyone drive me?
Thanks
Cedric
Use SUBSTRING_INDEX:
SELECT
SUBSTRING_INDEX(FILE_NAME,
'\\nt4\\applications\\prod\\hde\\atn\\',
-1) AS path
FROM yourTable;
Demo
The above query is a verbatim implementation of your requirement, since it returns only what is after the path of interest. Also note that the immediate reason why your query does not even run is that you need to escape backslashes by doubling them up \\ if you want them as literals.
You have to escape the "\" character in the query. You can add additional "\" to escape it.
e.g.
SELECT FILE_NAME, REPLACE (FILE_NAME,'\\nt4\\applications\\prod\\hde\\atn\\','') as newfilename from atn_documents
I have one table emp in MySQL database having column as name. In that name column, the value is 'abc\xyz'. I want to search this value. I have tried using following query:
select * from emp where name like 'abc\xyz';
Also i have tried
select * from emp where name like 'abc\xyz' escape '\\';
But i did not found any output. Could you please help me in finding such strings? Such strings can have special character at any location.
Thanks in advance.
You may try like this:
select * from emp
where empname like '%abc\\\\xyz%'
SQL Fiddle Demo
From the docs:
Because MySQL uses C escape syntax in strings (for example, “\n” to represent a newline character), you must double any “\” that you use in LIKE strings. For example, to search for “\n”, specify it as “\\n”. To search for “\”, specify it as “\\\\”; this is because the backslashes are stripped once by the parser and again when the pattern match is made, leaving a single backslash to be matched against.
SELECT REPLACE(text,'\\','') FROM tbl
You can use REPLACE to remove some special chars :)
I'm trying to remove parenthesis from the area code of a number. I'm able to do so but when I try to concatenate the two Replace functions, the numbers repeat with only one parenthesis removed.
This is what I tried so far:
SELECT HomePhone, REPLACE(HomePhone, '(', '') +
REPLACE(HomePhone, ')', '') AS Expr1
FROM dbo.Employees
http://i.imgur.com/4iJoFzE.png
Nest don't add
Replace(Replace(HomePhone,')',''),'(','')
Look at how the function replace works. It expects string With Text To Evaluate, string to replace, string to replace with)
By adding them you should be getting the number listed twice, but if the data type isn't long enough it may be truncating values. by nesting you're telling the system to replace the ) and then use that string w/o the ) to have the ( replaced with ''.
You cannot concatenate in this way, you must use the concat function in SQL. Or use thus:
SELECT HomePhone, REPLACE(REPLACE(HomePhone, ')', ''), '(', '') AS Expr1 FROM dbo.Employees
When I concat($name, $surname), is there a way of putting a space in between the $name $surname using my sql not php so when i get the result it formats a little cleaner?
You can concatenate string literals along with your fields, so you can add a space character in a string between the fields you're concatenating.
Use this:
CONCAT(name, " ", surname)
This functionality is documented quite clearly on the MySQL manual page for the CONCAT() function.
There is also the CONCAT_WS function which allows you to specify a separator to be used between each of the other fields passed to the function. If you're concatenating more than two fields in the same way, this function might be considered cleaner than repeating the separator between each field.
For example, if you wanted to add a middle name field, you could use this function to specify the separator only once:
CONCAT_WS(" ", first_name, middle_name, surname)
Just add a space in there.
SELECT CONCAT(name,' ',surname) AS full_name FROM table;
EDIT oops, bad spelling there... ;p
Use this, no version dependencies
concat(name,Char(32),venue)
Use CONCAT(name, ' ', surname).
after trying this i tried to do this
concat(name, _utf8 ' ' ,name1)
Hey i had the same problem and this is what i used and it really worked out great
CONCAT(column1," ",column2)
the issue is you add physical space in between the double courts("") using the space bar on your keyboard and then thats it
The above all options are not working ..
My query is
SELECT CONCAT(FIRSTNAME, ' ' , LASTNAME) AS FULLNAME FROM USERS;
getting the below error:
ORA-00909: invalid number of arguments 00909 00000- "invalid number of arguments" *Cause: *Action: Error at line 26 column :9
Use this methoid to add separator
CONCAT_WS(' ', first_string, second_string, n_string, ..., last_string)