我正在使用 SQL Server 2014,并且我的資料庫中有一個表 (t1),其中包含一個名為“MealPlan”的列。
此列包含字串串列(摘錄如下):
MealPlan
Sansrepas315€/pers.=630€pour2pers.Devis/RésaSelectionner
Sansrepas394€/pers.=787€pour2pers.Devis/RésaSelectionner
Sansrepas547€/pers.=1 093€pour2pers.Devis/RésaSelectionner
Sansrepas547€/pers.=1 093€pour2pers.Devis/RésaSelectionner
Sansrepas700€/pers.=1 400€pour2pers.Devis/RésaSelectionner
Sansrepas328€/pers.=656€pour2pers.Devis/RésaSelectionner
我需要提取字符=和€之間的數字
我有以下代碼,它們完全符合我的需要:
SUBSTRING(MealPlan,LEN(LEFT(MealPlan,CHARINDEX('=', MealPlan) 1)),LEN(MealPlan) - LEN(LEFT(MealPlan,CHARINDEX('=', MealPlan))) - LEN(RIGHT(MealPlan,CHARINDEX('€', (REVERSE(MealPlan)))))) AS [Price]
運行上面我的列“價格”后顯示如下:
Price
630
787
1 093
1 093
1 400
656
但是,我想去掉一千位數字中的那個空格。
我的預期輸出:
Price
630
787
1093
1093
1400
656
我已經嘗試了以下但它不作業:
REPLACE(SUBSTRING(MealPlan,LEN(LEFT(MealPlan,CHARINDEX('=', MealPlan) 1)),LEN(MealPlan) - LEN(LEFT(MealPlan,CHARINDEX('=', MealPlan))) - LEN(RIGHT(MealPlan,CHARINDEX('€', (REVERSE(MealPlan)))))), ' ','') AS [Price2]
任何幫助將非常感激。
uj5u.com熱心網友回復:
我剛剛在我的示例資料庫中運行了您的查詢,它運行良好..
select
REPLACE(SUBSTRING(Description,
LEN(LEFT(Description,CHARINDEX('=', Description) 1)),
LEN(Description) - LEN(LEFT(Description,CHARINDEX('=', Description))) - LEN(RIGHT(Description,CHARINDEX('€', (REVERSE(Description)))))
), ' ','') AS [Description]
from Worker
uj5u.com熱心網友回復:
表#a1
| MealPlan |
| -------- |
| Sansrepas315€/pers.=630€pour2pers.Devis/RésaSelectionner |
| Sansrepas394€/pers.=787€pour2pers.Devis/RésaSelectionner |
| Sansrepas547€/pers.=1 093€pour2pers.Devis/RésaSelectionner |
詢問
SELECT
REPLACE(
SUBSTRING(MealPlan,CHARINDEX('=', MealPlan) 1, CHARINDEX('=',REVERSE(MealPlan)) - CHARINDEX('€',REVERSE(MealPlan)) -1 )
,' ', ''
)
as value
FROM #a1
結果
| 價值 |
|---|
| 630 |
| 787 |
| 1093 |
uj5u.com熱心網友回復:
上面由 Ji?í Baum 提供的關于“剪切和粘貼”的解決方案對我有用。
uj5u.com熱心網友回復:
使用 XML 和 XQuery 標記字串非常容易。
無需決議字串并呼叫多個函式:SUBSTRING(), CHARINDEX(), PATINDEX(), LEN(), REVERSE()等。
查詢陳述句
-- DDL and sample data population, start
DECLARE @tbl TABLE (ID INT IDENTITY(1,1) PRIMARY KEY, MealPlan NVARCHAR(1000));
INSERT INTO @tbl (MealPlan) VALUES
(N'Sansrepas315€/pers.=630€pour2pers.Devis/RésaSelectionner'),
(N'Sansrepas394€/pers.=787€pour2pers.Devis/RésaSelectionner'),
(N'Sansrepas547€/pers.=1 093€pour2pers.Devis/RésaSelectionner'),
(N'Sansrepas547€/pers.=1 093€pour2pers.Devis/RésaSelectionner'),
(N'Sansrepas700€/pers.=1 400€pour2pers.Devis/RésaSelectionner'),
(N'Sansrepas328€/pers.=656€pour2pers.Devis/RésaSelectionner');
-- DDL and sample data population, end
DECLARE @separator CHAR(1) = '='
, @euro CHAR(1) = '€';
SELECT t.*
, REPLACE(c.value('(/root/r[3]/text())[1]', 'VARCHAR(20)'),SPACE(1),'') AS Price
FROM @tbl AS t
CROSS APPLY (SELECT TRY_CAST('<root><r><![CDATA['
REPLACE(REPLACE(MealPlan,@euro,@separator), @separator, ']]></r><r><![CDATA[')
']]></r></root>' AS XML)) AS t1(c);
輸出
---- ------------------------------------------------------------ --------
| ID | MealPlan | Result |
---- ------------------------------------------------------------ --------
| 1 | Sansrepas315€/pers.=630€pour2pers.Devis/RésaSelectionner | 630 |
| 2 | Sansrepas394€/pers.=787€pour2pers.Devis/RésaSelectionner | 787 |
| 3 | Sansrepas547€/pers.=1 093€pour2pers.Devis/RésaSelectionner | 1093 |
| 4 | Sansrepas547€/pers.=1 093€pour2pers.Devis/RésaSelectionner | 1093 |
| 5 | Sansrepas700€/pers.=1 400€pour2pers.Devis/RésaSelectionner | 1400 |
| 6 | Sansrepas328€/pers.=656€pour2pers.Devis/RésaSelectionner | 656 |
---- ------------------------------------------------------------ --------
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/404666.html
標籤:
