my brain might be really tired but i coudn't seem to work my head around this simple query.. I want to extract a last substring within delimiters such as ',' from a column? i was thinking about using REVERSE or RIGHT.. but the results are coming off as really bizarre...
Lets say i have a table column 'DESCRIPTION' in a table LOANS with an entry
Changed Loan Laptop from "IT-X130E-10" to "IT-X130E-9".
and i want the last substring
IT-X130E-9
within delimiters '"' hope its clearer now
Can try SUBSTRING INDEX(str,delim,count)
SELECT SUBSTRING_INDEX(
SUBSTRING_INDEX('yourString', '"', -2), '"', 1);
The key to this is reverse() and some brute force:
select replace(replace(right(val, instr(substring(reverse(val), 3, 100), '"')+2), '"', ''), '.', '')
from (select 'Changed Loan Laptop from "IT-X130E-10" to "IT-X130E-9".' as val) t
This assumes that the string does not contain either '"' or '.'.
Related
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), ',', '.'))
There are strings in table
+1-123-456-7890
11234567890
+1 1234567890
1777555999
I have variable $phoneNumber. I want to pass variable in sql query to get matching records.
example:
$phoneNumber holds the value 1234567890
and using same variable I want to fetch following records in one query
+1-123-456-7890
11234567890
+1 1234567890
I want to select both records in one query.
how can I match data (11234567890) with above saved numbers?
We can do this following the replacement logic already suggested here combined with regular expressions, to match only the phone numbers with the format you want to target:
WITH yourTable AS (
SELECT '+1-123-456-7890' AS phone UNION ALL
SELECT '+1 1234567890' UNION ALL
SELECT '+6512345678'
)
SELECT
phone,
REPLACE(REPLACE(REPLACE(phone, ' ', ''), '+', ''), '-', '') AS common_phone
FROM yourTable
WHERE phone REGEXP '\\+[0-9][[:space:]-][0-9]{3}-?[0-9]{3}-?[0-9]{4}';
Demo
Try this !
Regex :
[\+]?[0-9](?:[-| ])?\d{3}[-]?\d{3}[-]?\d{4}
Verify through regxe101:
If both $phonenumber and your string column could (potentially) both have the troublesome characters, this is hard to solve with regular expressions.
where replace(replace(replace($phonenumber, ' ', ''), '-', ''), '+') = replace(replace(replace(phonecol, ' ', ''), '-', ''), '+')
I solved this with using replace() in query.
I removed special characters from variable $phoneNumber (not through sql but before passing varable to sql) and compared it with the string result of the replace. SELECT phone FROM myTable where REPLACE(REPLACE(REPLACE(phone, ' ', ''), '+', ''), '-', '') = $phoneNumber;
Rethink the overall design.
Cleanse phone numbers before putting them into the table. Then that SELECT, and the other SELECTs that are yet to be written, will be simple.
I have a column that contains a string of comma delimited values. I use FIND_IN_SET to query this column and it works fine until there is a space between the value and the ,. I cannot control the input. The only solution I have found that works is by running REPLACE on the column within the FIND_IN_SET function. Unfortunately this will remove all spaces and could return undesired results.
The blow example would return both row in the table as opposed to the first one only.
col1 | col2
carpet , foo, bar | myVal1
abc, 123 , car pet | myVal2
Query
SELECT FIND_IN_SET('carpet', REPLACE(col1, ' ', ''));
Is there a way of limiting this to only trim the space wither side of the ,
You could try replacing ,[ ] or [ ], with just comma:
SELECT
col1,
col2,
FIND_IN_SET('carpet', REPLACE(REPLACE(col1, ', ', ','), ' ,', ',')) AS output
FROM yourTable;
Demo
Note: This answers assumes that there would be at most one leading/trailing space around the commas, and that your actual data itself does not contain commas. If there could arbitrary amount of whitespace, this answer would fail. In that case, what you would really need is regex replacement. MySQL 8+ does support this, but a better bet would be to normalize your data and stop storing CSV data like this.
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)
)
I have a table which i am using to query and getting its one column which matches regular expression which is (\/.+\/\?).
Content of the resulted column is like:
/Anything here/?
Example output:
\abc\cdf\?....
\ab\?....
\abc\cdf\?....
\sb\?....
where '....' can be anything
Desired result i want is unique values before \? such that rows with duplicate regexp matched content are shown once only like here (\abc\cdf\?.... showing twice instead of onece)
\abc\cdf\?....
\ab\?....
\sb\?....
OR
\abc\cdf\?
\ab\?
\sb\?
I have looked very much but couldn't find anything there is regexp_substr in oracle but that is not working in SQL.
Please if someone could help me with the sql query that would be awesome.
If you want everything before the last \, then you can use substring_index() and some string manipulation:
select substring_index(col, '\\',
length(col) - length(replace(col, '\\', ''))
) as firstpart,
count(*)
from table t
group by substring_index(col, '\\',
length(col) - length(replace(col, '\\', ''))
);