SMALLINT is the smallest binary integer type in DB2 for z/OS: two bytes, signed, exact. This page covers range and storage, when SMALLINT is the right choice, overflow and promotion behavior, and COBOL / host-variable mapping notes.
A SMALLINT column stores a binary integer with a precision of 15 bits. The representable range is −32768 through +32767. Storage is 2 bytes per value.
| Topic | Detail |
|---|---|
| Range | −32768 to +32767 |
| Storage | 2 bytes |
| Nulls | Allowed unless NOT NULL |
| Exact? | Yes—exact integers in range |
123456789CREATE TABLE APP.ITEM_STATUS ( STATUS_CODE SMALLINT NOT NULL, STATUS_NAME VARCHAR(30) NOT NULL, PRIMARY KEY (STATUS_CODE) ); INSERT INTO APP.ITEM_STATUS VALUES (1, 'NEW'); INSERT INTO APP.ITEM_STATUS VALUES (32767, 'MAX_OK'); -- INSERT INTO APP.ITEM_STATUS VALUES (32768, 'TOO_BIG'); -- overflow
Like other data types, SMALLINT may be null unless you specify NOT NULL. Primary keys and many code tables declare NOT NULL. The SMALLINT scalar function converts compatible values to SMALLINT (and can overflow if the source is too large).
123VALUES SMALLINT(100); VALUES SMALLINT(DECIMAL(20,0)); -- ok -- VALUES SMALLINT(40000); -- overflow
Choose SMALLINT when the domain is naturally small and stable:
Prefer INTEGER or BIGINT when:
Do not use SMALLINT for money. Even “dollars without cents” eventually exceed 32767 for real businesses. Use DECIMAL for currency.
Space savings are real but modest: millions of rows × 2 bytes versus 4 bytes can matter on huge tables and indexes, yet wrong range assumptions cost more than the bytes you saved. When in doubt, INTEGER is a safer default for new designs.
Any attempt to place a value outside −32768…32767 into a SMALLINT target fails with a numeric overflow or assignment error. Sources include:
123UPDATE APP.ITEM_STATUS SET STATUS_CODE = STATUS_CODE + 1 WHERE STATUS_CODE = 32767; -- next value would overflow
Application code should validate halfword ranges before writing, especially when values originate as INTEGER in COBOL or Java and are narrowed into SMALLINT columns.
On the numeric promotion precedence list, SMALLINT can promote toward INTEGER, BIGINT, decimal, real, double, and DECFLOAT. That helps function resolution when a routine expects a wider numeric parameter. Mixed expressions often yield INTEGER or DECIMAL results rather than staying SMALLINT—check the SQL Reference operation tables when the result type matters for downstream casts.
123-- SMALLINT column compared/combined with larger types promotes as needed SELECT STATUS_CODE * 1000 -- result type widens per arithmetic rules FROM APP.ITEM_STATUS;
Embedded SQL needs host variables that match SMALLINT’s binary halfword nature. Typical COBOL declarations (confirm with your shop):
12301 HV-STATUS-CODE PIC S9(4) COMP. * or PIC S9(4) COMP-5 / BINARY per standards 01 HV-STATUS-CODE-IND PIC S9(4) COMP.
Practical notes:
1234567891011EXEC SQL SELECT STATUS_CODE INTO :HV-STATUS-CODE:HV-STATUS-CODE-IND FROM APP.ITEM_STATUS WHERE STATUS_NAME = 'NEW' END-EXEC IF HV-STATUS-CODE-IND < 0 DISPLAY 'STATUS WAS NULL' ELSE DISPLAY 'CODE=' HV-STATUS-CODE END-IF
If your program uses INTEGER host variables against SMALLINT columns, Db2 converts on the way in and out—but out-of-range program values still overflow on INSERT/UPDATE. Matching types reduces conversion cost and surprise.
SMALLINT is a tiny number box that only holds counts from about negative thirty-two thousand to positive thirty-two thousand. It is perfect for “status 1, 2, 3” stickers. If you try to put a huge score into the tiny box, Db2 says no (overflow). In COBOL, you give the program a matching tiny box (a halfword COMP field) so Db2 can hand the number back and forth without squeezing it into the wrong size toy.
1. What is the range of SMALLINT?
2. How many bytes does a SMALLINT column use?
3. When is SMALLINT a good choice?
4. What happens if you INSERT 40000 into SMALLINT?
5. A common COBOL mapping for SMALLINT is: