My client had input both the city and state (ex: Atlanta, GA) in the city field. The city field is in the rmn_wpjb_job table in a column named job_city.
I want to be able to put the state portion of what the client had entered in the job_state column which has all empty rows right now. The end result would have just the city in the job_city column and the state in the job_state column. I would need to remove the comma in the job_city column.
My web hosting support gave me this query to start:
SELECT SUBSTRING_INDEX(job_city, ',', -1)
FROM `rmn_wpjb_job`
This works fine, but I want to take the results of this query and put it into the job_state column.
There are 500 rows that I need to do this for. Thanks in advance for the help.
You can perform an UPDATE operation
update rmn_wpjb_job
set job_state = SUBSTRING_INDEX(job_city, ',', -1)
where <some_condtion_if_needed>;
Related
In my database table, Due to some mistake same row has been entered twice, But it is now having one column value with some appended numeric value in the duplicate entry.
For example,
If my table has a column named filename, Then in one row it has value 'some-random-name.pdf'.
And in the duplicate row, it has value 'some-random-name-1532.pdf'.
I need to identify all such records. Please note that there can be any or zero number of dash(-) in the filename. So Like query something like '%-____-%.pdf did not help me.
Assuming that complete filename contains only one dot between name and extension you may try this:
WHERE SUBSTRING_INDEX(t1.value, '.', -1) = SUBSTRING_INDEX(t2.value, '.', -1)
AND LOCATE(SUBSTRING_INDEX(t1.value, '.', 1), SUBSTRING_INDEX(t2.value, '.', 1)) = 1
I have a table that contains multiple fields - let's say FieldA, FieldB etc. and finally Location. The Location field has values such as:
http://192.168.1.10/location?n=5
http://192.168.1.10/location?n=8
http://192.168.15.6/location?n=1
http://192.168.0.9/location?n=11
http://192.168.15.6/location?n=5
http://192.168.0.9/location?n=6
http://192.168.1.10/location?n=2
I need to get the unique values of the IP addresses in the Location field. In other words, from the above example data, I should get
http://192.168.1.10
http://192.168.15.6
http://192.168.0.9
Based on this answer, I am using the following SQL - without much luck
SELECT * FROM `table` WHERE FieldA = 'Example' GROUP BY (SELECT SUBSTRING_INDEX("`table`.Location", "/", 3))
The above gives me just a single record. What am I doing wrong?
Making judicious use of SUBSTRING_INDEX:
SELECT DISTINCT SUBSTRING_INDEX(SUBSTRING_INDEX(Location, '/', 3), '/', -1) AS distinct_ips
FROM yourTable;
Demo
For an explanation on how the above logic works, consider the location value http://192.168.1.10/location?n=5. The inner call to SUBSTRING_INDEX returns http://192.168.1.10, which is everything to the left of the third forward slash. Then, the outer call returns everything to the right of the last forward slash, which leaves us with the IP address.
I need to eliminate the result of the Select statement
if a certain amount of numbers appear at the end.
Example:
select name from table where registration = '1234'
if the data record is:
Joe Bin12345
I need the result to lose its registration number when printing:
Joe Bin
The result should loose its digits
only if it contains five numeric characters.
SELECT name,
REGEXP_REPLACE(name, '[0-9]{5}$', '') cleared
FROM test;
fiddle
Good Evening Everyone;
I have a table in MySql that I would like to update as follows. Just as an FYI I have searched this topic and have yet to find a solution.
The table name is ALK_Results_NEW in a MySQl database, the data in the ICD9 column contains data seperated by commas.
I need to keep the first set in the ICD9 column so for example the first row has V57.9 , 246.9.
I need to keep the V57.9 in the ICD9 column and move 246.9 into the ICD9_SECONDARY column.
If a row has more than 2 then I need to move all other into the ICD9_OTHER column (This column can have mulitple ICD9 Codes separated by commas)
To summarize the first code needs to stay in the ICD9 column and the second set of codes needs to be moved into the ICD9_SECONDARY. After the data is moved the ICD9 Column should only have the forst set of codes.
Any help would be greatly appreciated.
Assuming the two columns already exist in the table, you can change the data using an update:
update alk_results_new
set icd9_secondary = substr(icd9, instr(icd9, ',') + 1),
icd9 = substring_index(icd9, ',', 1)
where icd9 like '%,%';
EDIT:
Oops, I didn't realize there were three columns. The approach is similar, but a little more complicated because you need to take into account the length of the strings. I think the following should do what you want:
update alk_results_new
set icd9_other = (case when icd9 like '%,%,%'
then substr(icd9, length(substring_index(icd9, ',', 2)) + 2)
end),
icd9_secondary = (case when icd9 like '%,%'
then substring_index(substring_index(icd9, ',', 2), ',', -1)
end),
icd9 = substring_index(icd9, ',', 1);
Note: test the logic out on a select before running the update.
I'm trying to query the first letter of a last name in MySQL. I want to skip the first name and query the a certain letter in the last name. Thanks
In SKU_data, which SKU has a buyer whose last name begins with 'M'?
*/
select *
from sku_data
where buyer ;
In your where clause, search on WHERE buyer.LastName LIKE 'M%'. This will return all results that start with M.
You need to use a combination of the substring method and the substring_index method in your sql query.
Your select query should look something like this
SELECT SUBSTRING(last_name,1,1)
I'm assuming that your name field has both first and last name in it, instead of separate fields for first and last name. Use the substring_index method to figure out what last_name should be:
SUBSTRING_INDEX(full_name,' ',-1)
Combining this SQL, we have:
SELECT SUBSTRING(SUBSTRING_INDEX(buyer,' ',-1),1,1) as first_letter
Now you can use the first_letter field in a where clause to get your desired result:
SELECT *, SUBSTRING(SUBSTRING_INDEX(buyer,' ',-1),1,1) as first_letter
from sku_data
where first_letter = 'M' ;
Assuming that you have a field like say "full_name" which has first name and last name in the same column.
Lets say the full_name in table employee has a value "JEFF MOSTI"
You can simply use the following statement to get what you need
select * from employee where full_name regexp ' M';
I am assuming you are looking for the last name to start with 'M' and that there is a space (' ') between the first and last name.