I need a MySQL function to get the left part of a string with variable length, before the separator.
For example, with separator string '==' :
abcdef==12345 should return abcdef
abcdefgh==12 should return abcdefgh
Also the same thing, but for the right part...
SELECT SUBSTRING_INDEX(column_name, '==', 1) FROM table ; // for left
SELECT SUBSTRING_INDEX(column_name, '==', -1) FROM table; // for right
select substring_index('abcdef==12345','==',1)
for the right part use -1 instead of 1.
I would look into the substring function in SQL which is SUBSTR, but it is more for set positions in the string, not so much for variable lengths.
http://www.1keydata.com/sql/sql-substring.html
Related
I have this filename AAAA_BBBBB_CC_HDDD_HGGG.csv and I'm trying to keep the values after the second underscore starting from the right.
So i want to keep any values just before _HDDD_HGGG.csv
This is my code:
SET #NFileN = REVERSE(SUBSTRING(REVERSE(#source_filename),1,CHARINDEX('_',REVERSE(#source_filename), CHARINDEX('_', REVERSE (#source_filename), 0) + 1)))
And this is the returned value:
(6 rows affected)
_HDDD_HGGG.csv
Instead of being AAAA_BBBBB_CC.
Does anyone has a clue for this?
You are taking a SUBSTRING from 1 till your CHARINDEX while your string is reversed. Either reverse your string again or use LEN to find the length of your string like so:
REVERSE(
SUBSTRING(
REVERSE(#source_filename),
CHARINDEX('_',
REVERSE(#source_filename),
CHARINDEX('_',
REVERSE (#source_filename),
0)+1)+1,
LEN(#source_filename)
)
)
p.s.: Added a second +1 to remove the "_" between CC and HDDD
p.p.s: CHARINDEX is a SQL Server function which I assume is what you are actually using. The MySQL equivalent would be POSITION, the equivalent for LEN would be LENGTH
I'm trying to search and replace mobile numbers with the full international code.
So where rows have 07970000007 to replace the beginning with +447970000007
UPDATE tblMemberImportClub
SET msisdn = REPLACE(msisdn, '07', '+447')
WHERE INSTR(msisdn, '07') = 1;
But this also replaces the other matches:
+4479700000+447
I don't think i can use TRIM as some rows will already start with +447 and will therefore nor require any updates.
Thanks in advance for any assistance.
Use LIKE and INSERT():
UPDATE tblMemberImportClub
SET msisdn = INSERT(msisdn, 1, 2, '+447')
WHERE msisdn LIKE '07%';
INSERT() is a string function that replaces exactly the characters you specify (see here).
SELECT
CONCAT(
REPLACE(
LEFT('07970000007',2), '07', '+447'
),
SUBSTRING('07970000007', 3, CHAR_LENGTH('07970000007'))
)as replaced
I have a DB column like this .. part_1/part_2?part_3
Of course I've tried SUBSTRING_INDEX but it does not return the central part of the string.
I want to select records that match part_2. How do I do that?
Instead of using SUBSTRING_INDEX, use this : SUBSTRING(str, pos, len)
str is the column,
pos is the position where you want to start the substring and
len is the length of the substring.
Check out this link for more on this.
You could try something like :
SELECT
SUBSTRING_INDEX(SUBSTRING(COLUMN_NAME,5), '?', 1) AS PART2
FROM TABLE_NAME
is it at all possible to do this?
for instance if i had
$row['price']
and the value of this was 15000
I would like it to show as 15,000 but I dont have a clue about how to go about this?
thanks for any help.
So, you want to FORMAT your data? How about using the FORMAT function?
FORMAT(X,D)
Formats the number X to a format like '#,###,###.##', rounded to D decimal places, and returns the result as a string. If D is 0, the result has no decimal point or fractional part. D should be a constant value.
mysql> SELECT FORMAT(12332.123456, 4);
-> '12,332.1235'
mysql> SELECT FORMAT(12332.1,4);
-> '12,332.1000'
mysql> SELECT FORMAT(12332.2,0);
-> '12,332'
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_format
EDIT: Sample query
As opposed to the SELECT * people use (too often), slightly modify your queries.
SELECT id, fullName, position, FORMAT(pay, 0) as pay
FROM Employees WHERE lastName = 'Rubble';
Try this:
$row['price'] = 15000;
echo substr($row['price'], 0, -3) . ',' . substr($row['price'], -3);
echo substr($row['price'], 0, -3); // == 15
echo substr($row['price'], -3); // == 000
The 0 is the start position of the string. The -15 is the negative end position of the string.
http://at2.php.net/manual/en/function.substr.php
If you'd like to do the formatting in MySQL then you can use the FORMAT() function: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_format
From PHP, you can use number_format:
$formatted = number_format($row['price']);
This will add a comma between every group of thousands. If you also want to format your price with a dot and two decimal places, use this:
$formatted = number_format($row['price'], 2);
I have a MySQL database table 'photos' with a column 'filename'.
I need to replace the spaces in the filename column values with underscores.
Is it possible with a single/multiple query? If so how?
You can use the REPLACE function :
REPLACE(str,from_str,to_str)
Returns the string str with all
occurrences of the string from_str
replaced by the string to_str.
REPLACE() performs a case-sensitive
match when searching for from_str.
So, to replace all occurences of a character by another one in all lines of a table, something like this should do :
update photos set filename = replace(filename, ' ', '_');
ie, you search for ' ' in the column filename and use '_' instead ; and put the result back into filename.
update photos set filename = replace(filename,' ', '_');