If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Convert int to varchar

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-18-07, 05:08
desmondtanck desmondtanck is offline
Registered User
 
Join Date: Apr 2007
Posts: 23
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
Reply With Quote
  #2 (permalink)  
Old 05-18-07, 05:24
aschk aschk is offline
Registered User
 
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 05:29.
Reply With Quote
  #3 (permalink)  
Old 05-18-07, 05:28
aschk aschk is offline
Registered User
 
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)
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On