如何在c#中使用字符串获取最大ID [英] How to get max id with string in c#

查看:225
本文介绍了如何在c#中使用字符串获取最大ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是:



S001

S1102

S546

S999

S745


$ b $我使用此查询



选择最大值(receipt_no )来自tb_receipt的rcptno



每次返回

>>> S999



但是正确的ID是S1102





如何正确id ???

解决方案

SQL完全符合人们的期望。它返回该类型的高值。你想到了数字排序,但是在使用字符串时你得到了字符串排序。

你可以删除前导'S'并让SQL进行数字排序......

< pre lang =SQL> SELECT MAX(CAST(REPEACE(RECEIPT_NO,' S'' ' AS INT ))...


你必须保持排序中的字符串和数字元素分开。

排序数字,因为你想要更大的编号项目。


尝试下面

 选择  top   1  RECEIPT_NO 来自 
tb_receipt 订单 by
CInt(Mid。(RE CEIPT_NO, 2 ,len(RECEIPT_NO))) desc





查看以下答案

http://stackoverflow.com/questions/15969465/picking-the-maximum-value-from-an-alpha-numeric-column-in-ms-access [ ^ ]


Id is:

S001
S1102
S546
S999
S745

i am use this query

"Select max(receipt_no) as rcptno from tb_receipt"

every time return
>>> S999

but correct id is S1102


How to get correct id???

解决方案

SQL does exactly what one should expect. It returns the high value for that type. You think of numeric sorting, but while using string you got string sorting.
You may remove the leading 'S' and let SQL do a numeric sorting...

SELECT MAX(CAST(REPLACE(RECEIPT_NO, 'S', '') AS INT)) ...


You have to keep the string and the number element in the sorting separate.
Sort on the number since you want the larger numbered item.


try below

select top 1 RECEIPT_NO from
tb_receipt order by
CInt(Mid(RECEIPT_NO, 2, len(RECEIPT_NO))) desc



check below answers
http://stackoverflow.com/questions/15969465/picking-the-maximum-value-from-an-alpha-numeric-column-in-ms-access[^]


这篇关于如何在c#中使用字符串获取最大ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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