【问题标题】:Alphanumeric SQL Sorting to select Min and Max Values字母数字 SQL 排序以选择最小值和最大值
【发布时间】:2019-11-14 19:08:53
【问题描述】:

有人可以帮我得到这个结果吗?

所以我的表值对于一个表 dbo.size 如下:

Sc_Size
1
2
3
4
5
6
7
8
9
10

我尝试了以下查询:

select min(sc_size) as minsize,max(sc_size) as maxsize from dbo.Size

这是我得到的结果:

minsize     maxsize
  1            9

但我想得到这个作为我的结果

minsize     maxsize
  1            10

那么我如何得到这个结果

【问题讨论】:

  • 我怀疑 sc_size 是一个字符串?
  • 最简单也可能是最好的解决方案是停止将数字存储为字符串。
  • 问题是我也有 S、M、L 和 XL 等尺码

标签: sql-server sql-order-by


【解决方案1】:

下面将说明字符串的最大值与整数的最大值

Declare @YourTable Table ([Sc_Size] varchar(50))  Insert Into @YourTable Values 
 (1)
,(2)
,(3)
,(4)
,(5)
,(6)
,(7)
,(8)
,(9)
,(10)

Select MinString = min(sc_size)
      ,MaxString = max(sc_size)
      ,MinInt    = min(try_convert(int,sc_size))
      ,MaxInt    = max(try_convert(int,sc_size))
 from @YourTable

退货

MinString   MaxString   MinInt  MaxInt
1           9           1       10

【讨论】:

    猜你喜欢
    • 2020-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-09
    • 1970-01-01
    • 2012-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多