SQL Server Order mixed string/number column with Entity Framework

beq87vna  于 11个月前  发布在  其他
关注(0)|答案(2)|浏览(84)

In my database, I have a column which can contain either only numbers, or text (which can also contain numbers). For char columns, SQL server sorts value by character code (1, 12, 14, 2, 20, 200, 3, 30, ...).

How would I have to write my .OrderBy statement to sort them like numerics, when they are numeric?

x7yiwoj4

x7yiwoj41#

If you want to sort on DB ( not on application ), then

use patindex() and substring() functions to extract the integer part of your column( considering null cases as zero ), and then cast as integer :

Order By cast( coalesce(
                substring(yourCol, patindex('%[0-9]%', yourCol), 
                   patindex('%[0-9][^0-9]%', yourCol + 't') 
                   - patindex('%[0-9]%', yourCol) + 1)

               ,0)  as int )

within your select statement.

Demo

v8wbuo2f

v8wbuo2f2#

How about this SQL:

SELECT MyStringField,
       (CASE WHEN ISNUMERIC(MyStringField) = 1 THEN Cast(MyStringColumn as int) ELSE 999999999 END) MyNumericField
FROM MyEntity
ORDER BY MyNumericField, MyStringField

See also: How do I sort a VARCHAR column in SQL server that contains numbers?

I used the following C# snippets in order to get this behavior:

DataContext:

modelBuilder.Entity<MyEntity>().Property(s => s.MyNumericField).HasComputedColumnSql("CASE WHEN ISNUMERIC([MyStringField]) = 1 THEN Cast([MyStringField] as int) ELSE 9999999999999 END");

MyEntity:

public class MyEntity
{
  //...
    [Required]
    public string MyStringField
    {
        get;
        set;
    }

    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public decimal MyNumericField
    {
        get;
        set;
    }
  //...
}

Linq query:

var result = await query.OrderBy(x => x.MyNumericField).ThenBy(x => x.MyStringField).ToListAsync();

相关问题