In a mysql table i have column whit this info..
Col.
tr10
tr210
zbr10
00010
10010
tr 10
The question is simple, i need to find in a mysql query all the records number 10.. but as you can see in the example not 10010 etc..
Result:
tr10
zbr10
00010
tr 10
I know is a mess but the records had to be load in that form..
so you have characters at the begining, in some cases spaces, or zeros..
An option could be extract (by hand) hundred of characters to another column to keep the things less complex, but at the same time i still having problems with the 000010 values..
Use regular expressions
select * from table where col regexp '^[a-z]+10$'
Play with the regex until you get your desired results, i didnt fully understand you criteria so I just made one up but the one in my example will pull all the rows with any alpha characters proceeded by 10
Related
First let me apologize I have not been successful in finding anything online with this specific scenario.
I have been using MySQL for quite some time, but I am hoping to get some clarification on a certain situation I have come across, which honestly bothers me quite a bit.
I'm trying to match a string in a MySQL column that contains both \ and % literal characters using the LIKE operator.
Inside the table I have two records:
id value
-----------------------
1 100\\%A
2 100\%A
They both contain literal special characters.
If I do a SELECT, in an attempt to only match the first record (id=1), I would expect to write the query as such:
SELECT * FROM table_name WHERE value LIKE '%0\\\\\%A'
(\\\\ to match two literal backslashes, plus a backslash before % to match a literal %)
However, It only matches the row (id=2), which makes no sense to me.
If I change the query a little to be:
SELECT * FROM table_name WHERE value LIKE '%0\\\\%A'
I would expect to match the id=1 row only, (\\\\ to match 2 literal backslashes, and the % is not literal and should represent a wildcard). But instead, it matches both rows?
row (id=2) only has a single backslash but still matches.
Is row id=2 matching because the first 2 backslashes are matching the \, the 3rd backslash is ignored for some reason, and the 4th backslash is allowing a literal match on the %?
If I do a:
SELECT * FROM table_name WHERE value LIKE '%0\\\\\\\%A'
I for some reason get row (id=1), when I would expect to get no matches whatsoever.
I'm trying to find a solution in which I can do partial matches on any series of characters accurately, including those with consecutive special characters such as the scenario above. However, I'm having an impossible time trying to plan for situations such as these.
Any input would be greatly appreciated.
Maybe this help you understand the usage of escape chars in mySQL
https://stackoverflow.com/a/27061961/634698
I'm using MySQL. I have a table with a field called UnitName. I just want to display all the UnitNames in order. The Unit Names can vary:
101
T-101
G-202
1005
O-1305
When I display them using ORDER BY UnitName, The results are not sorted properly. For instance 1000 will come before 101, T-2000 will come before T-201. If they were all numeric values, I would try to use CAST(UnitName AS INT), but unfortunately I have to deal with the Alpha (and other '-') characters.
I believe the way to approach this is to make sure that the UnitNames have the same length, and then they will sort properly. So basically I am trying to insert a number of zeros into the middle of the string until the lengths are identical and then ORDER BY that.
My query currently is listed below.
<cfquery name="Get"datasource="rent">
SELECT * FROM Units
ORDER BY Left(UnitName,2)+#RepeatString(0, 10-'LEN(UnitName)')#+right(UnitName, 'LEN(UnitName)'-2)
</CFQUERY>
I'm basically saying sort by "the 2 left characters of the string+(a number of 0s determined by the number 10 minus the length of the string)+the characters on the right side of the string (except the first 2).
When I just do a CFOUTPUT of this the UnitName comes out perfectly. I do have to had some # to make it work: #Left(UnitName,2)##RepeatString(0, 10-'#LEN(UnitName)#')##right(UnitName, len(UnitName)-2)#
But the query fails to execute as written below. I've narrowed it down to the middle section where I'm trying to determine the number of Zeros to insert. Specifically if I use:
#RepeatString(0, 10-5)#, the query will execute. But the minute I put 'LEN(UnitName)' instead of 5, the query fails. Any idea what I'm doing wrong?
Any insight is appreciated--I've been banging my head against a wall.
I'm making a serial system for my site, and it using the MySQL CHAR_LENGTH function to get all the serials with the matched length. Thing is, it's returning strings that are more than I'm looking for. I've also tried with CHARACTER_LENGTH and LENGTH, having no impact on the result :/.
This is my I/O:
SELECT serials.code
FROM serials
WHERE CHARACTER_LENGTH(code) = 12
These are the results:
niels-er-sej
KUVX-21-40
KUVX-21-40
As you can see, the last two, are only 10 characters, yet they show up when I search for 12?
I've added a picture of the Character lengths as request:
I have these rows :
ROW 1 containing: How to .405
ROW 2 containing: How to 405
When i use the contains query such as :
Contains( FIELDNAME, '"405"' )
Or this query :
Contains( FIELDNAME, '"405*"' )
I got only the ROW 2 but not the ROW 1 because of the period.
I tried creating an empty stoplist and assigned it to the fulltext, it does not work.
I tried the FREETEXT query, it does not work also.
What is weird is when i add one caracter more to the value of ROW 1 (How to .405M), it works....
How to return also the ROW 1 when querying with only "405" ?
Thanks
Well, a bit old question but:
You did not tell what language you are using, so I will assume you are using english.
That happens because .405 is treated like a number. In English, .405 is a decimal value meaning 0.405. So the ft search will try to find numbers with 405 in the decimal part of them. Ft will also try to find rounded numbers similar to .405.
You should check my question: sql-server-2008-fulltext-rounding-money-value
I am trying to match a list of motorcycle models to a series of ebay codes for listing motorcycles in ebay.
So we get a motorcycle model name that will be something like:
XL883C Sportster where the manufacturer is Harley Davidson
I have a list of ebay codes that look like this
MB-100-0 Other
MB-100-1 883
MB-100-2 1000
MB-100-3 1130
MB-100-4 1200
MB-100-5 1340
MB-100-6 1450
MB-100-7 Dyna
MB-100-8 Electra
MB-100-9 FLHR
MB-100-10 FLHT
MB-100-11 FLSTC
MB-100-12 FLSTR
MB-100-13 FXCW
MB-100-14 FXSTB
MB-100-15 Softail
MB-100-16 Sportster
MB-100-17 Touring
MB-100-18 VRSCAW
MB-100-19 VRSCD
MB-100-20 VRSCR
So I want to match the model name against the list above using a regExp pattern.
I have tried the following code:
SELECT modelID FROM tblEbayModelCodes WHERE
LOWER(makeName) = 'harley-davidson' AND fnmodel REGEXP '[883|1000|1130|1200|1340|1450|Dyna|Electra|FLHR|FLHT|FLSTC|FLSTR|FXCW|FXSTB|Softail|Sportster|Touring|VRSCAW|VRSCD|VRSCR].*' LIMIT 1
however when I run the query I would expect the code to match on either MB-100-1 for 883 or MB-100-16 for Sportster but when I run it the query returns MB-100-0 for Other.
I am guessing that I have the pattern incorrect, so can anybody suggest what I might need to do to correct this?
Many thanks
Graham
[chars] matches any of the characters 'c','h','a','r','s'
So by giving it such a long list, it will inevitably match just the first item (single character)
Try this instead
LOWER(makeName) = 'harley-davidson' AND fnmodel REGEXP '(883|1000|1130|1200|1340|1450|Dyna|Electra|FLHR|FLHT|FLSTC|FLSTR|FXCW|FXSTB|Softail|Sportster|Touring|VRSCAW|VRSCD|VRSCR).*' LIMIT 1
You might also consider not using REGEX and using FIND_IN_SET instead.
Not really fully tested, but it should be something like this:
REGEXP '^MB-[0-9]+-[0-9]+[[:space:]]+(883|1000|1130|1200|1340|1450|Dyna|Electra|FLHR|FLHT|FLSTC|FLSTR|FXCW|FXSTB|Softail|Sportster|Touring|VRSCAW|VRSCD|VRSCR)$'
In detail:
^MB- Starts with MB-
[0-9]+ One or more digits
- Dash
[0-9]+ One or more digits
[[:space:]]+ One or more white space
(883|1000|...)$ Ends with one of these
Here's the reference for the regexp dialect spoken by MySQL:
http://dev.mysql.com/doc/refman/5.1/en/regexp.html
Answer to comment:
If you want to match the Sportster row them remove all other conditions. And you may not even need regular expressions:
WHERE fnmodel LIKE '% Sportster'