Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

String Contains -- Return Left Right

$
0
0

Hello,

I have this sample query that returns a beginning length of text.

Sample Data

CREATE TABLE [dbo].[SampleTable](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[TextColumn] [nvarchar](500) NOT NULL,
 CONSTRAINT [PK_SampleTable] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET IDENTITY_INSERT [dbo].[SampleTable] ON 

INSERT [dbo].[SampleTable] ([Id], [TextColumn]) VALUES (1, N'This is the first Test string')
INSERT [dbo].[SampleTable] ([Id], [TextColumn]) VALUES (2, N'This is the second one')
INSERT [dbo].[SampleTable] ([Id], [TextColumn]) VALUES (3, N'This is the first really long string of text that should be included in the result set.')
INSERT [dbo].[SampleTable] ([Id], [TextColumn]) VALUES (4, N'This is the second long string that will not be returned in the result set')
INSERT [dbo].[SampleTable] ([Id], [TextColumn]) VALUES (5, N'This is a really really long result set that should also be first included in the result set.  It has a seperate sentence in it as well.')
SET IDENTITY_INSERT [dbo].[SampleTable] OFF

The query:

SELECT  
      a.Id  ,
	  CASE When LEN(TextColumn) <= 60
		  Then TextColumn 
		  ELSE LEFT(TextColumn, 60) + '...'
		  END As Target
  FROM
       SampleTable a
	    WHERE CONTAINS((TextColumn) , 'first')

I would like to return the string and 20 on each side instead of the first.

So for example the third item in the returned result would look like this:

...should also be first included in the result...



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>