【问题标题】:Reverse characters in string with mixed Left-to-right and Right-to-left languages using SQL 2?使用 SQL 2 使用混合的从左到右和从右到左语言反转字符串中的字符?
【发布时间】:2018-01-08 09:47:10
【问题描述】:

我的表中有字符串值,其中包括希伯来字符(或本例中的任何 R-T-L 语言)和英文字符(或数字)。

问题是英文字符被颠倒了,看起来像:בדיקה 123456 esrever sti fI kcehC。

数字和英文字符颠倒,希伯来文就可以了。

如何使用内置 SQL 函数来识别英文子字符串(和数字)并将其反转,同时保持其他 RTL 字符的顺序?

我在以下链接中阅读了此问题的答案: Reverse characters in string with mixed Left-to-right and Right-to-left languages using SQL?

解决办法:

DECLARE @sourceString NVARCHAR(100) = N'123456 בדיקה esrever sti fI kcehC';
DECLARE @reversedString NVARCHAR(4000)  = nchar(8237) + REVERSE(@sourceString) +  nchar(8236)
SELECT @reversedString;

我的问题: 当我尝试使用此选项构建查询时,它不起作用例如:

select count(*) from table where nchar(8237) + REVERSE(column) +  nchar(8236) = N'‭Check If its reverse הקידב 654321‬'

我该如何解决?

【问题讨论】:

  • 能否提供一些示例行和预期输出
  • SQL2?你的意思是你想要一个符合 ANSI SQL-92 的查询吗?

标签: sql sql-server


【解决方案1】:

SQL-Server 中 RTL 字符串的行为,尤其是混合时,有时真的很奇怪。

我不确切知道您想要实现什么,但这至少会为您提供 unicode 代码点:

DECLARE @source NVARCHAR(100)=N'123456 בדיקה esrever sti fI kcehC';

WITH cte AS
(
    SELECT 1 AS pos
          ,UNICODE(SUBSTRING(@source,1,1)) AS CodeAtPosition
    UNION ALL
    SELECT cte.pos+1
          ,UNICODE(SUBSTRING(@source,cte.pos+1,1))
    FROM cte
    WHERE pos<LEN(@source)
)
SELECT * 
FROM cte

希伯来字符的范围要大得多:

pos CodeAtPosition

1   49
2   50
3   51
4   52
5   53
6   54
7   32
8   1489 <-- starting here
9   1491
10  1497
11  1511
12  1492 <-- ending here
13  32
14  101
...more rows

有帮助的回答:https://stackoverflow.com/a/29821207/5089204
关于双向文本:https://www.w3.org/TR/WCAG20-TECHS/H34.html

【讨论】:

    猜你喜欢
    • 2021-04-20
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 2018-02-03
    • 2014-10-05
    • 1970-01-01
    • 1970-01-01
    • 2020-11-12
    相关资源
    最近更新 更多