简体   繁体   中英

Truncate Datacolumn C#

I am using C#.

I have ODBC DSN connection that I bring into DataTable. Column One is "BRANCH".

Example:

BRANCH-----| TYPE-----| ID-----|

1V--------------|   R-----------|   ZZ

1A--------------|   R-----------|   ZZ

3---------------|   W-----------|   VW

2V--------------|   R-----------|   VW

1---------------|   R-----------|   NI

4---------------|   I-----------|   MA

3B--------------|   R-----------|   SZ

I want to change column one so it only contains the first character.

It would look like this.

BRANCH-----| TYPE-----| ID-----|

1---------------|   R-----------|   ZZ

1---------------|   R-----------|   ZZ

3---------------|   W-----------|   VW

2---------------|   R-----------|   VW

1---------------|   R-----------|   NI

4---------------|   I-----------|   MA

3---------------|   R-----------|   SZ

Please help I have tried many examples, but I can't make it work.

Based on your need to create a column using an Expression, you can just use substring in your expression. See MSDN info on DataColumn.Expression

I believe that the first character is at Index 1

dcUnits.Expression = string.Format("SUBSTRING({0}, 1, 1)+''+{1}+''+{2}", "BRANCH", "TYPE", "ID");

I used RegEx to get all the digits so if you have a two digit number it will work. If it doesn't work try the index number From : row["BRANCH"] To : row[0]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("BRANCH", typeof(string));
            dt.Columns.Add("TYPE", typeof(string));
            dt.Columns.Add("ID", typeof(string));

            dt.Rows.Add(new object[] { "1V", "R", "ZZ"});
            dt.Rows.Add(new object[] { "1A", "R", "ZZ"});
            dt.Rows.Add(new object[] { "3", "W", "V"});
            dt.Rows.Add(new object[] { "2V", "R", "VW"});
            dt.Rows.Add(new object[] { "1", "R", "NI"});
            dt.Rows.Add(new object[] { "4", "I", "MA"});
            dt.Rows.Add(new object[] { "1B", "R", "SZ"});

            foreach (DataRow row in dt.AsEnumerable())
            {
                row["BRANCH"] = Regex.Match(row["BRANCH"].ToString(), "\\d+").Value.ToString();
            }

        }
    }
}
​

Loop through your DataTable and reset the BRANCH value in each row.

        foreach (DataRow row in dt.Rows)
        {
            row["BRANCH"] = row["BRANCH"].ToString().Substring(0,1);
            Console.WriteLine(row["BRANCH"].ToString());
        }

Here is a full working version with the data you provide: https://dotnetfiddle.net/pJzeAO

Note: my reset code is different from previous answer that it resets it to the "first character" not numeric value. I believe "first character" is what you asked for in your question.

Both options are there. Use what fits your situation. Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM