如何从DataTable列中仅选择最后500个单词并在C#中的相同DataTable中更新它 [英] How to select only last 500 words from column of DataTable and update it in the same DataTable in C#
本文介绍了如何从DataTable列中仅选择最后500个单词并在C#中的相同DataTable中更新它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在DataTable中调用C#Form上的数据库,它有一个包含超过3K-4K单词的Text数据的列,但我只需要该列的最后500个单词,列索引是第6个,所以如何更新该列中只有500字?
我尝试过这样的事情:
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:
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!!
推荐答案
这是适合的你:
This is something that suits to you:
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)
< br / >
给出最后一个单词right。您可以根据自己的标准更改逻辑。
gives the last word "right". you can change the logic as per your criteria.
一块蛋糕
从表1中选择RIGHT(colname,500)
您也可以使用Left,
注意:即使没有足够的数据,它们也不会抛出任何错误。
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.
这篇关于如何从DataTable列中仅选择最后500个单词并在C#中的相同DataTable中更新它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文