Caché SQL Reference
Data Types
 [Next]
   
Server:docs2
Instance:LATEST
User:UnknownUser
 
-
Go to:
Search:    

Specifies the kind of data that an SQL entity (such as a column) can contain.
Description
The following topics are described here:
A data type specifies the kind of value that a column can hold. You specify the data type when defining a field with CREATE TABLE. When defining SQL expressions, you can specify the DDL data types listed in the following table (left-hand column). When you specify one of these DDL data types, it maps to the Caché data type listed in the right-hand column.
To view the current system data type mappings, go to the Management Portal, select [Home] > [Configuration] > [System-defined DDL Mappings].
You can also define additional user data types. To create or view the user data type mappings, go to the Management Portal, select [Home] > [Configuration] > [User-defined DDL Mappings].
Table of DDL Data Types
DDL Data Type Corresponding Caché Class Property Data Type
BIGINT %Library.BigInt
BIGINT(%1)
Equivalent to BIGINT. Provided for MySQL compatibility.
BINARY %Library.Binary(MAXLEN=1)
BINARY(%1) %Library.Binary(MAXLEN=%1)
BINARY VARYING %Library.Binary(MAXLEN=1)
BINARY VARYING(%1) %Library.Binary(MAXLEN=%1)
BIT %Library.Boolean
CHAR %Library.String(MAXLEN=1)
CHAR(%1) %Library.String(MAXLEN=%1)
CHAR VARYING %Library.String(MAXLEN=1)
CHAR VARYING(%1) %Library.String(MAXLEN=%1)
CHARACTER %Library.String(MAXLEN=1)
CHARACTER VARYING %Library.String(MAXLEN=1)
CHARACTER VARYING(%1) %Library.String(MAXLEN=%1)
CHARACTER(%1) %Library.String(MAXLEN=%1)
DATE %Library.Date
DATETIME %Library.DateTime
DATETIME2 %Library.DateTime
DEC %Library.Numeric A 64–bit signed integer. (MAXVAL=9223372036854775807, MINVAL=-9223372036854775808, SCALE=0)
DEC(%1) %Library.Numeric (MAXVAL=<|'$$maxval^%apiSQL(%1,0)'|>, MINVAL=<|'$$minval^%apiSQL(%1,0)'|>, SCALE=0). The largest meaningful value for %1 is 19; %1 values larger than 19 do not issue an error, but default to 19.
DEC(%1,%2) %Library.Numeric (MAXVAL=<|'$$maxval^%apiSQL(%1,%2)'|>, MINVAL=<|'$$minval^%apiSQL(%1,%2)'|>, SCALE=%2)
DECIMAL %Library.Numeric A 64–bit signed integer. (MAXVAL=9223372036854775807, MINVAL=-9223372036854775808, SCALE=0)
DECIMAL(%1) %Library.Numeric (MAXVAL=<|'$$maxval^%apiSQL(%1,0)'|>, MINVAL=<|'$$minval^%apiSQL(%1,0)'|>, SCALE=0) The largest meaningful value for %1 is 19; %1 values larger than 19 do not issue an error, but default to 19.
DECIMAL(%1,%2) %Library.Numeric (MAXVAL=<|'$$maxval^%apiSQL(%1,%2)'|>, MINVAL=<|'$$minval^%apiSQL(%1,%2)'|>, SCALE=%2)
DOUBLE %Library.Double This is the IEEE floating point standard. An SQL column with this data type returns a default precision of 20. For further details (including important max/min value limits), refer to the $DOUBLE function in the Caché ObjectScript Reference. Prior to Caché 5.2, this data type was equivalent to FLOAT.
DOUBLE PRECISION %Library.Double This is the IEEE floating point standard. An SQL column with this data type returns a default precision of 20. For further details (including important max/min value limits), refer to the $DOUBLE function in the Caché ObjectScript Reference. Prior to Caché 5.2, this data type was equivalent to FLOAT.
FLOAT (deprecated, use either DECIMAL or DOUBLE) %Library.Float
FLOAT(%1) (deprecated, use either DECIMAL or DOUBLE) %Library.Float (MAXVAL=<|'$$maxval^%apiSQL(%1)'|>, MINVAL=<|'$$minval^%apiSQL(%1)'|>)
IMAGE %Stream.GlobalBinary
INT %Library.Integer (MAXVAL=2147483647, MINVAL=-2147483648)
INT(%1)
%Library.Integer (MAXVAL=2147483647, MINVAL=-2147483648)
Equivalent to INT. Provided for MySQL compatibility.
INTEGER %Library.Integer (MAXVAL=2147483647, MINVAL=-2147483648)
LONG %Stream.GlobalCharacter
LONG BINARY %Stream.GlobalBinary
LONG RAW %Stream.GlobalBinary
LONGTEXT
Equivalent to LONG. Provided for MySQL compatibility.
LONG VARCHAR %Stream.GlobalCharacter
LONG VARCHAR(%1) %Stream.GlobalCharacter
LONGVARBINARY %Stream.GlobalBinary
LONGVARBINARY(%1) %Stream.GlobalBinary
LONGVARCHAR %Stream.GlobalCharacter
LONGVARCHAR(%1) %Stream.GlobalCharacter
MEDIUMINT
%Library.Integer(MAXVAL=8388607,MINVAL=-8388608)
Provided for MySQL compatibility.
MEDIUMINT(%1)
%Library.Integer(MAXVAL=8388607,MINVAL=-8388608)
Provided for MySQL compatibility.
MONEY %Library.Currency(MAXVAL=922337203685477.5807, MINVAL=-922337203685477.5808, SCALE=4)
NATIONAL CHAR %Library.String(MAXLEN=1)
NATIONAL CHAR(%1) %Library.String(MAXLEN=%1)
NATIONAL CHAR VARYING %Library.String(MAXLEN=1)
NATIONAL CHAR VARYING(%1) %Library.String(MAXLEN=%1)
NATIONAL CHARACTER %Library.String(MAXLEN=1)
NATIONAL CHARACTER(%1) %Library.String(MAXLEN=%1)
NATIONAL CHARACTER VARYING %Library.String(MAXLEN=1)
NATIONAL CHARACTER VARYING(%1) %Library.String(MAXLEN=%1)
NATIONAL VARCHAR %Library.String(MAXLEN=1)
NATIONAL VARCHAR(%1) %Library.String(MAXLEN=%1)
NCHAR %Library.String(MAXLEN=1)
NCHAR(%1) %Library.String(MAXLEN=%1)
NTEXT %Stream.GlobalCharacter
NUMBER %Library.Numeric A 64–bit signed integer. (MAXVAL=9223372036854775807, MINVAL=-9223372036854775808, SCALE=0)
NUMBER(%1) %Library.Numeric (MAXVAL=<|'$$maxval^%apiSQL(%1)'|>, MINVAL=<|'$$minval^%apiSQL(%1)'|>, SCALE=0) The largest meaningful value for %1 is 19; %1 values larger than 19 do not issue an error, but default to 19.
NUMBER(%1,%2) %Library.Numeric (MAXVAL=<|'$$maxval^%apiSQL(%1,%2)'|>, MINVAL=<|'$$minval^%apiSQL(%1,%2)'|>, SCALE=%2)
NUMERIC %Library.Numeric A 64–bit signed integer. (MAXVAL=9223372036854775807, MINVAL=-9223372036854775808, SCALE=0)
NUMERIC(%1) %Library.Numeric (MAXVAL=<|'$$maxval^%apiSQL(%1,0)'|>, MINVAL=<|'$$minval^%apiSQL(%1,0)'|>, SCALE=0) The largest meaningful value for %1 is 19; %1 values larger than 19 do not issue an error, but default to 19.
NUMERIC(%1,%2) %Library.Numeric (MAXVAL=<|'$$maxval^%apiSQL(%1,%2)'|>, MINVAL=<|'$$minval^%apiSQL(%1,%2)'|>, SCALE=%2)
NVARCHAR %Library.String(MAXLEN=1)
NVARCHAR(%1) %Library.String(MAXLEN=%1)
NVARCHAR(%1,%2) %Library.String(MAXLEN=%1)
NVARCHAR(MAX)
Equivalent to LONGVARCHAR. Provided for TSQL compatibility.
RAW(%1) %Library.Binary(MAXLEN=%1)
REAL (deprecated, use either DECIMAL or DOUBLE) %Library.Float
ROWVERSION
A system-assigned sequential integer. See ROWVERSION Data Type for details.
SERIAL %Library.Counter
SMALLDATETIME %Library.DateTime MAXVAL=’2079-06- 06-23:59:59’; MINVAL=’1900-01-01 00:00:00’)
SMALLINT %Library.SmallInt
SMALLINT(%1)
Equivalent to SMALLINT. Provided for MySQL compatibility.
SMALLMONEY %Library.Currency SCALE=4
SYSNAME %Library.String(MAXLEN=128)
TEXT %Stream.GlobalCharacter
TIME %Library.Time
TIMESTAMP %Library.TimeStamp
TINYINT %Library.TinyInt
TINYINT(%1)
Equivalent to TINYINT. Provided for MySQL compatibility.
UNIQUEIDENTIFIER %Library.UniqueIdentifier
VARBINARY %Library.Binary(MAXLEN=1)
VARBINARY(%1) %Library.Binary(MAXLEN=%1)
VARCHAR %Library.String(MAXLEN=1)
VARCHAR(%1) %Library.String(MAXLEN=%1)
VARCHAR(%1,%2) %Library.String(MAXLEN=%1)
VARCHAR2(%1) %Library.String(MAXLEN=%1)
VARCHAR(MAX)
Equivalent to LONGVARCHAR. Provided for TSQL compatibility only.
Important:
Each of the DDL or Caché data type expressions shown above is actually one continuous string. These strings may contain space characters, but generally do not contain white space of any kind. Some white space appears in this table for readability.
MAXLEN and Space Usage
Caché SQL should not be affected by an overly large MAXLEN value. When specifying a %Library.String data type, the MAXLEN value you specify does not have to correspond closely to the actual size of the data. If the field value is "ABC", Caché only uses that much space on disk, in the global buffers, and in private process memory. Even if the field is declared with MAXLEN=1000, the private process memory does not allocate that much space for the field. Caché only allocates memory for the actual size of the field value, regardless of the declared length.
ODBC applications may be affected by an overly large MAXLEN value. ODBC applications try to make decisions about the size of a field needed based on metadata from the server, so the application may allocate more buffer space than then is actually needed. The Caché ODBC driver takes the data from the TCP buffer and converts it into the applications buffer, so MAXLEN size does not affect our ODBC client.
JDBC applications should not be affected by an overly large MAXLEN value. Java and .Net do not have the application allocate buffers. The clients only allocated what is needed to hold the data as a native type.
Precision and Scope
Numeric data types such as NUMERIC(6,2) have two integer values (p,s) precision and scope. These are mapped to Caché %Library class data types, as described in “Understanding DDL Data Type Mappings”. When specified in an SQL data type, the following apply on Windows systems (maximums may differ on other systems):
The following example shows the maximum values for different combinations of precision and scope:
  FOR i=0:1:6 {
      WRITE "Max for (",i,",2)=",$$maxval^%apiSQL(i,2),!}
 
SQL System Data Type Mappings
The syntax shown for DDL and Caché data type expressions in the above table are the default mappings configured for the SQL.SystemDataTypes. There are separate mapping tables available for supplied system data types, and user data types.
To view and modify the current data type mappings, Go to the Management Portal, select [Home] > [Configuration] > [System-defined DDL Mappings].
Understanding DDL Data Type Mappings
When mapping data types from DDL to Caché, regular parameters and function parameters follow these rules:
Data Type Precedence
When an operation can return several different values, and these values may have different data types, Caché assigns the return value whichever data type has the highest precedence. For example, a NUMERIC data type can contain all possible INTEGER data type values, but an INTEGER data type cannot contain all possible NUMERIC data type values. Thus NUMERIC has the higher precedence (is more inclusive).
For example, if a CASE statement has a possible result value of data type INTEGER, and a possible result value of data type NUMERIC, the actual result is always of type NUMERIC, regardless of which of these two cases are taken.
The precedence for data types is as follows, from highest (most inclusive) to lowest:
LONGVARBINARY
LONGVARCHAR
VARBINARY
VARCHAR
GUID
TIMESTAMP
DOUBLE
NUMERIC
BIGINT
INTEGER
DATE
TIME
SMALLINT
TINYINT
BIT
Normalize and Validate
The %Library.DataType superclass contains classes for the specific data types. These data type classes provide a Normalize() method to normalize an input value to the data type format and an IsValid() method to determine if an input value is valid for that data type, as well as various mode conversion methods such as LogicalToDisplay() and DisplayToLogical().
The following examples show the Normalize() method for the TimeStamp data type:
  SET indate=63445
  SET tsdate=##class(%Library.TimeStamp).Normalize(indate)
  WRITE "TimeStamp date: ",tsdate
 
  SET indate="2014-2-2"
  SET tsdate=##class(%Library.TimeStamp).Normalize(indate)
  WRITE "TimeStamp date: ",tsdate
 
The following examples show the IsValid() method for the TimeStamp data type:
  SET datestr="July 4, 2014"
  SET stat=##class(%Library.TimeStamp).IsValid(datestr)
    IF stat=1 {WRITE datestr," is a valid TimeStamp",! }
    ELSE {WRITE datestr," is not a valid TimeStamp",!}
 
  SET leapdate="2004-02-29 00:00:00"
  SET noleap="2005-02-29 00:00:00"
  SET stat=##class(%Library.TimeStamp).IsValid(leapdate)
    IF stat=1 {WRITE leapdate," is a valid TimeStamp",! }
    ELSE {WRITE leapdate," is not a valid TimeStamp",!}
  SET stat=##class(%Library.TimeStamp).IsValid(noleap)
    IF stat=1 {WRITE noleap," is a valid TimeStamp",! }
    ELSE {WRITE noleap," is not a valid TimeStamp",!}
 
Date, Time, and Timestamp Data Types
You can define date, time, and timestamp data types, and interconvert dates and timestamps through standard Caché SQL date and time functions. For example, you can use CURRENT_DATE or CURRENT_TIMESTAMP as input to a field defined with that data type, or use DATEADD, DATEDIFF, DATENAME, or DATEPART to manipulate date values stored with this data type.
The data type classes %Library.Date, %Library.Time, %Library.TimeStamp, %MV.Date, %Library.FilemanDate, and %Library.FilemanTimeStamp are treated as follows with regard to SqlCategory:
  1. %Library.Date classes, and any user-defined data type class that has a logical value of +$HOROLOG (the date portion of $HOROLOG) should use DATE as the SqlCategory.
  2. %Library.Time classes, and any user-defined data type class that has a logical value of $PIECE($HOROLOG,”,”,2) (the time portion of $HOROLOG) should use TIME as the SqlCategory. TIME supports fractional seconds, so this data type can also be used for HH:MI:SS.FF to a user-specified number of digits of precision. To support fractional seconds set the Precision parameter: a value of 0 rounds to the nearest second; a value of "" (the default) retains whatever precision is specified in the data value.
  3. %Library.TimeStamp classes, and any user-defined data type class that has a logical value of YYYY-MM-DD HH:MI:SS.FF should use TIMESTAMP as the SqlCategory.
  4. %Library.DateTime is a subclass of %Library.TimeStamp. It defines a type parameter named DATEFORMAT and it overrides the DisplayToLogical() and OdbcToLogical() methods to handle imprecise datetime input that TSQL applications are accustomed to. The xDBC data type is TIMESTAMP and the SqlCategory is TIMESTAMP.
  5. %MV.Date classes, or any user-defined data type class that has a logical date value of $HOROLOG-46385, should use MVDATE as the SqlCategory.
  6. %Library.FilemanDate classes, or any user-defined data type class that has a logical date value of CYYMMDD, should use FMDATE as the SqlCategory.
  7. %Library.FilemanTimeStamp classes, or any user-defined data type class that has a logical date value of CYYMMDD.HHMMSS, should use FMTIMESTAMP as the SqlCategory.
  8. A user-defined date data type that does not fit into any of the preceding logical values should define the SqlCategory of the data type as DATE and provide in the data type class a LogicalToDate() method to convert a user-defined logical date value to a %Library.Date logical value, and a DateToLogical() method to convert a %Library.Date logical value to the user-defined logical date value.
  9. A user-defined time data type that does not fit into any of the preceding logical values should define the SqlCategory of the data type as TIME and provide in the data type class a LogicalToTime() method to convert a user-defined logical time value to a %Library.Time logical value, and a TimeToLogical() method to convert a %Library.Time logical value to the user-defined logical time value.
  10. A user-defined timestamp data type that does not fit into any of the preceding logical values should define the SqlCategory of the data type as TIMESTAMP and provide in the data type class a LogicalToTimeStamp() method to convert a user-defined logical timestamp value to a %Library.TimeStamp logical value, and a TimeStampToLogical() method to convert a %Library.TimeStamp logical value to the user-defined logical timestamp value.
When comparing FMTIMESTAMP category values with DATE category values, Caché no longer strips the time from the FMTIMESTAMP value before comparing it to the DATE. This is identical behavior to comparing TIMESTAMP with DATE values, and comparing TIMESTAMP with MVDATE values. It is also compatible with how other SQL vendors compare timestamps and dates. This means a comparison of a FMTIMESTAMP 320110202.12 and DATE 62124 are equal when compared using the SQL equality (=) operator. Applications must convert the FMTIMESTAMP value to a DATE or FMDATE value to compare only the date portions of the values.
Dates Prior to December 31, 1840
A date is commonly represented by the DATE data type. This data type stores a date in $HOROLOG format, as a positive integer count of days from the arbitrary starting date of December 31, 1840.
By default, dates can only be represented by a positive integer (MINVAL=0). However, you can change the MINVAL type parameter to enable storage of dates prior to December 31, 1840. By setting MINVAL to a negative number, you can store dates prior to December 31, 1840 as negative integers. The earliest allowed MINVAL value is -672045. This corresponds to January 1 of Year 1 (CE). DATE data type cannot represent BCE (also known as BC) dates.
Note:
Be aware that these date counts do not take into account changes in date caused by the Gregorian calendar reform (enacted October 15, 1582, but not adopted in Britain and its colonies until 1752).
You can redefine the minimum date for your locale as follows:
  SET oldMinDate = ##class(%SYS.NLS.Format).GetFormatItem("DATEMINIMUM")
  IF oldMinDate=0 {
    DO ##class(%SYS.NLS.Format).SetFormatItem("DATEMINIMUM",-672045)
    SET newMinDate = ##class(%SYS.NLS.Format).GetFormatItem("DATEMINIMUM")
    WRITE "Changed earliest date to ",newMinDate
    }
  ELSE { WRITE "Earliest date was already reset to ",oldMinDate}
The above example sets the MINVAL for your locale to the earliest permitted date (1/1/01).
Note:
Caché does not support using Julian dates with negative logical DATE values (%Library.Date values with MINVAL<0). Thus, these MINVAL<0 values are not compatible with the Julian date format returned by the TO_CHAR function.
Long Strings
Caché provides support for long strings. Long strings are strings greater than 32,767 characters (64K bytes) up to a maximum length of 3,641,144 characters. Commonly, such strings should be assigned one of the %Stream.GlobalCharacter data types. (Prior to version 2011.1, these were assigned the CStream%String (or %Library.GlobalCharacterStream) data type; these older data types continue to be fully supported.)
Long string support is enabled by default. You can disable or enable long string support for a Caché instance. If disabled, you can enable long strings using either the Management Portal or the Caché ObjectScript EnableLongStrings property of the Config.Miscellaneous class. In the Management Portal select [Home] > [Configuration] > [Memory and Startup]. To enable support for long strings system-wide, select the Enable Long Strings check box. Then click the Save button. After long strings are enabled, any future invoked process on that system will support long strings. For further details, refer to Long Strings in the “Data Types and Values” chapter of Using Caché ObjectScript.
With long strings enabled, you can assign a %Library.String data types a MAXLEN of greater than 16,374 Unicode characters (or 32K 8-bit characters). How such long strings are handled depends on your xDBC protocol:
The protocol that is used is the highest protocol supported by both the Caché instance and the ODBC driver facilities. If all facilities on both host and client support Protocol 47, that protocol is used. If any one facility only supports Protocol 46 that protocol is used, regardless of Protocol 47 support in other facilities. The protocol that was actually used is recorded in the Caché ODBC log.
List Structures
Caché supports the list structure data type %List (data type class %Library.List). This is a compressed binary format, which does not map to a corresponding native data type for Caché SQL. In its internal representation it corresponds to data type VARBINARY with a default MAXLEN of 32749.
For this reason, Dynamic SQL cannot use %List data in a WHERE clause comparison. You also cannot use INSERT or UPDATE to set a property value of type %List.
Dynamic SQL returns the data type of list structured data as VARCHAR.
If you use an ODBC or JDBC client, %List data is projected to VARCHAR string data, using LogicalToOdbc conversion. A list is projected as a string with its elements delimited by commas. Data of this type can be used in a WHERE clause, and in INSERT and UPDATE statements.
For further details on data type class %Library.List, refer to the InterSystems Class Reference. For further details on using lists in a WHERE clause, see the %INLIST predicate and the FOR SOME %ELEMENT predicate. For further details on handling list data as a string, see the %EXTERNAL function.
Caché SQL supports eight list functions: $LIST, $LISTBUILD, $LISTDATA, $LISTFIND, $LISTFROMSTRING, $LISTGET, $LISTLENGTH, and $LISTTOSTRING. Caché ObjectScript supports three additional list functions: $LISTVALID to determine if an expression is a list, $LISTSAME to compare two lists, and $LISTNEXT to sequentially retrieve elements from a list.
Stream Data Types
The Stream data types correspond to the Caché class property data types %Stream.GlobalCharacter (for CLOBs) and %Stream.GlobalBinary (for BLOBs).
A field with a Stream data type cannot be used as an argument to most SQL scalar, aggregate, or unary functions. Attempting to do so generates an SQLCODE -37 error code. The few functions that are exceptions are listed in the Storing and Using Stream Data (BLOBs and CLOBs) chapter of Using Caché SQL.
A field with a Stream data type cannot be used as an argument to most SQL predicate conditions. Attempting to do so generates an SQLCODE -313 error code. The predicates that accept a stream field are listed in the Storing and Using Stream Data (BLOBs and CLOBs) chapter of Using Caché SQL.
The use of Stream data types in indices, and when performing inserts and updates are also restricted. For further details on Stream restrictions, refer to the Storing and Using Stream Data (BLOBs and CLOBs) chapter of Using Caché SQL.
SERIAL Data Type
A field with a SERIAL data type can take a user-specified positive integer value, or Caché can assign it a sequential positive integer value.
An INSERT operation specifies one of the following values for a SERIAL field:
Thus a SERIAL field contains a series incremental integer values. These values are not necessarily continuous or unique. For example, the following is a valid series of values for a SERIAL field: 1, 2, 3, 17, 18, 25, 25, 26, 27. Sequential integers are either Caché-generated or user-supplied; nonsequential integers are user-supplied. If you wish SERIAL field values to be unique, you must apply a UNIQUE constraint on the field.
An UPDATE operation can only change a serial field value if the field currently has no value (NULL), or its value is 0. Otherwise, an SQLCODE -105 error is generated.
Caché imposes no restriction on the number of SERIAL fields in a table.
ROWVERSION Data Type
The ROWVERSION data type defines a read-only field that contains a unique system-assigned positive integer, beginning with 1. Caché assigns sequential integers as part of each insert, update, or %Save operation. These values are not user-modifiable.
Caché maintains a single row version counter namespace-wide. All tables in a namespace that contain a ROWVERSION field share the same row version counter. Thus, the ROWVERSION field provides row-level version control, allowing you to determine the order in which changes were made to rows in one or more tables in a namespace.
You can only specify one field of ROWVERSION data type per table.
The ROWVERSION field should not be included in a unique key or primary key. The ROWVERSION field cannot be part of an IDKey index.
For details on using ROWVERSION, refer to RowVersion Field section of the “Defining Tables” chapter of Using Caché SQL.
ROWVERSION and %Counter
Both ROWVERSION and %Counter (%Library.Counter) assign a sequential integer to a field as part of an INSERT operation. But these two counters are significantly different and are used for different purposes:
DDL Data Types Exposed by Caché ODBC / JDBC
Caché ODBC exposes a subset of the DDL data types, and maps other data types to this subset of data types. These mappings are not reversible. For example, the statement CREATE TABLE mytable (f1 BINARY) creates a Caché class that is projected to ODBC as mytable (f1 VARBINARY). A Caché list data type is projected to ODBC as a VARCHAR string.
ODBC exposes the following data types: BIGINT, BIT, DATE, DOUBLE, GUID, INTEGER, LONGVARBINARY, LONGVARCHAR, NUMERIC, OREF, SMALLINT, TIME, TIMESTAMP, TINYINT, VARBINARY, VARCHAR.
When one of these ODBC/JDBC data type values is mapped to Caché SQL, the following operations occur: DOUBLE data is cast using $DOUBLE. NUMERIC data is cast using $DECIMAL.
The GUID data type corresponds to Caché SQL UNIQUEIDENTIFIER data type. Failing to specify a valid value to a GUID / UNIQUEIDENTIFIER field generates a #7212 General Error.
Query Metadata Returns Data Type
You can use Dynamic SQL to return metadata about a query, including the data type of a specified column in the query.
The following Dynamic SQL examples return the column name and the integer code for the ODBC data type for each of the columns in Sample.Person and Sample.Employee:
  SET myquery="SELECT * FROM Sample.Person"
  SET rset = ##class(%SQL.Statement).%New()
  SET tStatus = rset.%Prepare(myquery)
    SET x=rset.%Metadata.columns.Count()
  WHILE x>0 {
    SET column=rset.%Metadata.columns.GetAt(x)
  WRITE !,x," ",column.colName," ",column.ODBCType
  SET x=x-1 }
  WRITE !,"end of columns"
 
  SET myquery="SELECT * FROM Sample.Employee"
  SET rset = ##class(%SQL.Statement).%New()
  SET tStatus = rset.%Prepare(myquery)
    SET x=rset.%Metadata.columns.Count()
  WHILE x>0 {
    SET column=rset.%Metadata.columns.GetAt(x)
  WRITE !,x," ",column.colName," ",column.ODBCType
  SET x=x-1 }
  WRITE !,"end of columns"
 
List structured data, such as the FavoriteColors column in Sample.Person, returns a data type of 12 (VARCHAR) because ODBC represents a Caché %List data type value as a string of comma-separated values.
Steams data, such as the Notes and Picture columns in Sample.Employee, return the data types -1 (LONGVARCHAR) or -4 (LONGVARBINARY).
A ROWVERSION field returns data type -5 because %Library.RowVersion is a subclass of %Library.BigInt.
For further details, refer to the Dynamic SQL chapter of Using Caché SQL and the %SQL.Statement class in the InterSystems Class Reference.
Integer Codes for Data Types
In query metadata and other contexts, the defined data type for a column may be returned as an integer code. There are two sets of integer codes used to represent data types:
For further details, refer to the Dynamic SQL chapter of Using Caché SQL.
Creating User-Defined DDL Data Types
You can modify the set of data types either by overriding the data type mapping for a system data type parameter value, or by defining a new user data type. You can modify system data types to override the InterSystems default mappings. You can create user-defined data types to provide additional data type mappings that InterSystems does not supply.
To view and modify or add to the current user data type mappings, Go to the Management Portal, select [Home] > [Configuration] > [User-defined DDL Mappings]. To add a user data type, select Create New User-defined DDL Mapping. In the displayed box, input a Name, for example VARCHAR(100) and a Datatype, for example MyString100(MAXLEN=100).
The result will be an entry in the list of user-defined DDL data types.
As shown in previous examples, there are several useful routines for entering user-defined DDL data types:
If you need to map a DDL data type to a Caché property with a collection type of Stream, specify %Stream.GlobalCharacter for Character Stream data and %Stream.GlobalBinary for Binary Stream data. (Prior to version 2011.1, character stream data was assigned the CStream%String (or %Library.GlobalCharacterStream) data type, and binary stream data was assigned the BStream%String (or %Library.GlobalBinaryStream) data type. These older data types continue to be fully supported.)
Pass-through if No DDL Mapping is Found
If DDL encounters a data type not in the DDL data type column of the UserDataTypes table, it next examines the SystemDataTypes table. If no mapping appears for the data type in either table, no conversion of the data type occurs, and the data type passes directly to the class definition as specified in DDL.
For example, the following field definitions could appears in a DDL statement:
     CREATE TABLE TestTable (
          Field1 %String,
          Field2 %String(MAXLEN=45)
          )
Given the above definitions, if DDL finds no mappings for %String or %String(MAXLEN=%1) or %String(MAXLEN=45) in UserDataTypes or SystemDataTypes, then the %String and %String(MAXLEN=45) types are passed directly to the appropriate class definition.
Converting Data Types
To convert data from one data type to another, use the CAST or CONVERT function.
CAST supports conversion to several character string and numeric data types, as well as to DATE, TIME, and TIMESTAMP.
CONVERT has two syntactical forms. Both forms support conversion to and from DATE, TIME, and TIMESTAMP data types, as well as conversion between other data types.
CAST and CONVERT Handling of VARCHAR
The VARCHAR data type (with no specified size) is mapped to a MAXLEN of 1 character, as shown in the above table. However, when you CAST or CONVERT a value to VARCHAR, the default size mapping is 30 characters. This default size of 30 characters is provided for compatibility with non-Caché software requirements.
See Also