我有表 1 資料,我需要根據標準創建另一列
如果 id 有“-”或“任何字母”,則從發票中獲取價值
表格1
--------- ---------
| id | invoice |
--------- ---------
| 1234 | 2534 |
| 9870 | 6542 |
| ABC234 | 9874 |
| 34-5469 | 325416 |
--------- ---------
預期結果為 id2
--------- --------- --------
| id | invoice | id2 |
--------- --------- --------
| 1234 | 2534 | 1234 |
| 9870 | 6542 | 9870 |
| ABC234 | 9874 | 9874 |
| 34-5469 | 325416 | 325416 |
--------- --------- --------
uj5u.com熱心網友回復:
您可以使用 isumeric 函式來確定 id 是否為 int。
select *,
case when isnumeric(id) = 1 then id else invoice end as id2
from [yourtable]
編輯:isnumeric 并不總是提供可靠的結果,因此如果您使用 SQL Server 2012 或 2014 及更高版本,您可能會選擇 try_cast
select *
,case when try_cast(id as int) is not null then id else invoice end as id2
from [yourtable]
uj5u.com熱心網友回復:
假設您只是在尋找帶有字母或連字符 ( -) 的值,您可以使用如下CASE運算式和 a LIKE:
SELECT CASE WHEN id LIKE '%[A-z-]%' THEN invoice ELSE id END
FROM dbo.YourTable;
然而,可能會更好的是檢查id除了數字之外不重視任何字符:
SELECT CASE WHEN id LIKE '%[^0-9]%' THEN invoice ELSE id END
FROM dbo.YourTable;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/441786.html
標籤:sql服务器
