I'm having problems with creating my Function GenerateID. The function is supposed to created an ID formatted in the following:
last 2 digits of the year + '-' + The unique ID which is incremental having a format of '0001' Max 4digits only so it's supposed to look like this --> 13-0001, 13-0002, 13-0003 etc.
So now I have this table and function I have the unique identifier which is auto-incremental the ID while I have the Number which will contain the '13-0001' etc numbers. Basically my function will get the ID and combine those with the Year, '-' and the '000' through the use of concat and cast but it doesnt look like it will work as I'm getting a syntax error in creating my function.
--MYSQL
-- Function
DROP FUNCTION generateNumbers
CREATE FUNCTION generateNumbers (id int)
RETURNS VARCHAR(10)
RETURN CONCAT(substr((cast(CURDATE() as varchar(10))),3,2), '-', right(concat('000000', cast( id as varchar(6) ) ), 6) );
-- Trigger
DROP TRIGGER generateNumber_Insert
DELIMITER ||
CREATE TRIGGER generateNumber_Insert BEFORE INSERT ON ABC
FOR EACH ROW
BEGIN
SET NEW.Number = generateNumbers(abc.ID) ;
END
||
--
DROP TABLE abc;
Create table abc
(
ID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
Number VARCHAR(10),
name varchar(32)
)
I'm not sure what's up with my syntax. And also, is it better to USE COnvert() than CAST()? or vice versa? Can't seem to differentiate the function of the 2.
Thanks