Pages

Thursday, 11 September 2014

ARITHEMATIC OPERATORS

ADD: +

Adds two numbers.
This addition arithmetic operator can also add a number, in days, to a date.

Syntax
expression + expression

Arguments
Expression
Is any valid Microsoft® SQL Server™ expression of any of the data types in the numeric category except the bit data type.

Result Types
Returns the data type of the argument with the higher precedence.

Examples:
declare @var1 int = 10,
@var2 tinyint = 25
select SQL_VARIANT_PROPERTY((@var1+@var2), 'BaseType') AS Return_type

Return_type
------------
int

declare @var1 float = 15.68,
@var2 tinyint = 25
select SQL_VARIANT_PROPERTY((@var1+@var2), 'BaseType') AS Return_type

Return_type
----------------
float

declare @var1 datetime = '11 Sep 2014',
@var2 tinyint = 25
select SQL_VARIANT_PROPERTY((@var1+@var2), 'BaseType') AS Return_type

Return_type
------------
datetime

declare @var1 money = 15.68,
@var2 int = 25
select (@var1+@var2) AS Value,SQL_VARIANT_PROPERTY((@var1+@var2), 'BaseType') AS Return_type

Value                 Return_type
--------------------- ---------------
40.68                 money




SUBTRACT: -
Subtracts two numbers.

This subtraction arithmetic operator can also subtract a number, in days, from a date.

Syntax
expression - expression

Arguments
expression
Is any valid Microsoft® SQL Server™ expression of any of the data types of the numeric data type category except the bit data type.

Result Types
Returns the data type of the argument with the higher precedence.



MULTIPLY: *

Multiplies two expressions (an arithmetic multiplication operator).

Syntax
expression * expression

Arguments
expression
Is any valid Microsoft SQL Server expression of any of the data types of the numeric data type category except the datetime or smalldatetime data types.

Result Types
Returns the data type of the argument with the higher precedence.


/ (DIVIDE)

Divides one number by another (an arithmetic division operator).

Syntax
dividend / divisor

Arguments
dividend
Is the numeric expression to divide. dividend can be any valid Microsoft® SQL Server expression of any of the data types of the numeric data type category except the datetime and smalldatetime data types.

divisor
Is the numeric expression to divide the dividend by. divisor can be any valid SQL Server expression of any of the data types of the numeric data type category except the datetime and smalldatetime data types.



Result Types
Returns the data type of the argument with the higher precedence.

If an integer dividend is divided by an integer divisor, the result is an integer that has any fractional part of the result truncated.

Remarks
The actual value returned by the / operator is the quotient of the first expression divided by the second expression.

Examples:
select 14/5 AS Value   

Value
-----------
2

select 14.0/5 AS Value 

Value
-------------
2.800000


% (Modulo)

Provides the remainder of one number divided by another.

Syntax
dividend % divisor

Arguments
dividend
Is the numeric expression to divide. dividend must be any valid Microsoft® SQL Server™ expression of the integer data type category. (A modulo is the integer that remains after two integers are divided.)

divisor
Is the numeric expression to divide the dividend by. divisor must be any valid SQL Server expression of any of the data types of the integer data type category.

Result Types
int

Remarks
The modulo arithmetic operator can be used in the select list of the SELECT statement with any combination of column names, numeric constants, or any valid expression of the integer data type category.

select 14%5 AS Value   

Value
-----------
4

select 14.0%5 AS Value 

Value
---------
      4.0




No comments:

Post a Comment