I need a scenario where results need to be sorted out depending on the number of periods in it.
Field values are:
aa.bb.ee.tt
aa.tt.ee
aa.tt
If I want to search only values which consist on a single period, what will be the query?
I need a scenario where results need to be sorted out depending on the number of periods in it.
Field values are:
aa.bb.ee.tt
aa.tt.ee
aa.tt
If I want to search only values which consist on a single period, what will be the query?
if column name is str, and you put substr = "." in the following SQL, you will get what you want. Sorted by number of periods in the column str
SELECT (LENGTH(str) - LENGTH(REPLACE(str, substr, ''))) / LENGTH(substr) AS cnt
...
ORDER BY cnt DESC
UTF-8
. see here stackoverflow.com/questions/1734334/mysql-length-vs-char-length
– John Woo
Aug 14 '12 at 9:29
Since there is no inbuilt MySQL function to count the occurrence of a character in string, we can do it by using these steps:
Ex.
SELECT CHAR_LENGTH('hello') - CHAR_LENGTH(REPLACE('hello', 'l', '')) AS `COUNT`
returns 2
and if you want to order the results,
SELECT CHAR_LENGTH(colName) - CHAR_LENGTH(REPLACE(colName, xChar, '')) AS `COUNT`
FROM tableName
ORDER BY `COUNT` DESC
WHERE
colName
is the name of the column that contains your string
xChar
is the character you want to find
BTW, use CHAR_LENGTH()
instead of LENGTH()
because LENGTH()
returns the length of the string measured in bytes. CHAR_LENGTH()
returns the length of the string measured in characters.
private void CheckPeriod(string stringLine)
{
int counter = 0;
foreach (char c in stringLine)
{
if(c == ".")
{
counter = counter + 1;
}
}
if(counter == 1)
{
//Do code
}
}
Hope this helps, or points in the right direction
hmm you could something like
SELECT * FROM TABLE_NAME WHERE COL_NAME REGEXP "[A-Za-z0-9]*\.[A-Za-z0-9]*"
for getting single period values and add more periods same way for more than one period results