如何通过分隔符拆分值?

时间:2017-06-12 20:07:58

标签: sql advantage-database-server

我正在运行数据库10。

我的一个现场数据库有"客户名称"它存储为"姓氏,名字和#34;。我希望能够从该字段中提取名字。我认为这样可行,但没有骰子:

SELECT   [Customer Name]
        ,LEFT([Customer Name], CHARINDEX(',', [Customer Name]) - 1) AS [Surname]
        ,REPLACE(SUBSTRING([Customer Name], CHARINDEX(',', [Customer Name]), LEN([FullName])), ',', '') AS [FirstName]
FROM    orders

我没有收到任何结果。以下是错误消息:

poQuery: Error 7200: AQE Error: State = S0000; NativeError = 2158; [iAnywhere Solutions][Advantage SQL Engine]Scalar function not found: charindex -- Location of error in the SQL statement is: 57 (line: 2 column: 32)

基本上我需要 |客户姓名|      Doe,John

成为: |第一个名字|最后一个名字|  约翰| DOE

1 个答案:

答案 0 :(得分:0)

使用POSITION(',' IN field)语法。

请参阅此处的文档:

http://devzone.advantagedatabase.com/dz/webhelp/Advantage12/master_string_functions.htm

您的查询看起来像这样(未经测试):

SELECT   [Customer Name]
        ,LEFT([Customer Name], POSITION(',' IN [Customer Name]) - 1) AS [Surname]
        ,REPLACE(SUBSTRING([Customer Name], POSITION(',' IN [Customer Name]), LENGTH([FullName])), ',', '') AS [FirstName]
FROM    orders