Files
Fuchs_Intranet/db/dbo.ott_b10.UserDefinedFunction.sql
T

57 lines
2.6 KiB
Transact-SQL

USE [site_fuchs]
GO
/****** Object: UserDefinedFunction [dbo].[ott_b10] Script Date: 02.12.2020 21:05:05 ******/
DROP FUNCTION [dbo].[ott_b10]
GO
/****** Object: UserDefinedFunction [dbo].[ott_b10] Script Date: 02.12.2020 21:05:06 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- 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
GO
ALTER AUTHORIZATION ON [dbo].[ott_b10] TO SCHEMA OWNER
GO