Returning first line from a string
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).
精彩评论