IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fn_ParayiYaziyaCevir]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN 
DECLARE @SQL VARCHAR(7000) = '
 CREATE FUNCTION [dbo].[fn_ParayiYaziyaCevir]  
(@num AS numeric(18,2), @isMoney as bit) RETURNS varchar(255)  BEGIN 

 DECLARE @Split tinyint 

 DECLARE @iSplit tinyint 

 DECLARE @sNum varchar(20) 

 DECLARE @NumSet varchar(3) 

 DECLARE @Char char(1) 

 DECLARE @NumText varchar(255) 

 DECLARE @SetText varchar(100) 

 DECLARE @HunText varchar(100) 

 DECLARE @CharText varchar(50) 

 DECLARE @Match bit 

 DECLARE @Point tinyint 

 DECLARE @Cents varchar(2) 

 DECLARE @CentText varchar(100) 

 DECLARE @NumStrings TABLE (Num int, NumStr varchar(20)) 

 INSERT INTO @NumStrings 

 SELECT 1, ''Bir'' 

 UNION SELECT 2, ''İki'' 

 UNION SELECT 3, ''Üç'' 

 UNION SELECT 4, ''Dört'' 

 UNION SELECT 5, ''Beş'' 

 UNION SELECT 6, ''Altı'' 

 UNION SELECT 7, ''Yedi'' 

 UNION SELECT 8, ''Sekiz'' 

 UNION SELECT 9, ''Dokuz'' 

 UNION SELECT 10, ''On'' 

 UNION SELECT 11, ''Onbir'' 

 UNION SELECT 12, ''Oniki'' 

 UNION SELECT 13, ''Onüç'' 

 UNION SELECT 14, ''Ondört'' 

 UNION SELECT 15, ''Onbeş'' 

 UNION SELECT 16, ''Onaltı'' 

 UNION SELECT 17, ''Onyedi'' 

 UNION SELECT 18, ''Onsekiz'' 

 UNION SELECT 19, ''Ondokuz'' 

 UNION SELECT 20, ''Yirmi'' 

 UNION SELECT 30, ''Otuz'' 

 UNION SELECT 40, ''Kırk'' 

 UNION SELECT 50, ''Elli'' 

 UNION SELECT 60, ''Altmış'' 

 UNION SELECT 70, ''Yetmiş'' 

 UNION SELECT 80, ''Seksen'' 

 UNION SELECT 90, ''Doksan'' 

 SET @sNum = cast(@num as varchar(20)) 

 SET @Point = charindex(''.'', @sNum) 

 IF @Point > 0 

 BEGIN 

 SET @Cents = substring(@sNum, @Point + 1, 2) 

 SET @sNum = left(@sNum, @Point-1) 

 IF @isMoney = 1 

 BEGIN

 SET @Match = (select count(*) FROM @NumStrings WHERE Num = @Cents) 

 IF @Match <> 0 

 BEGIN 

 SET @CentText = (select NumStr FROM @NumStrings WHERE Num = @Cents) 

 END 

 ELSE 

 BEGIN 

 SET @CentText = isnull((select NumStr FROM @NumStrings WHERE Num = left(@Cents, 1) + ''0''),'''') + (select NumStr from @NumStrings where Num = right(@Cents, 1)) 

 END 

 END 

 ELSE

 BEGIN 

 SET @CentText = isnull((select NumStr FROM @NumStrings WHERE Num = left(@Cents, 1)),'''') + isnull((select NumStr from @NumStrings where Num = right(@Cents, 1)),'''') 

 END 

 END 

 IF @CentText IS NULL 

 SET @CentText = '''' 

 SET @Split = ((len(@sNum)-1) / 3) + 1 

 SET @iSplit = 0 

 SET @NumText = '''' 

 WHILE @iSplit < @Split 

 BEGIN 

 SET @CharText = '''' 

 SET @HunText = '''' 

 SET @SetText = '''' 

 SET @NumSet = right(left(@sNum, len(@sNum) - @iSplit * 3), 3) 

 IF len(@Numset) = 3

 BEGIN 

 SET @Char = left(@NumSet, 1) 

 if @Char=''1''  

 SET @HunText = ''Yüz'' 

 ELSE 

 SET @HunText = isnull((select NumStr FROM @NumStrings WHERE Num = @Char) + ''Yüz'', '''') 

 SET @SetText = @HunText 

 END 

 SET @Match = (select count(*) FROM @NumStrings WHERE Num = right(@NumSet, 2)) 

 IF @Match <> 0 

 BEGIN 

 SET @CharText = (select NumStr FROM @NumStrings WHERE Num = right(@NumSet, 2)) 

 END 

 ELSE 

 BEGIN 

 SET @CharText = isnull((select NumStr FROM @NumStrings WHERE Num = left(right(@NumSet, 2), 1) + ''0''),'''') + (select NumStr from @NumStrings where Num = right(@NumSet, 1)) 

 END 

 IF @CharText IS NULL SET @CharText = '''' 

 IF @SetText IS NULL set @SetText = '''' 

 IF @HunText <> '''' AND @CharText <> '''' 

 SET @SetText = @SetText + '''' 

 IF @HunText = '''' AND @CharText <> '''' and @iSplit = 0 and @Split > 1 and @CentText = '''' 

 SET @SetText = '''' + @SetText 

 SET @SetText = @SetText + @CharText 

 if @iSplit = 1 AND @SetText=''Bir'' BEGIN  

 SET @SetText='''' 

 SET @SetText = @SetText + CASE @iSplit 

 WHEN 0 THEN '''' 

 WHEN 1 THEN ''Bin'' 

 WHEN 2 THEN ''Milyon'' 

 WHEN 3 THEN ''Milyar'' 

 WHEN 4 THEN ''Trilyon'' 

 WHEN 5 THEN ''Katrilyon'' 

 END + CASE WHEN len(@NumText) > 0 THEN '''' ELSE '''' end 

 END 

 ELSE 

 BEGIN 

 IF @SetText <> '''' 

 SET @SetText = @SetText + CASE @iSplit 

 WHEN 0 THEN '''' 

 WHEN 1 THEN ''Bin'' 

 WHEN 2 THEN ''Milyon'' 

 WHEN 3 THEN ''Milyar'' 

 WHEN 4 THEN ''Trilyon'' 

 WHEN 5 THEN ''Katrilyon'' 

 END + CASE WHEN len(@NumText) > 0 THEN '''' ELSE '''' end 

 END 

 SET @NumText = @SetText + @NumText 

 SET @iSplit = @iSplit + 1 

 END 

 IF @isMoney = 1 AND @NumText <> '''' and rtrim(ltrim(@NumText)) <> ''Bir'' 

 SET @NumText = @NumText + '' TL'' 

 IF @isMoney = 1 AND rtrim(ltrim(@NumText)) = ''Bir'' 

 SET @NumText = @NumText + '' TL'' 

 IF @isMoney = 1 AND @CentText <> '''' and rtrim(ltrim(@CentText)) <> ''Bir'' 

 SET @CentText = @CentText + '' kuruş'' 

 IF @isMoney = 1 AND rtrim(ltrim(@CentText)) = ''Bir'' 

 SET @CentText = @CentText + '' kuruş'' 

 IF @isMoney = 0 AND @CentText <> '''' 

 SET @CentText = '' Point'' + @CentText 

 IF @isMoney = 1 AND @NumText <> '''' and @CentText <> '''' 

 SET @CentText = '' '' + @CentText 

 SET @NumText = @NumText + @CentText 

 IF left(@sNum, 1) = ''-'' 

 SET @NumText = ''virgül'' + @NumText 

 RETURN @NumText 

 END 

'

EXEC(@SQL)
END