In a table having an integer primary key of indexRow
in which the last two digits are currently 55, I'd like to change that to 50 but only if the column added
is an integer value 55 and the indexRow
ends in 55. I'm using SQLite.
I tested it as follows. Would you please tell me if this is the correct approach (if there is a better method) because I'd like to use it to run an update on the table?
Of course, I'll do it within a transaction and test before committing; but wanted to ask. I expected to have to use some math to determine which indexRows ended in 55, but converting to string seems quite easy.
select indexRow, indexRow-5, substring(format('%s', indexRow),-2)
from newSL
where added=55
and substring(format('%s', indexRow),-2)='55'
limit 10;
indexRow indexRow-5 substring(format('%s', indexRow),-2)
----------- ----------- ------------------------------------
10080171455 10080171450 55
10130031255 10130031250 55
10140021655 10140021650 55
10140080955 10140080950 55
10240330155 10240330150 55
10250230555 10250230550 55
10270031155 10270031150 55
10270290355 10270290350 55
10300110355 10300110350 55
10300110455 10300110450 55
CodePudding user response:
Yes, use the modulo operator, %
. In the expression x % y
, the result is the remainder of dividing x
by y
. Therefore, 4173 % 100 = 73.
Note that %
is a math operator, just like * for multiplication and / for division, and is not related to using the %
in the format function.
CodePudding user response:
you can use the string representation of the integer and check the last two characters to determine if it ends in 25. For example:
in python
if str(x)[-2:] == "25":
print("The integer ends in 25.")
else:
print("The integer does not end in 25.")
Here, the string representation of x is obtained using the str() function. The slice [-2:] is used to extract the last two characters of the string, and the result is compared to the string "25" to determine if the integer ends in 25.
Both of these methods are valid ways to determine if an integer ends in specific two digits in Python. Which method you choose will depend on your specific needs and preferences.