Sugerir traducción
 
Otros han sugerido:

progress indicator
No hay más sugerencias.
Evaluar y enviar comentarios
Contraer todo/Expandir todo Contraer todo
Ver contenido:  en paraleloVer contenido: en paralelo
Data Type Conversion (Database Engine)

Data types can be converted in the following scenarios:

  • When data from one object is moved to, compared with, or combined with data from another object, the data may have to be converted from the data type of one object to the data type of the other.

  • When data from a Transact-SQL result column, return code, or output parameter is moved into a program variable, the data must be converted from the SQL Server system data type to the data type of the variable.

When you convert between an application variable and a SQL Server result set column, return code, parameter, or parameter marker, the supported data type conversions are defined by the database API.

Data types can be converted either implicitly or explicitly.

Implicit conversions are not visible to the user. SQL Server automatically converts the data from one data type to another. For example, when a smallint is compared to an int, the smallint is implicitly converted to int before the comparison proceeds.

Explicit conversions use the CAST or CONVERT functions.

The CAST and CONVERT functions convert a value (a local variable, a column, or another expression) from one data type to another. For example, the following CAST function converts the numeric value of $157.27 into a character string of '157.27':

CAST ( $157.27 AS VARCHAR(10) )

Use CAST instead of CONVERT if you want Transact-SQL program code to comply with ISO. Use CONVERT instead of CAST to take advantage of the style functionality in CONVERT.

The following illustration shows all explicit and implicit data type conversions that are allowed for SQL Server system-supplied data types. These include xml, bigint, and sql_variant. There is no implicit conversion on assignment from the sql_variant data type, but there is implicit conversion to sql_variant.

Data type conversion table

Some implicit and explicit data type conversions are not supported when you are converting the data type of one SQL Server object to another. For example, an nchar value cannot be converted to an image value. An nchar can only be converted to binary by using explicit conversion, an implicit conversion to binary is not supported. However, an nchar can be explicitly or implicitly converted to nvarchar.

The following topics describe the conversion behaviors exhibited by their corresponding data types:

Converting Data Types by Using OLE Automation Stored Procedures

Because SQL Server uses Transact-SQL data types and OLE Automation uses Visual Basic data types, the OLE Automation stored procedures must convert the data that passes between them.

The following table describes SQL Server to Visual Basic data type conversions.

SQL Server data type

Visual Basic data type

char, varchar, text, nvarchar, ntext

String

decimal, numeric

String

bit

Boolean

binary, varbinary, image

One-dimensional Byte() array

int

Long

smallint

Integer

tinyint

Byte

float

Double

real

Single

money, smallmoney

Currency

datetime, smalldatetime

Date

Anything set to NULL

Variant set to Null

All single SQL Server values are converted to a single Visual Basic value with the exception of binary, varbinary, and image values. These values are converted to a one-dimensional Byte() array in Visual Basic. This array has a range of Byte(0 to length1) where length is the number of bytes in the SQL Server binary, varbinary, or image values.

These are the conversions from Visual Basic data types to SQL Server data types.

Visual Basic data type

SQL Server data type

Long, Integer, Byte, Boolean, Object

int

Double, Single

float

Currency

money

Date

datetime

String with 4000 characters or less

varchar/nvarchar

String with more than 4000 characters

text/ntext

One-dimensional Byte() array with 8000 bytes or less

varbinary

One-dimensional Byte() array with more than 8000 bytes

image

Conversiones de tipos de datos (motor de base de datos)

Los tipos de datos se pueden convertir en los siguientes casos:

  • Cuando los datos de un objeto se comparan o se combinan con los datos de otro objeto, o bien se mueven a estos, puede que sea necesario convertir los datos desde el tipo de datos de un objeto al tipo de datos del otro.

  • Cuando los datos de una columna de resultados, un código de retorno o un parámetro de salida de Transact-SQL se mueven a una variable de programa, se deben convertir del tipo de datos del sistema de SQL Server al tipo de datos de la variable.

Cuando se realiza una conversión entre una variable de aplicación y una columna de conjunto de resultados, código de retorno, parámetro o marcador de parámetro de SQL Server, la API de base de datos define cuáles son las conversiones de tipos de datos admitidas.

Los tipos de datos se pueden convertir de forma implícita o explícita.

Las conversiones implícitas no son visibles para el usuario. SQL Server convierte automáticamente los datos de un tipo de datos al otro. Por ejemplo, si se comparan datos de tipo smallint con datos de tipo int, antes de realizar la comparación los datos de tipo smallint se convierten implícitamente al tipo int.

Las conversiones explícitas utilizan las funciones CAST o CONVERT.

Las funciones CAST y CONVERT convierten un valor (una variable local, una columna u otra expresión) de un tipo de datos a otro. Por ejemplo, la siguiente función CAST convierte el valor numérico $157.27 a una cadena de caracteres '157.27':

CAST ( $157.27 AS VARCHAR(10) )

Utilice CAST en lugar de CONVERT si desea que el código de programa de Transact-SQL cumpla las normas ISO. Use CONVERT en lugar de CAST para aprovechar la funcionalidad de estilo de CONVERT.

En la ilustración siguiente se muestran todas las conversiones de tipos de datos explícitas e implícitas permitidas para los tipos de datos proporcionados por el sistema de SQL Server. Incluyen xml, bigint y sql_variant. No existe una conversión implícita en la asignación del tipo de datos sql_variant, pero sí hay una conversión implícita en sql_variant.

Tabla de conversión de tipo de datos

Algunas conversiones implícitas y explícitas de tipos de datos no se admiten cuando convierte el tipo de datos de un objeto de SQL Server a otro. Por ejemplo, un valor nchar no se puede convertir a un valor image. Un valor nchar solo se puede convertir a binary con una conversión explícita; la conversión implícita a binary no se admite. Sin embargo, un valor nchar se puede convertir implícita o explícitamente a nvarchar.

En los temas siguientes se describen los comportamientos de conversión que presentan los tipos de datos correspondientes:

Convertir tipos de datos con procedimientos almacenados de automatización OLE

SQL Server utiliza tipos de datos Transact-SQL y la automatización OLE utiliza tipos de datos Visual Basic; por tanto, los procedimientos almacenados de automatización OLE deben convertir los datos que se pasan entre ellos.

En la tabla siguiente se describen las conversiones de tipos de datos de SQL Server a Visual Basic.

Tipo de datos de SQL Server

Tipo de datos de Visual Basic

char, varchar, text, nvarchar, ntext

String

decimal, numeric

String

bit

Boolean

binary, varbinary, image

Matriz Byte() unidimensional

int

Long

smallint

Integer

tinyint

Byte

float

Double

real

Single

money, smallmoney

Currency

datetime, smalldatetime

Date

Cualquiera establecido en NULL

Variant establecido en NULL

Los valores únicos de SQL Server se convierten a un valor único de Visual Basic, con la excepción de los valores binary, varbinary y image. Estos valores se convierten a una matriz Byte() unidimensional en Visual Basic. Esta matriz incluye un intervalo de Byte(0 a length1) en que length es el número de bytes de los valores de SQL Server binary, varbinary o image.

A continuación se indican las conversiones de tipos de datos de Visual Basic a tipos de datos de SQL Server.

Tipo de datos de Visual Basic

Tipo de datos de SQL Server

Long, Integer, Byte, Boolean, Object

int

Double, Single

float

Currency

money

Date

datetime

String con 4.000 caracteres o menos

varchar/nvarchar

String con más de 4.000 caracteres

text/ntext

Matriz Byte() unidimensional con 8.000 bytes o menos

varbinary

Matriz Byte() unidimensional con más de 8.000 bytes

image

Contenido de la comunidad   ¿Qué es Community Content?
Agregar contenido nuevo RSS  Anotaciones
Processing
© 2012 Microsoft. Reservados todos los derechos. Condiciones de Uso | Marcas registradas | Declaración de privacidad
Page view tracker