How to get last 12 digits from a string in MySQL? - mysql

How would I get last 12 digits of a string using mysql?
Let's say I have a varchar field with a tracking number, that may be anywhere from 5 to 20 varchars long. But I only need to select last 12 digits or less if there are less.
so in a field = 12345678123456789012
I would only need to get what's in brackets
field = 12345678[123456789012]
I saw a few examples using mid, etc, but they dont' produce the desired result or I can't find an example that makes sense :-(
Thank you.

SELECT RIGHT(field, 12);

Nick,
Try using the RIGHT(str, len) function.
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_right
I'm not sure of the semantics if the string is shorter than length as I don't have access to MySQL but it might do what you're looking for.

Related

How to determine the right datatype for columns

Please look at my screenshots and help me to understand what I am missing.
What datatype should I choose for these columns in MYSQL? I keep getting mistakes in decimal datatype columns. I chose decimаl12,3 because no columns(revenue, product&purchase price) with currency have more than 12 digits in total, 9 before and 3 after the decimal point. Could someone help me to understand what data type to choose with examples?
if we have an integer number e.g. 85192 we choose int?
for currency we choose the decimal, right? then what have I done wrong that I keep getting errors? 0 records imported.
if we have a combination of numbers and letters or just letters then we choose varchar? and varchаr1 equals 1 character, eg. apple32 = 7 characters, therefore vаrchar7?
turning to decimal, 12,464.87 in total 7 digits, 5 before and 2 after the decimal point, hence mysql decimаl7,2 should be enough, right? or would it be better to put decimаl10,3 with a margin so to say.
excel
mysql
data
$1,000.00 contains two characters that cannot be part of a numeric literal: the dollar sign and the comma that is used as a thousands separator.
Find a way to change '$1,000.00' to '1000.00' in the input file. Then, the load will succeed.
Alternatively, create an intermediate table where product_price is a VARCHAR(32), load into that, and then:
INSERT INTO target_table
SELECT
other_col1
,other_col2
, ....
,CAST(REPLACE(REPLACE(product_price,',',''),'$','') AS DECIMAL(15,2)
,other_col_n
,...
FROM staging_table;
You don't need an intermediate table. When doing LOAD DATA, put and columns into #variables; then use a SET to convert as needed:
LOAD DATA
...
col1, col2, #price, ...,
SET price = CAST(REPLACE(REPLACE(product_price,',',''),'$','') AS DECIMAL(15,2))
Dates need to be like this: "2022-07-25 22:02:22". Either change what Excel is delivering, or use STR_TO_DATE(...) in the SET.

mysqli - I am trying to get substring working

SELECT update_log, update_idccode, update_filenumber, update_filetype, update_timedate
FROM updates_log
WHERE substring(update_idccode,0,7) ='idc2997%' AND update_filetype = 'E'
ORDER BY update_log DESC
I am trying to get this to get the first 7 characters of my update_idccode table column. I cannot get it to work. Any thoughts?
You're pulling out 7 chars from your column, and comparing them against an 8-char string. In other words, it is imposssible for a 7-char word to ever be identical to an 8-char word.
Try
WHERE substring(update_idccode,0,7) = 'idc2997' ...
instead. As an alternative,
WHERE update_idccode LIKE 'idc2997%' ...
would also work. The % suggests you may have been trying this, but % is only relevant as a wildcard in LIKE comparisons, not = equality testing.

how to sort varchar numeric columns? [duplicate]

This question already has answers here:
Natural Sort in MySQL
(22 answers)
Closed 8 years ago.
I write ...
ORDER BY column ASC
but my column is VARCHAR and it sorts wrong like
I want to sorting numeric value but its datatype is varchar then how
value like this
1.2.840.113619.2.55.3.163578213.42.1355218116.691.1
1.2.840.113619.2.55.3.163578213.42.1355218116.691.10
1.2.840.113619.2.55.3.163578213.42.1355218116.691.100
1.2.840.113619.2.55.3.163578213.42.1355218116.691.101
1.2.840.113619.2.55.3.163578213.42.1355218116.691.2
1.2.840.113619.2.55.3.163578213.42.1355218116.691.20
but i want to in sequence last
1.2.840.113619.2.55.3.163578213.42.1355218116.691.1
1.2.840.113619.2.55.3.163578213.42.1355218116.691.2
1.2.840.113619.2.55.3.163578213.42.1355218116.691.10
1.2.840.113619.2.55.3.163578213.42.1355218116.691.20
1.2.840.113619.2.55.3.163578213.42.1355218116.691.100
1.2.840.113619.2.55.3.163578213.42.1355218116.691.101
and also I have string like this
1.2.840.114257.0.10325113632288210457800001002296133400001
1.2.840.114257.0.10379710976288210457800000002296491200000
1.2.840.114257.0.10328923264288210457800000002296158400001
I also want to sort this ...
For your specific example, you can do:
ORDER BY length(col), col
If you have other examples, you might need to "parse" the values using substring_index().
I have tried the following and it is working fine.
First Remove the "." From the String so you have only numeric and then sort by using ABC() Function so, It will not truncated.
SELECT yourcol AS v FROM test ORDER BY ABS(REPLACE(yourcol, '.', '')), yourcol;
I think you should redesign your schema,
If you have only digits in ur input than go for #Gordon Linoff answer
Or may your input also contain character string(eg: ankit123) and you want sort in order to that char and number also than you may go for this
You have to implementing any Algorithm like this
Lets take eg
1 >1.2.1
2 >1.2.10
3 >1.2.2
Now make all digits equal, Decide any max length(eg: 5) so attach 0 ahead to make all digits with same length
now above string look like
1 >00001.00002.00001
2 >00001.00002.00010
3 >00001.00002.00002
Now if you you fire ORDER BY on this string you get your output in sorted order.

Finding number of occurence of a specific string in MYSQL

Consider the string "55,33,255,66,55"
I am finding ways to count number of occurence of a specific characters ("55" in this case) in this string using mysql select query.
Currently i am using the below logic to count
select CAST((LENGTH("55,33,255,66,55") - LENGTH(REPLACE("55,33,255,66,55", "55", ""))) / LENGTH("55") AS UNSIGNED)
But the issue with this one is, it counts all occurence of 55 and the result is = 3,
but the desired output is = 2.
Is there any way i can make this work correct? please suggest.
NOTE : "55" is the input we are giving and consider the value "55,33,255,66,55" is from a database field.
Regards,
Balan
You want to match on ',55,', but there's the first and last position to worry about. You can use the trick of adding commas to the frot and back of the input to get around that:
select LENGTH('55,33,255,66,55') + 2 -
LENGTH(REPLACE(CONCAT(',', '55,33,255,66,55', ','), ',55,', 'xxx'))
Returns 2
I've used CONCAT to pre- and post-pend the commas (rather than adding a literal into the text) because I assume you'll be using this on a column not a literal.
Note also these improvements:
Removal of the cast - it is already numeric
By replacing with a string one less in length (ie ',55,' length 4 to 'xxx' length 3), the result doesn't need to be divided - it's already the correct result
2 is added to the length because of the two commas added front and back (no need to use CONCAT to calculate the pre-replace length)
Try this:
select CAST((LENGTH("55,33,255,66,55") + 2 - LENGTH(REPLACE(concat(",","55,33,255,66,55",","), ",55,", ",,"))) / LENGTH("55") AS UNSIGNED)
I would do an sub select in this sub select I would replace every 255 with some other unique signs and them count the new signs and the standing 55's.
If(row = '255') then '1337'
for example.

MySQL Regexp won't work

I have the following query:
SELECT `admin_users`.*
FROM `admin_users`
WHERE (avatar REGEXP 'avatars/Name-[0-9]+.jpg+')
ORDER BY `admin_users`.`avatar`
DESC LIMIT 1
It's ok if I have something like:
avatars/Name-5.jpg
avatars/Name-6.jpg
But if I have, avatars/Name-15.jpg, for example, it doesn't return in query.
In other words, It only works for 1 digit, not for more. How can I solve it?
When comparing strings (an that is what avatar is), "avatars/Name-1..." comes before "avatars/Name-5..." simply because the string "1" comes before "5".
It is not practical to order those by an embedded number. This would do what you want, but it is pretty cryptic:
ORDER BY 0 + MID(avatar, 14)
To explain
MID will start at the 14th character of 'avatars/Name-15.jpg' and extract '15.jpg'.
0+ will take that string, convert it to a number and deliver the number 15. (When a string is turned into a number, the first characters are taken as long as it looks like a number. So, 0+'abc' will deliver 0, since there is nothing at the beginning of abc that looks like a number.)
If the left part were not exactly 14 characters in all cases, the trick will fail. And it may get so complicated as to be 'impossible' in SQL.