开发者

Returning first line from a string

开发者 https://www.devze.com 2023-02-17 14:04 出处:网络
I have string which contains three lines how i can retreive first line from the string in sqlserver. forex:

I have string which contains three lines how i can retreive first line from the string in sqlserver.

forex:

declare @Str as nvarchar(200)

Set @Str='This is for test'+char(10)+
         ' second line'+char(10)+
         'Third line'

insert into some test table开发者_JAVA百科.

how i can retrive the first line from this.


SELECT SUBSTRING(@Str,0,CHARINDEX(char(10),@Str))

EDIT: If you have also text with only one line you can use a CASE:

SELECT CASE WHEN CHARINDEX(char(10),@Str)=0 THEN @Str
   ELSE SUBSTRING(@Str,0,CHARINDEX(char(10),@Str)) END


At least since Postgres 9.0:

SELECT split_part(some_column, E'\n', 1) from some_table

note: E is required to escape the literal to a line break


Try this:

select Note_Sets.cNoteCode,
    SUBSTRING(Note_Sets.cScript,1,CHARINDEX(char(10),Note_Sets.cScript)) as cFirstLine
from dbo.Note_Sets

Can either use char(10) or char(13).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号