Hi.
On Mon, Dec 10, 2001 at 06:30:47PM +0530, [EMAIL PROTECTED] wrote:
[...]
> here shortname is varchar and pcallid is integer. i need shortname and
> pcallid as ' callno' of length 6. pcallid starts from 1 .. 999.
>
> to get effect like for callnumber 'xxx001' i plan to add pcallid by 1000 and
> pick last 3 characters and concat with shortname.
If you always want pcallid to have always leading zeros, you can
declared it like
PCallID SMALLINT(3) ZEROFILL
> concat(ShortName , substring(Cast( (.PCallID + 1000) as varchar(10) ),2
> 3) ) as 'Call No'
>
> here i would like to cast (.PCallID + 1000) as varchar
As I said, in such situations MySQL will perform an implicit cast, so
simply write:
SELECT CONCAT(ShortName, SUBSTRING(PCallID + 1000, 2, 3)) AS 'Call No'
Btw, alternatives to that would be
SELECT CONCAT(ShortName, RIGHT(PCallID + 1000, 3) AS 'Call No'
SELECT CONCAT(ShortName, LPAD(PCallID, 3, '0') AS 'Call No'
or, if the column is declared with ZEROFILL, as explained above
SELECT CONCAT(ShortName, PCallID) AS 'Call No'
Bye,
Benjamin.
[...]
> > > how can i cast a variable either from integer to varchar or varchar to
> > > integer in mysql.
> >
> > That's not possible in the way you ask for. Values will get
> > automatically converted to the column type in question. In
> > expressions, MySQL doesn't use "varchar", but simply string, integer
> > and floating point.
> >
> > You can force a string to an integer by adding 0 (like
> > 'SELECT "1234"+0') and an number to a string by some string functions
> > (like 'SELECT CONCAT(10)').
> >
> > I assume, there are no explicit casts, because if you want to add some
> > number, you get the conversion implicitly by using '+'.
> >
> > If you could provide an example why you need explicit casts, maybe we
> > could give a more precise answer.
--
[EMAIL PROTECTED]
---------------------------------------------------------------------
Before posting, please check:
http://www.mysql.com/manual.php (the manual)
http://lists.mysql.com/ (the list archive)
To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php