﻿
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[ott_b10]
(
	@string varchar(10)
)
RETURNS int
AS
BEGIN
	DECLARE @RET int;


	DECLARE @str VARCHAR(10) = REVERSE(LOWER(@string));
	WITH Split(stpos,endpos) AS(
			SELECT 1 AS stpos, 2 AS endpos
			UNION ALL
			SELECT endpos, endpos+1
			FROM Split
			WHERE endpos <= LEN(@str)
	)
	, c as (
		SELECT 
			'character' = SUBSTRING(@str,stpos,COALESCE(NULLIF(endpos,0),LEN(@str)+1)-stpos)
		   ,'charindex' = stpos
		FROM Split
	)
	, s as (
		SELECT 
			*
			,'num' = ASCII(character) - ASCII('a') + 1
			,'fac' = POWER(26,([charindex] -1))
			,'val' = POWER(26,([charindex] -1)) *  (ASCII(character) - ASCII('a') + 1)
		FROM c
	)
	select @RET = SUM([val]) FROM s;
	
	RETURN @RET;

END