3

How to convert string '۱۳۹۴' to '1394'?

I try change collation but does not work.

Please note that I read data from external device in C# .

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Hamid
  • 1,099
  • 3
  • 22
  • 37
  • `۱۳۹۴` is it really 1394 – wiretext Jul 26 '15 at 08:01
  • @tinka yes. see http://unicode-table.com/en/#arabic – Hamid Jul 26 '15 at 08:12
  • Why don't you convert when you read it? – paparazzo Jul 26 '15 at 09:02
  • They might be the same word in terms of your language, but actually they are two different entities in terms of unicode characters. It's like asking the system to convert "one" to "1" automatically. Unless you use a mapping table it cannot be done. – Alan Jul 26 '15 at 09:40
  • Hi, i don't know but have you tried this? SELECT ASCII('۱۳۹۴') however it returned 63 instead of 1394. – ken lacoste Jul 26 '15 at 11:03
  • @kenlacoste ACSII 63 is just ?. Every single one of the those chars would return 63 – paparazzo Jul 26 '15 at 11:46
  • yeah, I noticed that too, well I think you have no other choice but to either use a UDF just like the script of tinka, or modify your C# with number interchange just like the answer of amer in this forum http://stackoverflow.com/questions/8926792/converting-numbers-from-western-arabic-digits-1-2-3-to-eastern-arabic-digit – ken lacoste Jul 26 '15 at 12:44

2 Answers2

3

i have tried to solve problem after search on internet i came to the conclusion the best way to solve this problem is function

ALTER FUNCTION [dbo].[udf_ReplaceArabicNumbers]
    (@str NVARCHAR(1000))
    RETURNS NVARCHAR(2000)
AS
BEGIN

    DECLARE @i INT = 1
    WHILE @i<=LEN(@str)

    BEGIN
        DECLARE @val NVARCHAR(1)
        SET @val = SUBSTRING(@str, @i, 1)
            DECLARE @newchar NVARCHAR(1)
            SET @newchar = CASE(@val)
                    WHEN N'۱' THEN 1
                    WHEN N'۲' THEN 2
                    WHEN N'۳' THEN 3
                    WHEN N'۴' THEN 4
                    WHEN N'۵' THEN 5
                    WHEN N'۶' THEN 6
                    WHEN N'۷' THEN 7
                    WHEN N'۸' THEN 8
                    WHEN N'۹' THEN 9
                    WHEN N'۰' THEN 0
                END
        SET @str = REPLACE(@str, @val, @newchar)
        SET @i+=1;
    END

RETURN @str
END

and call to this function

select [dbo].[udf_ReplaceArabicNumbers] (N'۱۳۹۴')

i refer this site http://unicode-table.com/en/ with the help of UNICODE we can get HTML-Code and use in our Program

select  '&#' + cast (UNICODE(N'۱')as nvarchar(10)) + ';',
        '&#' + cast (UNICODE(N'۳')as nvarchar(10)) + ';',
        '&#' + cast (UNICODE(N'۹')as nvarchar(10)) + ';',
        '&#' + cast (UNICODE(N'۴')as nvarchar(10)) + ';'

and result would be

enter image description here

wiretext
  • 3,302
  • 14
  • 19
0

Based on the properties of the unicode code points numbers, you could use something like this:

DECLARE @ArabicNumber NVARCHAR(4)
SET @ArabicNumber=N'۱۳۹۴'
SELECT 
    LEFT(CONVERT(NVARCHAR(4),CONVERT(VARBINARY(8), 
        CONVERT(BIGINT,CONVERT(VARBINARY(8),CONVERT(NCHAR(4),@ArabicNumber))) 
        & CONVERT(VARBINARY(8),REPLICATE(0x0F00,4))
        ^ CONVERT(VARBINARY(8),REPLICATE(0x3000,4))
    )),LEN(@ArabicNumber))

This works if the input string contains only numbers and it is limited to 4 characters, to fit in a bigint, for the bitwise operations. For longer strings, you should use a WHILE loop to process each character.

Razvan Socol
  • 5,426
  • 2
  • 20
  • 32