問題描述
我有一個帶有 nvarchar 列的表.此列的值例如:
I have a table with a nvarchar column. This column has values for example:
- 983
- 294
- a343
- a3546f
等等.
我想取這個值的 MAX,但不是文本,而是數字.所以在這個例子中數字是:
I would like to take MAX of this values, but not as text but like from numerics. So in this example numerics are:
- 983
- 294
- 343
- 3546
MAX 值是最后一個 - 3546.如何在 Microsoft SQL 上的 TSQL 中執行此操作?
And the MAX value is the last one - 3546. How to do this in TSQL on Microsoft SQL?
推薦答案
首先安裝一個正則表達式函數.這篇文章有你的代碼可以剪切/粘貼.
First install a regular expression function. This article has code you can cut/paste.
然后使用 RegexReplace(來自那篇文章),您可以從字符串中提取數字:
Then with RegexReplace (from that article) you can extract digits from a string:
dbo.RegexReplace( '.*?(\d+).*', myField, '$1' )
然后將此字符串轉換為數字:
Then convert this string to a number:
CAST( dbo.RegexReplace( '.*?(\d+).*', myField, '$1' ) AS INT )
然后在 SELECT
的 MAX()
函數中使用這個表達式.
Then use this expression inside a MAX()
function in a SELECT
.
這篇關于如何獲取varchar列中數值的最大值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!