我寫了這是一個功能,因爲我覺得它更容易比內聯代碼爲:
create function dbo.convert_amount_str (@amount_str varchar(50))
returns money
as
begin
declare @char_index int
declare @amount money
declare @char varchar(50)
declare @decimal money
-- Match the first non-numeric character
select @char_index = PATINDEX('%[^0-9]%', @amount_str)
-- Get the numeric characters into a numeric variable
select @amount = convert(money, SUBSTRING(@amount_str, 0, @char_index))
-- Get the non-numeric character (will work for multiple characters)
select @char = SUBSTRING(@amount_str, @char_index, (len(@amount_str) - @char_index) + 1)
-- Convert the non-numeric characters into decimal amounts
select @decimal = case @char
when 'A' then .8 -- whatever this should equate to
when 'H' then .7 -- whatever this should equate to
-- output for remaining characters
end
return @amount + @decimal
end
就用這樣的:
select dbo.convert_amount_str('00484326A')
或者,更可能的是,引用任何包含數字字符串值的列。
現在不能爲你寫,但嘗試SUBSTRING和CASE。 –
我可以做的子字符串,但我必須知道,爲什麼每個值都以特定的charector結束。我想有一個理由要知道。 –
eequalsmcaputo給你的CASE部分。 –