简体   繁体   English

SQL Server解析泰语全名到第一个

[英]SQL Server Parsing Thai Language Full Name to First Last

I have a unique issue. 我有一个独特的问题。 I've worked a long time with SQL Server. 我在SQL Server上工作了很长时间。 We import a file into SQL Server that includes Full Name. 我们将包含全名的文件导入SQL Server。 All I need to do is parse the full name into First and Last. 我需要做的就是将全名解析为名字和姓氏。 If the name is English character set, my parse works fine. 如果名称是英文字符集,我的解析工作正常。 But, we're parsing Thai names which uses a different character set? 但是,我们正在解析使用不同字符集的泰语名称?

This is my code: 这是我的代码:

DECLARE @FullName NVARCHAR(MAX) = N'กล้วยไม้ สวามิวัศดุ์'


SELECT 
    LEN(@FullName) AS StringLength,
    @FullName AS FullName,
    REVERSE(@FullName) AS ReverseName,
    LEFT(@FullName, LEN(@FullName) - CHARINDEX(' ', REVERSE(@FullName))) AS FirstName,
    STUFF(RIGHT(@FullName, CHARINDEX(' ', REVERSE(@FullName))),1,1,'') AS LastName;

Results: 结果:

20  กล้วยไม้ สวามิวัศดุ์    ์ุดศัวิมาวส ้มไยว้ลก    กล้วยไม้ สวามิวัศดุ์     NULL

Like I said, the query works fine when I use english. 就像我说的那样,当我使用英语时查询工作正常。 For example, 'John Smith' returns: 例如,“ John Smith”返回:

10  John Smith  htimS nhoJ  John    Smith

I have searched this and other sites all day! 我整天都在搜索这个网站和其他网站! Thanks in advance. 提前致谢。

I've seen this (IMO) complicated logic involving reverse and stuff previously and do not understand the purpose of using reverse. 我以前已经看过(IMO)涉及反向和东西的复杂逻辑,并且不了解使用反向的目的。 It just seems overly complicated to me. 对我来说,这似乎太复杂了。 Below does what you want without using reverse. 下面做了您想要的而不使用反向的操作。

set nocount on;
declare @names table (FullName nvarchar(40) not null); 
insert @names (FullName) values (N'กล้วยไม้ สวามิวัศดุ์'), (N'John Smith'), (N'Bozo Clown'), (N'Eva Perón'), (N'Stefan Löfven');

select * from @names;


select  
    LEN(FullName) AS StringLength,
    FullName AS FullName,
    LEFT(FullName, CHARINDEX(N' ', FullName)) AS FirstName,
    RIGHT(FullName, len(FullName) - CHARINDEX(N' ', FullName) + 1) as LastName,
    STUFF(FullName, 1, CHARINDEX(N' ', FullName), N'') as LName  
from  @names
;

For consistency (a habit you should develop) I've changed your string literals from ansi strings to unicode strings (prefaced with N'). 为了保持一致性(您应该养成一种习惯),我已将字符串文字从ansi字符串更改为unicode字符串(以N'开头)。 And a word of warning - names are complicated. 还有个警告-名称很复杂。 Verify your assumption about always having 2 part names. 验证关于始终具有2个零件名称的假设。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM