SQL 在字段中的位置获取字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1409822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 03:33:33  来源:igfitidea点击:

SQL Get char at position in field

sql

提问by Adriaan Stander

How can I get the character at position 4 in a field?

如何获得字段中位置 4 的字符?

e.g.

例如

field contents = "hello"

字段内容 = "你好"

I want to return the value of position 2 = "l"

我想返回位置 2 = "l" 的值

回答by Adriaan Stander

In SQL Server you can use SUBSTRING

在 SQL Server 中,您可以使用 SUBSTRING

SELECT SUBSTRING('hello', 3, 1)

Take care: index is 1-based.

注意:索引是基于 1 的。

回答by Ed Harper

In Oracle, use SUBSTR

在 Oracle 中,使用 SUBSTR

Syntax is SUBSTR(<string to parse>,<start position>,(<length>))- i.e.

语法是SUBSTR(<string to parse>,<start position>,(<length>))- 即

SELECT SUBSTR('hello',3,1)

Start position and length are one-, not zero-based. Zero is accepted, but will be interpreted as 1.

起始位置和长度从一开始,而不是从零开始。零被接受,但将被解释为 1。

回答by Akruwala

following Query will search from specific index for char and will return result

以下查询将从特定索引中搜索字符并返回结果

select * from tbPatientMaster
where SUBSTRING (fname,CHARINDEX ('.',fname,0)+1,LEN (fname)) like 'a%'