Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
I'm calling the DB on C# Form in a DataTable, it has a column with Text data which more than 3K-4K words but I only need last 500 words from that column and the column index is 6th so how to update only last 500 words in that column?

I tried something like this:
C#
SqlDataAdapter da = new SqlDataAdapter("select * from Table1", sqlCon);
DataTable dtMainSQLData = new DataTable();
                if (dtMainSQLData.Columns.Contains("DA_ArticleDetails"))
                {
                    //not sure what to code here
                    //but the code should select last 500 words from the column
                    //and update them in the same column
               }
//fill the data table with the data from SELECT query
 da.Fill(dtMainSQLData);


Help Please!!
Posted
Updated 23-Jul-14 0:28am
v3
Comments
Varun Sareen 23-Jul-14 6:13am    
what have you tried so far?
GoneBump 23-Jul-14 6:25am    
I updated the question. Could you please look at it.
Maciej Los 23-Jul-14 6:28am    
What you mean by "last 500 words"?
GoneBump 23-Jul-14 6:30am    
This column 'DA_ArticleDetails' contains unwanted text record, mainly more that 3K-4K words and the usefull text is at the end of the every cell in the column.
Maciej Los 23-Jul-14 6:34am    
OK, is there any specific order? How to recognize which is last?

This is something that suits to you:


SQL
Create FUNCTION [dbo].[fnGetWords](@pStr varchar(max), @pWords int)
RETURNS VARCHAR(MAX)
AS
BEGIN
     DECLARE @lPos int, @lCtr int
     SET @lCtr = Len(@pStr)
     SET @lPos = 0
     WHILE @lCtr>=@pWords
     BEGIN
          SET @lPos = CHARINDEX(' ',  @pStr, @lPos + 1)
          SET @lCtr = @lCtr - 1
     END
     RETURN SUBSTRING(@pStr,@lPos, Len(@pStr)-1)
END


select dbo.fnGetWords('This is getting words from right',-2)


gives the last word "right". you can change the logic as per your criteria.
 
Share this answer
 
Piece of cake
select RIGHT(colname, 500)from Table1

You can also use Left ,
Note : Both of them won't throw any error even if there is not enough data available.
 
Share this answer
 
v2
Comments
Maciej Los 23-Jul-14 6:50am    
500 words, not signs in a single row!
GoneBump 23-Jul-14 8:48am    
Thanks for the Solution. It helped in some way but it is giving 500 characters and not words.
GoneBump 23-Jul-14 9:37am    
I tried something like this:

UPDATE table1
SET [DA_ArticleDetails] =
(
select RIGHT([DA_ArticleDetails], 8000)
from table1
)

but it shows a error:

Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.

Could you help?
arry.net 27-May-15 8:06am    
your query return more then one row, try top 1 or add a where clause
select RIGHT([DA_ArticleDetails], 8000)
from table1

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900