Math calculation in a function : Function « Procedure Function « SQL / MySQL

Home
SQL / MySQL
1.Aggregate Functions
2.Backup Load
3.Command MySQL
4.Cursor
5.Data Type
6.Database
7.Date Time
8.Engine
9.Event
10.Flow Control
11.FullText Search
12.Function
13.Geometric
14.I18N
15.Insert Delete Update
16.Join
17.Key
18.Math
19.Procedure Function
20.Regular Expression
21.Select Clause
22.String
23.Table Index
24.Transaction
25.Trigger
26.User Permission
27.View
28.Where Clause
29.XML
SQL / MySQL » Procedure Function » Function 
Math calculation in a function
       

CREATE TABLE TEAMS_NEW
      (TEAMNO     INTEGER NOT NULL PRIMARY KEY,
       EmployeeNO   INTEGER NOT NULL,
       DIVISION   SET ('first','second','third','fourth'));

INSERT INTO TEAMS_NEW VALUES (127'first')
;
INSERT INTO TEAMS_NEW VALUES (227'first,third')
;
INSERT INTO TEAMS_NEW VALUES (327'first,third,sixth')
;
INSERT INTO TEAMS_NEW VALUES (427'first,fifth')
;
INSERT INTO TEAMS_NEW VALUES (527, NULL)
;
INSERT INTO TEAMS_NEW VALUES (6277)
;
INSERT INTO TEAMS_NEW VALUES (727, CONV(1001,2,10))
;
SELECT FROM TEAMS_NEW;


SELECT   TEAMNO, DIVISION
FROM     TEAMS_NEW
WHERE    DIVISION & POWER(2,3-1= POWER(2,3-1)
;

delimiter $$

CREATE FUNCTION POSITION_IN_SET
   (P_COLUMN BIGINT, POSITION SMALLINT)
   RETURNS BOOLEAN
BEGIN
   RETURN (P_COLUMN & POWER(2, POSITION-1
          POWER(2,POSITION-1));
END$$

delimiter ;

SELECT   TEAMNO, DIVISION
FROM     TEAMS_NEW
WHERE    POSITION_IN_SET(DIVISION, 3);

drop table teams_new;

   
    
    
    
    
    
    
  
Related examples in the same category
1.Creating Functions
2.A function that takes a parameter, performs an operation using an SQL function, and returns the result
3.Single statement function
4.Nested function call
5.Using buildin function in user-defined function
6.To remove a stored function, use the DROP command
7.Create a procedure for 'READS SQL DATA'
8.Function to shorten a string
9.Calculate number of day
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.