Results 1 to 7 of 7

Thread: How to set mysql auto increment varchar

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2017
    Posts
    6

    How to set mysql auto increment varchar

    Done:
    i already created Customer id as integer, primary Key and Auto increment in customer table.
    Label, Text box and Save button created in VB.NET in form1
    Expiation :r
    while opening form1, customer id should be generated like C - 0001
    Kindly help

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,753

    Re: How to set mysql auto increment varchar

    Whenever you call your Insert command, do not pass a value for the auto-increment column and one will be generated for you.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2017
    Posts
    6

    Re: How to set mysql auto increment varchar

    Thanks for your replay how i insert string into that eg: " C - 0001"

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: How to set mysql auto increment varchar

    Auto increment won't work on a varchar field. It only makes sense for numeric fields. Therefore, you have a couple options:

    1) Leave an auto increment field holding just the "1" part of "C - 0001", while a second column holds the "C" part as a varchar field. When you want to display it, you concatenate the varchar field with " - ", then the auto increment field formatted to four characters.

    2) Don't use an auto increment field at all, and just use a varchar field that holds the whole value. For this to work, it will be up to you to figure out what each key should be. There would be many ways to do this, such as storing the highest value in some other table, or just digesting the varchar "C - 0001" to get the number part and getting the maximum from that. Of course, that would mean that the letter would be irrelevant, so there's at least one, more complex version, where you have a different table that keeps a letter and the highest value associated with that letter.
    My usual boring signature: Nothing

  5. #5
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,758

    Re: How to set mysql auto increment varchar

    I would leave the auto increment and adjust the SQL to give you what you want.

    If the "C" simply denotes you are pulling from the customers table then use this sql command....

    Code:
    select concat('C-',LPAD(id,4,'0')) from <myTable>
    If the "C" comes from the database, then adjust the command to fetch it rather than hard code it.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: How to set mysql auto increment varchar

    That would be my preference, as well.
    My usual boring signature: Nothing

  7. #7
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: How to set mysql auto increment varchar

    How we do it: Three fields: SEQUENCEID - int, autoinc; USERDEFINEDID - varchar; CALCUSERID - computed field, if there's something in the USERDEFINEDID field, that is returned as it is... if not, then the prefix (C-) is added to a 0-padded SEQUENCEID ....and we get C-00000008


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width