具有Character数据类型的SQL Max函数 [英] Sql Max function with Character data type

查看:130
本文介绍了具有Character数据类型的SQL Max函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我在一个字段中存储数字值,例如1,23 ... 99,并且该字段具有字符数据类型.
我需要此字段的最大值,但由于字符数据类型,它没有返回正确的值.
如果您有任何建议,请告诉我.
预先感谢

Hello everybody
I am storing number values like 1,23...99 in a field, and this field have character data type.
I need max value from this field but due to character data type it is not returning correct value.
If u have any suggestion then please tell me..
Thanks in advance

推荐答案

切勿在基于字符的字段中存储数字,否则任何形式的比较均无法正常进行.例如,基于字符的值"10"在基于字符的值"9"之前.

根据偏好,将数值存储在数字字段中.
如果您不能更改表定义(我会这样做,效率会更高,并且容易出错),则可以CAST值:
Never store numbers in a character based field, or any form of comparison cannot work correctly. For example, the character based value "10" is before the character based value "9".

By preference, store numeric values in numeric fields.
If you can''t change the table definition (and I would, it is a lot more efficient and prone to errors) you can CAST the values:
SELECT MAX(CAST(dat as int)) FROM myTable


CAST(或CONVERT)您的文本结果为数字,然后使用MAX.
CAST (or CONVERT) your text result to a numeric one and then use MAX.


您的问题类似于此人:

从任何表中查找最大值 [ ^ ]
Your problem is similar to this person :

Find maximum value from any table[^]


这篇关于具有Character数据类型的SQL Max函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆