I am using a mySQL statement that returns me average of values as comma separated integer.
Eg : 2,109. But I want my output to be plain integer like 2109. Please help me on this.
You can use something like this:
SELECT REPLACE(fieldname, ',', '')
FROM ...
Or if type of fieldname is integer use this query
SELECT REPLACE(CONCAT(fieldname), ',', '')
FROM ...
Related
SELECT * FROM TABLE_NAME WHERE 'string' in ('string1','string2','string3')
Its not given correct result find_in_set only given a exact result. If any way to get correct result without using find_in_set
SELECT * FROM TABLE_NAME WHERE 'string' in
('string1','string2','string3');
In the above query, string is supposed to be a field (column) so, if you were to have this query work, it would be without single quotes ', like this:
SELECT * FROM TABLE_NAME WHERE string IN
('string1','string2','string3');
This will return all rows where the field string is exactly string1 or string2 or string3.
Now, if you want to query the field string for values like "string", not exactly matching, then you use the LIKE operator:
SELECT * FROM TABLE_NAME WHERE string LIKE '%string%';
In the above query, all 3 records containing string1, string2 and string3 will be returned.
I have JSON stored in a MySQL database (version 5.6.17) that I'm trying to regex into a column to retrieve a list of campaign IDs. My query is as follows:
SELECT JSON REGEXP '"id":([0-9]*)' AS id
FROM PROD_APPNEXUS.dimension_json_creatives;
where JSON is a column containing the data I need to parse as ID. I know REGEXP can be used for strings in SELECT queries (i.e. SELECT 'foobar' REGEXP '([a-z]+)' AS foobar) but can columns be pattern matched in the same way?
Would there be a way to cast the JSON column as string and then regex?
Any help would be appreciated!
Thanks,
Sam
You can use replace and substring_index to split your column, like this;)
SELECT replace(substring_index(JSON, ':', -1), '"', '') AS id
FROM PROD_APPNEXUS.dimension_json_creatives;
when I run sql below return aaaa,
select replace(substring_index('"id":"aaaa"', ':', -1), '"', '');
I assumed your JSON's value does not exist :.
I have values stored like this in a field 1,255,230,265.
Is there a function in MySQL that will give me the second value in that string? In this case it'll be 255.
I tried using locate, but that does not seem to be meant for this.
Try this
select SUBSTRING_INDEX(SUBSTRING_INDEX(field_name,',',2),",",-1) from table_name
You might want to use SUBSTRING_INDEX() function.
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(field,',',2),',',-1)
FROM yourTable.
This grabs everything infront of the second comma, then grabs everything after the last comma (-1)
Try This
select * from Table1 where ',' + Nos+ ',' like '%,255,%'
Refer:
MySQL query finding values in a comma separated string
mysql check if numbers are in a comma separated list
Use FIND_IN_SET() function:
SELECT *
FROM tableA
WHERE FIND_IN_SET(255, columnName)
OR
Use LIKE operator
SELECT *
FROM tableA
WHERE CONCAT(',', columnName, ',') LIKE '%,255,%'
I have a column, called net_amount, it contains values like 244,98. Its a varchar column. When I try to sum it using the sum function, it only sums the 244 and skipts the decimal places. I tried casting it to decimal like this:
select cast(net_amount as decimal) from mytable
This skips the decimal places as well ... any idea what might be wrong?
Thanks!
You could use REPLACE to replace comma to dot:
SELECT REPLACE('244,98', ',', '.') * 1
or you can use CAST like this:
cast(REPLACE(net_amount, ',', '.') as decimal(8,2))
I am writing a PHP and MySQL application in which i have to concatenate multiple column values into one single column.I would have used the concat() function,but it does not handle null values,and the concat_ws(),which does not return the result in the output i want.
What i need can be achieved in the Oracle database like this:
Select 'The Surname Is'||last_name from employees;
My Issue is how can i achieve this same result with MySQL..without using the above named functions?
CONCAT with IFNULL:
SELECT
CONCAT('The Surname Is ', IFNULL(last_name, 'sadly not available'))
FROM `employees`
#Minesh: CONCAT_WS does not 'take care' of NULL values. To illustrate this...
CONCAT_WS("~",house.name,house.address,house.type)
In the above example, if house.address is NULL the returned result will not contain a neat double tilda (~~) as expected. It will be a tilda separated list with only 1 tilda. eg "fun House~mansion"
You can also use CONCAT_WS function which takes care of NULL values
SELECT
CONCAT_WS(' ','The Surname Is',lastname)
FROM `employees`
Use coalesce to concat an empty string
select concat(coalesce(null, ''));
A little trick: Use empty string like separator with CONCAT_WS (Some times you wan't insert white spaces)
CONCAT_WS('','The Surname Is:',lastname)
FROM `employees`