Select Comma Separator Column Data - mysql

I have data 1,2,3,1 like one column
in table
When i was display the these data in frond end
Stock column data its not taking space,
so wants select wit space for each value in stock
columnGROUP_CONCAT(msw_incoming_stock_detail SEPARATOR ', ')

You can use replace():
select replace(col, ',', ', ')
from table t

Related

Mysql- replace third to last character with RIGHT function without losing the rest of value

I have columns with numeric values like this:
14.333,67
3.123,90
1.234.222,01
and so on. i've written a small statement that will convert the periods to commas via the REPLACE function.
ie
UPDATE table SET column = REPLACE (column, '.', ',')
now what i need to do is update that last comma value to be a period. the datatype is char and i need to keep it that way.
I'm trying to use the RIGHT function like this.
REPLACE (RIGHT(column, 3), ',', '.')
which only seems to pull the last three values. For example after I run these two statements I get the following results in order:
original: 14.333,67
first update: 14,333,67
last update: .67
how can i squeeze all of this into one update/set statement to get the full value?
Try to chain few REPLACEs. You will need three in one row due to you have to temporarily replace one of your replaced characters to something which doesn't match to the second replaced character.
SELECT
REPLACE(
REPLACE(
REPLACE(IFNULL('123.456,78', ''),
',', ';'),
'.', ','),
';', '.') as result;
result
123,456.78
So, your update query will be like:
UPDATE
table
SET
column = REPLACE(
REPLACE(
REPLACE(IFNULL(column, ''),
',', ';'),
'.', ','),
';', '.')
You can concatenate the rest of the string with the part you're replacing in.
SET column = CONCAT(LEFT(column, LENGTH(column)-3), REPLACE(RIGHT(column, 3), ',', '.'))

Getting the name with dashes using substring_index MySQL

I have a data that have this result:
I'm using the query
select substring_index(descriptn, ' ', -1) from table1
and I get my result along with the dash for example "JACQ-ARMIE"
I want to get only JACQ and ABBY. Can you give me hints on how to get the name? Does this also apply for substring_index?
Just use substring_index() again:
select substring_index(substring_index(descriptn, ' ', -1), '-', 1)
from table1;

group_concat text field in mysql

Tried to group_concat a text field in mysql.But its taking only the first value.(if the values are '177,178') it taking only 177 because I am saving this as a text field.So how can I do group_concat with this fields?
My query looks as such:
SELECT GROUP_CONCAT(abc.displayValue SEPARATOR ' ') FROM abc WHERE abc.lookupId IN ('177,178')
Are you misplacing the quotes within your IN?
SELECT GROUP_CONCAT(abc.displayValue SEPARATOR ',') FROM abc WHERE abc.lookupId IN (177,178)
The problem isn't with GROUP_CONCAT, the problem is that your WHERE clause is only selecting one row. Since the lookupId field is an integer, it's converting the string '177,178' to an integer, which is just 177. You shouldn't have quotes around the values in the IN() list, that's making it just a single value to look for.
SELECT GROUP_CONCAT(abc.displayValue SEPARATOR ' ')
FROM abc
WHERE abc.lookupId IN (177, 178)
If the comma-separated string is actually coming from a column in a table you're joining with, see sql join tables where 1 column has comma.
SELECT GROUP_CONCAT(table1.displayValue SEPARATOR ' ')
FROM table1
JOIN table2 ON FIND_IN_SET(table1.lookupID, table2.lookupIDs)
WHERE ...

Mysql Subquery as input second query

I want to calculate distance from mysql record, firstly I get all cordinates and saved as LineString object, but I have error. What is wrong with my sql?
WITH tmp AS
(SELECT GROUP_CONCAT(CONCAT_WS(' ',lat,lng) SEPARATOR ', ') FROM track WHERE vh_id='75' AND DATE(tdate)='2016-06-09' ORDER BY tdate)
SELECT ST_Length(ST_GeomFromText(tmp));
Firstly, MySQL doesn't support the WITH clause; secondly, you should define variables like 10.4 User-Defined Variables.
You can change you sql to this;)
SELECT GROUP_CONCAT(CONCAT_WS(' ',lat,lng) SEPARATOR ', ') INTO #tmp
FROM track
WHERE vh_id='75' AND DATE(tdate)='2016-06-09' ORDER BY tdate;
SELECT ST_Length(ST_GeomFromText(#tmp));
Or just with one query:
SELECT ST_Length(ST_GeomFromText(GROUP_CONCAT(CONCAT_WS(' ',lat,lng) SEPARATOR ', ')))
FROM track
WHERE vh_id='75' AND DATE(tdate)='2016-06-09' ORDER BY tdate;

Select Word after specific word in Field

I have a string field, FieldS, that can be 2-10 words long. I can easily find all fields that contain word 'X' but what I am looking for is to extract all unique word pairs with 'X NextWord'. Clearly I can do
Select FieldS from Table1 where FieldS like '% X %'
and I'm thinking somehow there is a substring_index involved here but I can't construct if after several tries so thought perhaps there is a more relevant function I am unaware of.
You can do this with substring_index() -- twice.
Here is one way:
select concat(X, ' ',
substring_index(substring_index(field5, 'X', -1), ' ', 1)
)
Notes:
This assumes that 'X' only occurs once in the string. Or, more accurately, it uses the last occurrence of 'X' in the field.
This assumes that the only word break character is space.
EDIT:
If you are concerned about the spaces around the X you can use:
select concat(X, ' ',
substring_index(substring_index(concat(' ', field5, ' '), ' X ', -1), ' ', 1)
)