Mysql cut string, the first character? - mysql

Hi is it possible to cut string like this:
String in "data" columns: ,123,456
Cut the first character i.e "," (comma).
So the query is something like:
Update users set data = cut first string...

UPDATE users SET data = SUBSTR(data, 2);
This will iterate through all rows in users and replace data with itself minus the first character.

Related

How to receive mysql query output as list instead of tuple?

I am using the following query to get a list of ids corresponding to the given parameters:
userids=[]
sql_select_query = """SELECT userid FROM database1 WHERE username = %s"""
cursor.execute(sql_select_query, (username,))
record = cursor.fetchall()
for row in record:
userids.append(row)
print(userids)
I get a result like:
[('1460871223475326979',), ('1460871240332238850',), ('1460871258518736898',), ('1460871271219085312',), ('1460871286180220941',), ('1460871308963680260',)]
I would like to get this result as a list without the brackets, quotes and braces as 1460871223475326979, 1460871240332238850, 1460871258518736898, 1460871271219085312, 1460871286180220941, 1460871286180220941, 1460871308963680260
I tried the
",".join(map(str,userids))
method, but it only removed the [ ] at the start and end and did nothing for the braces and quotes.
Try this:
You can perform indexing on the list of tuples. All you have to do is,
for row in record:
userids.append(row[0])
print(userids)
Indexing at zero returns only the first element of each tuple in the list.```

Remove All data in string after 10th entry

I have this data in a string 0871234567ThisPartOfTheStringIsRandom
How Do I update the string to just keep the first 10 Chars?
Please Keep in mind I have thousands of entries where 'ThisPartOfTheStringIsRandom' is different in every case
The LEFT function is a string function that returns the left part of a string with a specified length.
UPDATE TableA
SET YourColumn = LEFT(YourColumn,10)

How to split this String in two parts?

I would like to split a string like this in Access 2000 (Visual Basic function):
"[Results]
[Comments]"
in two parts:
the results part
the comments part
As you can notice, these two parts are separated by an empty line (always, this is our separator).
[Results] and [Comments] are blocks of text. We don't care what's in it except:
the results part doesn't have any empty lines in it, so the first empty line we see is the separator one.
I want my function to extract the Comments part only.
Here is what i tried:
Public Function ExtractComm(txt As String) As String
Dim emptyLine As Integer
txt = Trim(txt)
'emptyLine = first empty line index ??
emptyLine = InStrRev(txt, (Chr(13) + Chr(10)) & (Chr(13) + Chr(10)))
'Comments part = all that is after the empty line ??
ExtractComm = Mid(txt, emptyLine + 4)
End Function
But it doesn't work well.
If I do:
ExtractComm(
"Res1
Res2
Comment1
Comment2"
)
I want to obtain:
"Comment1
Comment2"
but I only obtain Comment2. Any idea to extract the comment part ?
Thanks a lot !
Maybe you need to use InStr instead of InStrRev
InStrRev
Returns the position of the first occurrence of one string within another, starting from the right side of the string.
InStr
Returns an integer specifying the start position of the first occurrence of one string within another.

Incrementing numerical value and changing string in SQL

I have a database that has stored values in a complicated, serialized array where one component is a string and another is the length of the characters of the string, in this format:
s:8:"test.com"
Where "s" holds the character length of the string in the quotations.
I would like to change the string from "test.com" to "testt.com", and I'm using the following statement in SQL:
UPDATE table SET row=(REPLACE (row, 'test.com','testt.com'))
However, this breaks the script in question, because it doesn't update the character length in the "s" preceding the string where "test.com" is stored.
I was wondering if there is a query I can use that would replace the string, and then also increment the value of this "s" preceding to where the replacement occurs, something like this:
UPDATE table SET row=(REPLACE (row, 's:' number 'test.com','s:' number+1 'testt.com'))
Does anyone know if this kind of query is even possible?
UPDATE table set row = concat('s:',length('testt.com'),':"testt.com"');
If you need to change exact string, then use exact query -
UPDATE table SET row = 's:9:"testt.com"' WHERE row = 's:8:"test.com"';
The string is a "serialized string".
If there are multiple strings to be replaced, it might be easier to create a script to handle this.
In PHP, it goes something like this:
$searchfor = serialize('test.com');
$replaceby = serialize('testt.com');
// strip last semicolon from serialized string
$searchfor = trim($searchfor,';');
$replaceby = trim($replaceby,';');
$query = "UPDATE table SET field = '$replaceby' WHERE field = '$searchfor';";
This way, you can create an exact query string with what you need.
Do fill in the proper code for db connection if necessary.

mysql replace last characters in string if matched

I have a table that has some rogue tags that need replacing
The offending string ends <tr> and needs replacing with </table>
Not all record are affected, so I need to find these and then replace them
Our skills using Update Replace Where are limited as the characters are not unique within the string but their position is, ie the last 4 characters
Have tried using
UPDATE table
SET field
REPLACE (RIGHT(field,4),</table>)
but suspec this is over simplified (and also fails)
try this:
UPDATE table
SET field=concat(left(field,length(field) -4),'</table>')
I had a similar situation in which needed to replace '_' from end of the transaction number field, where there where more than one occurrences of _ in field. Example: 20161124_C_BGN_5570.77_ & 20161121_C_HRK_1502360000__
Solution:
UPDATE temp
SET transaction = LEFT(transaction, LENGTH(transaction) -1)
WHERE RIGHT(transaction, 1) = '_';
// in case of double underscore (__)
UPDATE temp
SET transaction = LEFT(transaction, LENGTH(transaction) -2) # WHERE id = xxx WHERE RIGHT(transaction, 2) = '__';