English 中文(简体)
Select only first four lines, from Sql text field
原标题:

Sql Server 2008 Express >> Visual Web Developer >> C#

I m pulling records out of the table like this:

SELECT Name, Category, Review FROM ReviewTable

This works fine but the Review field Type in SQL Server is text and is very long (think a magazine article).

I only want to pull the first four lines from the Review field for each row, and display them in my repeater control. These lines will be like a teaser of the article.

Is this possible? How can it be accomplished?

最佳回答

This will return this first 1000 characters from the review.

SELECT Name, Category, CAST(Review AS VARCHAR(1000) FROM ReviewTable

If you must have the first 4 lines you need to use some split function. This could work:

CREATE FUNCTION [dbo].[Split]
(
    @SearchString VARCHAR(8000),
    @Separator VARCHAR(5),
    @MaxItems INT
)
RETURNS @strtable TABLE (strval VARCHAR(8000))
AS

BEGIN
DECLARE @tmpStr VARCHAR(8000), @intSeparatorLength INT, @counter int

IF @MaxItems IS NULL
    SET @MaxItems = 2147483647 -- max int

SET @intSeparatorLength = LEN(@Separator)
SET @Counter = 0

SET @tmpStr = @SearchString
    WHILE 1=1 BEGIN
        INSERT INTO @strtable VALUES ( SUBSTRING(@tmpStr, 0 ,CHARINDEX(@Separator, @tmpStr)))
        SET @tmpStr = SUBSTRING(@tmpStr,CHARINDEX(@Separator,@tmpStr)+LEN(@Separator),8000)
        SET @counter = @counter + 1     
        IF (CHARINDEX(@Separator,@tmpStr) < 1 OR @counter >= @MaxItems)
            BREAK
    END

RETURN
END

Usage: select * from dbo.split( aaa**bbbb**CCCC**dddd**eeee**dffff**ggggg , ** , 4)

问题回答

Well ,the first for lines may be a bit more difficult, but why don t you just put out the first 250 characters or so?

SELECT Name, Category, SubString(Review, 1, 250) AS Review FROM ReviewTable

If your database server is in the same local network as your web server, I think I d probably select the entire field, since you re accessing it at all. You ll still have to do a lookup to access any data in that field, so sql performance-wise for finding the data is a non-issue. The only downside of retrieving the entire field would be the amount of data passed between the servers. Thus: if they re in the same network, I d say this would definitely be cheaper than tampering with each record during selection. It also gives you the ability to cache your response, so that you don t have to hit the database again when the user wants to see the full version of the text.

But, to answer your question, the below should probably do it, altho it looks rather tacky

SELECT Name, Category, left(convert(varchar(8000), Review), charindex( 
 , convert(varchar(8000), Review), charindex( 
 , convert(varchar(8000), Review), charindex( 
 , convert(varchar(8000), Review), charindex( 
 , convert(varchar(8000), Review))+1)+1)+1)-1) FROM ReviewTable

...hrrm, yeah, really, i d consider my first paragraph





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签