I want to substr of slice because I have a database that contains application name and their versions.
I'd like to get every version that needs to be followed. (every last version of each branch).
Those version name could be formatted 8.XX.YY.ZZZZ. And I'd like to get every last version for the 3 first set of number.
For exemple :
800000018
800000024
800010000
800010009
800020001
800020005
801000004
801000005
800020006
800020007
800010010
800020009
I would only get :
801000005 (8.1.0.5)
800020009 (8.0.2.9)
800010010 (8.0.1.10)
800000024 (8.0.0.24)
I've been thinking of a procedure that would do something like this :
get max version (here it'd be 8.1.0.5).
Then, get max version but substract by 1 if XX > 1 (8(XX -1))
And again, get max version and substract by 1 if YY>1 (800(YY-1))
Continue until there is only the 4 last number left and the first number that aren't 0.
In SQL it'd look like that :
select max(version_name) from application;
while (XX >0) SET XX=XX-1;
select max(version_name) from application where version_name LIKE '8XX';
while (YY>0) SET YY=YY-1;
select max(version_name) from application where version_name LIKE '800YY';
End there and last version_name should be = 80000ZZZZ.
Is there a way to substr if possible from the end of the string.
I have a filter in JS that transform it on the format that I want (VV.XX.YY.ZZZZ) if it can help :
parseInt(version.slice(0, -8)), parseInt(version.substr(-8, 2)),
parseInt(version.substr(-6, 2)), parseInt(version.slice(-4))
I hope it is possible.
you can group by substring and get the max
select max(version_name)
from application
group by
substring(version_name,1,1),
substring(version_name,2,2),
substring(version_name,4,2)
and order/parse it as you wish
My query in MYSQL
SELECT
CONCAT_WS(
'.',
SUBSTRING(VERSION, -9, 1),
SUBSTRING(VERSION, -8, 2) +0,
SUBSTRING(VERSION, -6, 2) +0,
SUBSTRING(VERSION, -4, 4) +0
) AS result,
VERSION
FROM
`test`
Output:
result version
--------------------
8.0.0.18 800000018
8.0.2.9 800020009
8.0.1.10 800010010
8.1.0.5 801000005
Related
I have a row who contains this value :
account :
adm.ahrgrst001
adm.ns2dhdujhd
adm.ff2hdjhh
adm.haidhidh103
adm.hshiksh122
adm.cn3ehuioe
i want to extract two different values:
when it ends like adm.hshiksh122 i want to extract hshiksh
and with start with adm.cn3ehuioe i want ehuioe
both without the adm. at the beginning
I have thinked this
IIF(isnumeric(RIGHT (account,3)),LEFT(account,LEN(account)-4),RIGHT (account,LEN(account)-7))
the value that are like adm.cn3ehuioe i got wrong like adm.cn3ehui
and adm.ahrgrst001 is correct ahrgrst
Thanks to everyone who will read
The correct way to get the 2 types of values is with:
account LIKE 'adm.[!0-9][!0-9]#[!0-9]*'
for the values that have 2 letters, 1 digit and letters after the dot, and:
account LIKE 'adm.*###'
for the values that end in 3 digits.
So use this:
SELECT IIF(
account LIKE 'adm.*###' ,
MID(account, 5, LEN(account) - 7),
MID(account, 8)
) AS result
FROM tablename
WHERE account LIKE 'adm.*###' OR account LIKE 'adm.[!0-9][!0-9]#[!0-9]*'
If there are no other values than these 2 types then you may remove the WHERE clause.
Results for your sample data:
result
ahrgrst
dhdujhd
hdjhh
haidhidh
hshiksh
ehuioe
I've got a database with a column that contains the following data:
aaa-1
aaa-2
aaa-3
...
aaa-10
aaa-11
...
aaa-100
aaa-101
...
aaa-1000
When I query and sort the data in ascending order, I get:
aaa-1
aaa-10
aaa-11
...
aaa-100
aaa-101
...
aaa-1000
...
aaa-2
...
aaa-3
Is this actually the correct (machine) way of sorting? Is the order being screwed up because of the aaa- prefix? How do I go about sorting this the way a human would (ie something that looks like the first snippet)?
P.S. If the problem does lie in the prefix, is there a way to remove it and sort with just the numeric component?
P.P.S. It's been suggested to me that I should just change my data and add leading zeroes like aaa-0001 and aaa-0002, etc. However, I'm loathe to go that method as each time the list goes up an order of 10, I'd have to reformat this column.
Thank you all in advance! :)
You can extract the number part, convert it to numeric data type and then do an ORDER BY:
SELECT mytable.*,
CAST(SUBSTRING_INDEX(mycolumn, '-', - 1) AS UNSIGNED) mycolumnintdata
FROM
mytable
ORDER BY mycolumnintdata;
If there are expressions which does not match number, the CAST function would return 0 and those records would be displayed first. You may handle this separately if needed.
I had a similar issue and the trick that did it for me was this one
*"ORDER BY LENGTH(column_name), column_name
As long as the non-numeric part of the value is the same length, this will sort 1 before 10, 10 before 100, etc."*
as given by Andreas Bergström on this question.
Hope that helps someone.
this is the alphabetical order,
you want numerical order,
for do this you must in the ORDER BY clause
trim the costant "aaa-" part
convert it in number
convert(SUBSTRING(val, 3), integer)
I will give you a sample sorting. Not based on your data sample, but this could help you out.
Say you have data like this :
id
----
1
2
6
10
13
when you do ORDER BY id ASC would return :
id
----
1
10
13
2
6
I suggest, use LPAD.
This query : SELECT LPAD('12',5,'0') return 00012
So when you have table data like I provide above, you can sort them like this :
SELECT * FROM TABLE
ORDER BY LPAD(ID,7,'0') ASC
Based on your data.
SELECT SUBSTR('aaa-100',5,LENGTH('aaa-100') - 3) return 100
So, SELECT LPAD( SUBSTR('aaa-100',5,LENGTH('aaa-100') - 3), 7, '0') return 00000100
So you can combine string function such as SUBSTR and LPAD. Do have any clue now?
I have a csv file that contains phone numbers, some of them have 9 digits and some of them have 10. Is there a command that would allow the transformation of the column such that numbers that have only 9 digits will have a 0 appended in front of the numbers.
For example,
if the column has values "443332332" and "0441223332", I would like to have the value of the one with 9 digits changed to "0443332332"?
Sorry, I should have elaborated.
I was wondering if there was a command to do it in SQLlite easily? I prefer not to use excel to transform the column as if I can get it to working with sqllite it would be so much easier and faster.
A more generic solution would be:
select substr('0000000000'||'1234567', -10, 10) from table_name;
The above query would always return 10 digits and add leading zeroes to the missed out number of digits.
For example, the above query would return : 0001234567
For Update, use
UPDATE TABLE_NAME SET PHONE_NO = substr('0000000000'|| PHONE_NO, -10, 10);
If you're sure that just prepending a zero on strings with length 9 will work for your application, something simple will work:
SELECT CASE WHEN LENGTH(phone_number) = 9 THEN '0'||phone_number
ELSE phone_number
END AS phone_number
FROM your_table
;
You could also update the table, depending on your needs:
UPDATE your_table
SET phone_number = '0'||phone_number
WHERE LENGTH(phone_number) = 9
;
Open the .csv using Excel,
Add a filter to the column,
Sort from A-Z to get all the columns with 9 digits,
Then follow the steps here
http://office.microsoft.com/en-au/excel-help/keep-leading-zeros-in-number-codes-HA010342581.aspx
I have a column (of VARCHAR type) with values like these:
1
2
3
1.1
1.1.1
1.2.1
5
4
7
8
9
10
10.2
10.1
I hop[e to select this column and order it naturally, like the following:
1
1.1
1.1.1
1.2.1
2
3
4
5
...
I have tried ordering it with this for example and a lot of other query
SELECT data
FROM sample
ORDER BY LEN(data), data
Does someone have an idea how to do this?
try this
ORDER BY data, LEN(data)
or this
ORDER BY CONVERT(SUBSTRING_INDEX(data, ',', -1), SIGNED), Len(data)
i give demo in mysql as tsql there is not in sqfiddle .
DEMO
You seem to be hoping to order a series of hierarchically named items in a natural order. It looks like these items' names take the form.
token [ .token [. token [ .token ]]]
where subsequent tokens after the first are optional.
I suppose you want each token, if it's numeric, to be handed as a number. That is, for example, you want 1.123 to come after 1.2 because 123 is numerically greater than 2.
You didn't say what you want done with alphabetical tokens, e.g. 401.k and 403.b. I suppose they should come after the numerical ones, but in lexical order.
This query (http://sqlfiddle.com/#!2/81756/2/0) will do the trick out to five hierarchical levels of tokens.
SELECT col
FROM T
ORDER BY
FLOOR(SUBSTRING_INDEX(col,'.',1)),
SUBSTRING_INDEX(col,'.',1),
FLOOR(SUBSTRING(col, 2+LENGTH(SUBSTRING_INDEX(col,'.',1)))),
SUBSTRING(col, 2+LENGTH(SUBSTRING_INDEX(col,'.',1))),
FLOOR(SUBSTRING(col, 2+LENGTH(SUBSTRING_INDEX(col,'.',2)))),
SUBSTRING(col, 2+LENGTH(SUBSTRING_INDEX(col,'.',2))),
FLOOR(SUBSTRING(col, 2+LENGTH(SUBSTRING_INDEX(col,'.',3)))),
SUBSTRING(col, 2+LENGTH(SUBSTRING_INDEX(col,'.',3))),
FLOOR(SUBSTRING(col, 2+LENGTH(SUBSTRING_INDEX(col,'.',4)))),
SUBSTRING(col, 2+LENGTH(SUBSTRING_INDEX(col,'.',4)))
Why does this work? FLOOR() converts the leftmost part of a string to an integer, so it picks up the leading integer. If it doesn't find any numbers in the string it's trying to convert, it returns zero.
And, SUBSTRING(col, 2+LENGTH(SUBSTRING_INDEX(col,'.',NNN))) picks up the part of the col item to the right of the NNNth dot.
The data seems something like Hierarchical. With the current set of data supplied, if data is converted to hierarchical Data the order by can be done using something similar to:-
SELECT data FROM sample ORDER BY CAST ( '/' + replace( data, '.',
'/' ) + '/' as hierarchyid )
I am making a simple select query with the following result -
select source_uri from image
--
source_uri
"image/30022/A.jpg"
"image/30022/B.jpg"
"image/30022/C.jpg"
"image/30022/D.jpg"
"image/30023/A.jpg"
"image/30023/B.jpg"
"image/30023/C.jpg"
"image/30023/D.jpg"
"image/30024/A.jpg"
"image/30024/B.jpg"
"image/30024/C.jpg"
"image/30024/D.jpg"
I want result like -
source_uri
"image/30022/A.jpg"
"image/30023/B.jpg"
"image/30024/C.jpg"
I tried having a group by clause... but it doesnot group by since the values are different.
--
I am able to use the SUBSTRING_INDEX function in mySQL.
Looking for the exact alternative in Sybase.
If there is always the last 6 i.e D.jpg" characters you need to apply group by then you can use the RIGHT(col,len),Right returns the rightmost len characters from the string str, or NULL if any argument is NULL
select source_uri
from image
GROUP BY RIGHT(source_uri ,6)
RIGHT(str,len)
Other way you need get the part after last / so use SUBSTRING_INDEX
select source_uri
from image
GROUP BY SUBSTRING_INDEX(source_uri ,'/',-1)
SUBSTRING_INDEX(str,delim,count)
EDIT after reading comments
SUBSTRING_INDEX(source_uri ,'/',-1) /* will give you A.jpg*/
Now to get remaining part you can do so
SUBSTRING_INDEX(source_uri ,SUBSTRING_INDEX(source_uri ,'/',-1) ,1) /* will give you image/30022/ */
FOR MySQL -
M Khalid Junaid's answer works great.
FOR Sybase -
I did a lean trick.. not sure how efficient this is -
select source_uri from image
Problem: To extract "image/30022" from "image/30022/a.jpg"
Solution:
1) Got the file length - file_length = CHARINDEX('/', reverse(source_uri))
2) Substring - SUBSTRING(source_uri, 1, LEN(source_uri) - file_length)
select SUBSTRING(source_uri, 1, LEN(source_uri) - file_length) from image
group by SUBSTRING(source_uri, 1, LEN(source_uri) - file_length)