SQL 如何在SQL中获取字符串的第一个字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/792294/
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 01:50:08  来源:igfitidea点击:

How to get first character of a string in SQL?

sqlsql-serversql-server-2005

提问by Vinod

I have a SQL column with a length of 6. Now want to take only the first char of that column. Is there any string function in SQL to do this?

我有一个长度为 6 的 SQL 列。现在只想获取该列的第一个字符。SQL中是否有任何字符串函数可以做到这一点?

回答by Eric

LEFT(colName, 1)will also do this, also. It's equivalent to SUBSTRING(colName, 1, 1).

LEFT(colName, 1)也会这样做,也。它相当于SUBSTRING(colName, 1, 1).

I like LEFT, since I find it a bit cleaner, but really, there's no difference either way.

我喜欢LEFT,因为我觉得它更简洁一些,但实际上,两种方式都没有区别。

回答by onedaywhen

I prefer:

我更喜欢:

SUBSTRING (my_column, 1, 1)

because it is Standard SQL-92 syntax and therefore more portable.

因为它是标准 SQL-92 语法,因此更具可移植性。



Strictly speaking, the standard version would be

严格来说,标准版本是

SUBSTRING (my_column FROM 1 FOR 1)

The point is, transforming from one to the other, hence to any similar vendor variation, is trivial.

关键是,从一个转换到另一个,因此转换到任何类似的供应商变体,都是微不足道的。

p.s. It was only recently pointed out to me that functions in standard SQL are deliberately contrary, by having parameters lists that are not the conventional commalists, in order to make them easily identifiable as being from the standard!

ps 最近才向我指出标准 SQL 中的函数是故意相反的,通过使用不是传统逗号列表的参数列表,以便使它们易于识别为来自标准!

回答by Damovisa

SUBSTRING ( MyColumn, 1 , 1 )for the first character and SUBSTRING ( MyColumn, 1 , 2 )for the first two.

SUBSTRING ( MyColumn, 1 , 1 )对于第一个字符和SUBSTRING ( MyColumn, 1 , 2 )前两个字符。

回答by Devendra Verma

SELECT SUBSTR(thatColumn, 1, 1) As NewColumn from student

回答by jet_choong

It is simple to achieve by the following

通过以下方式很容易实现

DECLARE @SomeString NVARCHAR(20) = 'This is some string'
DECLARE @Result NVARCHAR(20)

Either

任何一个

SET @Result = SUBSTRING(@SomeString, 2, 3)
SELECT @Result

@Result = his

@Result = his

or

或者

SET @Result = LEFT(@SomeString, 6)
SELECT @Result

@Result = This i

@Result = This i

回答by Shiv

INPUT

输入

STRMIDDLENAME
--------------
Aravind Chaterjee
Shivakumar
Robin Van Parsee

SELECT STRMIDDLENAME, 
CASE WHEN INSTR(STRMIDDLENAME,' ',1,2) != 0 THEN SUBSTR(STRMIDDLENAME,1,1) || SUBSTR(STRMIDDLENAME,INSTR(STRMIDDLENAME,' ',1,1)+1,1)||
SUBSTR(STRMIDDLENAME,INSTR(STRMIDDLENAME,' ',1,2)+1,1)
WHEN INSTR(STRMIDDLENAME,' ',1,1) != 0 THEN SUBSTR(STRMIDDLENAME,1,1) || SUBSTR(STRMIDDLENAME,INSTR(STRMIDDLENAME,' ',1,1)+1,1)
ELSE SUBSTR(STRMIDDLENAME,1,1)
END AS FIRSTLETTERS
FROM Dual;

OUTPUT
STRMIDDLENAME                    FIRSTLETTERS
---------                        -----------------
Aravind Chaterjee                AC           
Shivakumar                       S
Robin Van Parsee                 RVP

回答by Darshan

Select First two Character in selected Field with Left(string,Number of Char in int)

选择所选字段中的前两个字符 Left(string,Number of Char in int)

SELECT LEFT(FName, 2) AS FirstName FROM dbo.NameMaster

回答by LittleJC

If you search the first char of string in Sql string

如果在Sql字符串中搜索字符串的第一个字符

SELECT CHARINDEX('char', 'my char')

=> return 4