Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

Sort Numeric String in sql query in C#

I have this data in a column in my datatable:

j1
ds2
b15

I need to select and sort this column. Using below code :

var drs = inpDataTable.Select($"myCol<> 'jj'", "myCol Desc");

returns wrong answer!:

j1
b15
ds2

How can I get true answer? Chars aren't important. I'm using SQL Server


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you want to order by just numeric in the mycol then you need PATINDEX as follows:

order by cast(SUBSTRING(mycol, PATINDEX('%[0-9]%', mycol), LEN(mycol)) as INT)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...