Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2007
    Posts
    23

    Unanswered: Convert int to varchar

    I'm trying to do a looping to create large amout of user.

    DECLARE v1 INT DEFAULT 0;
    WHILE v1 < 10000 DO
    INSERT INTO account
    VALUES ("user"+v1,"password","abc@email.com");
    SET v1 = v1 + 1;
    END WHILE;

    I tried to use: "user"+v1

    but it doesnt seem to be working

    any idea how to solve it?

    thanks a lot

  2. #2
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    You need to use the format
    Code:
    CONCAT('user',CAST(1 AS BINARY))
    so :

    Code:
    DECLARE v1 INT DEFAULT 0;
    WHILE v1 < 10000 DO
    INSERT INTO account
    VALUES (CONCAT('user',CAST(v1 AS BINARY)),'password','abc@email.com');
    SET v1 = v1 + 1;
    END WHILE;
    p.s. please use single quotes ' to encapsulate strings, NOT double quotes "
    Last edited by aschk; 05-18-07 at 06:29.

  3. #3
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    Just decided to do a spot of manual reading on the subject and in MySQL the CONCAT function implicitly converts numbers to BINARY type.

    So :
    CONCAT('user',CAST(1 AS BINARY)) === CONCAT('user',1)

Posting Permissions

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