+ Reply to Thread
Results 1 to 7 of 7

Thread: How to insert "-" in existing records

  1. #1
    victor64 is offline VB.NET Forum Enthusiast victor64 is on a distinguished programming path ahead
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Nov 2008
    Age
    46
    Posts
    56
    Reputation
    24

    Default How to insert "-" in existing records

    Hello, I am using VB.NET 2005 and MS ACCESS 2003

    My database contains data in the following format:

    1305000248767

    I need to insert "-" in several location to obtain the following format

    1305-00-024-8767

    How do I append the records to insert the "-" at these locations.

    Thanks,

    Victor

  2. #2
    JuggaloBrotha's Avatar
    JuggaloBrotha is offline VB.NET Forum Moderator JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist
    .NET Framework
    .NET 2.0
    Join Date
    Jun 2004
    Location
    Lansing, MI; USA
    Age
    26
    Posts
    3,897
    Reputation
    524

    Default

    Pull the data out as a string, then re-assign the string using .SubString() to insert the hyphen(s) in the correct places. For Example:
    Code:
    Dim myString As String = "123456"
    myString = myString.SubString(0I, 3I) & "-" & myString.SubString(3I, 3I)
    'myString is now "123-456"
    Currently using: VS 2005 & 2008 Pro w/sp1 & VS 2010 Ultimate on Win7 Ultimate x64.


    There are 3 kinds of people in the world: Those who can count and those who can't.
    4 out of 3 people have trouble with fractions.

    Windows has a 64 bit GUI for a set of 32 bit extensions on a 16 bit shell for an 8 bit OS using a 4 bit kernel made by a 2 bit company that can't stand 1 bit of competition.

  3. #3
    victor64 is offline VB.NET Forum Enthusiast victor64 is on a distinguished programming path ahead
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Nov 2008
    Age
    46
    Posts
    56
    Reputation
    24

    Default How to insert "-" in field data

    Hello,

    Thanks for your reply, I'm afraid I can't assign mystring to a field value.
    Do you have another approach?

    This is what I have so far, but can't figure out the mySQL_Statement, or perhaps I need to use a For Next stament to go over each record??

    Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\aopt2002orgorg.mdb;Persist Security Info=True;Jet OLEDBatabase Password=testaopupdate"
    Dim objConnection As New OleDb.OleDbConnection(ConnectionString)

    'data adapter
    Dim objDataAdapter As New OleDb.OleDbDataAdapter(mySQL_Statement, objConnection)

    'dataset object
    Dim objDataSet As New DataSet
    'fill dataset
    objConnection.Open()
    objDataAdapter.Fill(objDataSet, "SN")
    objConnection.Close()

    Thanks

  4. #4
    MattP is offline VB.NET Forum All-Mighty MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute
    .NET Framework
    .NET 4.0
    Join Date
    Feb 2008
    Location
    USA
    Posts
    1,016
    Reputation
    533

    Default

    I'd use the substring function for T-SQL. Here's an example of formatting a social security number.

    Code:
    SELECT SUBSTRING(emp_ssn, 0, 3) + '-' + SUBSTRING(emp_ssn, 4, 2) + '-' + SUBSTRING(emp_ssn, 6, 4) AS SSN
    FROM employee_table
    I believe (don't use Access) that instead of SUBSTRING you use MID (same parameters) and '&' to concatenate rather than '+'.

  5. #5
    victor64 is offline VB.NET Forum Enthusiast victor64 is on a distinguished programming path ahead
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Nov 2008
    Age
    46
    Posts
    56
    Reputation
    24

    Default

    Matt,

    Thanks for your input, I believe SSN will be a new field created automatically. I was able to make the conversion, but the code I used was much longer, I will also try it with your code.

    I initially made a mistake the data in the field didn't have the first 4 characters, basically it was in two separate fields:

    Field 1: 1340 and Field 2: 001188989, I modified the code to obtain 00-118-8989

    Now I need to do a check with a second table which contains

    1340-00-118-8989(1)
    1305-00-198-8989
    1340-00-118-8989(1)
    1315-00-108-7777
    etc...

    How do I start my search after the fifth position to check against data in the table which contains the 00-118-8989 format and only take into consideration the 11 chatracters ( will also need to avoid the (1) data from the other table).

    I propbably should open a new thread, but since you're familiar with what I'm trying to do, I thought it would be easier to get some feedback.

    Thanks,

    Victor

  6. #6
    MattP is offline VB.NET Forum All-Mighty MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute
    .NET Framework
    .NET 4.0
    Join Date
    Feb 2008
    Location
    USA
    Posts
    1,016
    Reputation
    533

    Default

    You want to use these 2 values to do a JOIN on the tables?

    Code:
    SELECT *
    FROM TableA a JOIN TableB b ON MID(a.column, 5, 11) = b.column

  7. #7
    victor64 is offline VB.NET Forum Enthusiast victor64 is on a distinguished programming path ahead
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Nov 2008
    Age
    46
    Posts
    56
    Reputation
    24

    Default Searching for data to display from two tables a link field.

    How would you use this statement with a where clause?
    I will search the tables bases on values entered in a textbox.

    Thanks,

    Victor

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts