SQL Server/T-SQL/Data Type/decimal

Материал из SQL эксперт
Перейти к: навигация, поиск

Average of decimal values

   <source lang="sql">

6> CREATE TABLE T ( 7> int1 int, 8> bit1 bit, 9> varchar1 varchar(3), 10> dec1 dec(5,2), 11> cmp1 AS (int1 + bit1) 12> ) 13> GO 1> 2> INSERT T (int1, bit1) VALUES (1, 0) 3> INSERT T (int1, varchar1) VALUES (2, "abc") 4> INSERT T (int1, dec1) VALUES (3, 5.25) 5> INSERT T (bit1, dec1) VALUES (1, 9.75) 6> GO (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) 1> SELECT CAST(AVG(dec1) AS dec(5,2)) "Avg of dec1" 2> FROM T 3> WHERE dec1 IS NOT NULL 4> GO Avg of dec1


      7.50

(1 rows affected) 1> drop table t; 2> GO 1> 2>

</source>
   
  


Cast decimal to varchar and append another string

   <source lang="sql">

3> CREATE TABLE employee( 4> id INTEGER NOT NULL PRIMARY KEY, 5> first_name VARCHAR(10), 6> last_name VARCHAR(10), 7> salary DECIMAL(10,2), 8> start_Date DATETIME, 9> region VARCHAR(10), 10> city VARCHAR(20), 11> managerid INTEGER 12> ); 13> GO 1> INSERT INTO employee VALUES (1, "Jason" , "Martin", 5890,"2005-03-22","North","Vancouver",3); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (2, "Alison", "Mathews",4789,"2003-07-21","South","Utown",4); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (3, "James" , "Smith", 6678,"2001-12-01","North","Paris",5); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (4, "Celia" , "Rice", 5567,"2006-03-03","South","London",6); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (5, "Robert", "Black", 4467,"2004-07-02","East","Newton",7); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (6, "Linda" , "Green" , 6456,"2002-05-19","East","Calgary",8); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (7, "David" , "Larry", 5345,"2008-03-18","West","New York",9); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (8, "James" , "Cat", 4234,"2007-07-17","West","Regina",9); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (9, "Joan" , "Act", 6123,"2001-04-16","North","Toronto",10); 2> GO (1 rows affected) 1> 2> select * from employee; 3> GO id first_name last_name salary start_Date region city managerid


---------- ---------- ------------ ----------------------- ---------- -------------------- -----------
         1 Jason      Martin          5890.00 2005-03-22 00:00:00.000 North      Vancouver                      3
         2 Alison     Mathews         4789.00 2003-07-21 00:00:00.000 South      Utown                          4
         3 James      Smith           6678.00 2001-12-01 00:00:00.000 North      Paris                          5
         4 Celia      Rice            5567.00 2006-03-03 00:00:00.000 South      London                         6
         5 Robert     Black           4467.00 2004-07-02 00:00:00.000 East       Newton                         7
         6 Linda      Green           6456.00 2002-05-19 00:00:00.000 East       Calgary                        8
         7 David      Larry           5345.00 2008-03-18 00:00:00.000 West       New York                       9
         8 James      Cat             4234.00 2007-07-17 00:00:00.000 West       Regina                         9
         9 Joan       Act             6123.00 2001-04-16 00:00:00.000 North      Toronto                       10

(9 rows affected) 1> 2> 3> SELECT ID, CAST(Salary AS varchar(10)) + " Salary" 4> FROM Employee 5> GO ID


-----------------
         1 5890.00 Salary
         2 4789.00 Salary
         3 6678.00 Salary
         4 5567.00 Salary
         5 4467.00 Salary
         6 6456.00 Salary
         7 5345.00 Salary
         8 4234.00 Salary
         9 6123.00 Salary

(9 rows affected) 1> 2> 3> 4> drop table employee; 5> GO

</source>
   
  


Cast int to decimal

   <source lang="sql">

1> create table employee( 2> ID int, 3> name nvarchar (10), 4> salary int, 5> start_date datetime, 6> city nvarchar (10), 7> region char (1)) 8> GO 1> 2> insert into employee (ID, name, salary, start_date, city, region) 3> values (1, "Jason", 40420, "02/01/94", "New York", "W") 4> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (2, "Robert",14420, "01/02/95", "Vancouver","N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (3, "Celia", 24020, "12/03/96", "Toronto", "W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (4, "Linda", 40620, "11/04/97", "New York", "N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (5, "David", 80026, "10/05/98", "Vancouver","W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (6, "James", 70060, "09/06/99", "Toronto", "N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (7, "Alison",90620, "08/07/00", "New York", "W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (8, "Chris", 26020, "07/08/01", "Vancouver","N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (9, "Mary", 60020, "06/09/02", "Toronto", "W") 3> GO (1 rows affected) 1> 2> select * from employee 3> GO ID name salary start_date city region


---------- ----------- ----------------------- ---------- ------
         1 Jason            40420 1994-02-01 00:00:00.000 New York   W
         2 Robert           14420 1995-01-02 00:00:00.000 Vancouver  N
         3 Celia            24020 1996-12-03 00:00:00.000 Toronto    W
         4 Linda            40620 1997-11-04 00:00:00.000 New York   N
         5 David            80026 1998-10-05 00:00:00.000 Vancouver  W
         6 James            70060 1999-09-06 00:00:00.000 Toronto    N
         7 Alison           90620 2000-08-07 00:00:00.000 New York   W
         8 Chris            26020 2001-07-08 00:00:00.000 Vancouver  N
         9 Mary             60020 2002-06-09 00:00:00.000 Toronto    W

(9 rows affected) 1> 2> SELECT ID, Name, 3> CAST(Salary AS dec(12,2)) "Salary in decimal" 4> FROM Employee 5> GO ID Name Salary in decimal


---------- -----------------
         1 Jason               40420.00
         2 Robert              14420.00
         3 Celia               24020.00
         4 Linda               40620.00
         5 David               80026.00
         6 James               70060.00
         7 Alison              90620.00
         8 Chris               26020.00
         9 Mary                60020.00

(9 rows affected) 1> 2> 3> 4> drop table employee 5> GO 1>

      </source>
   
  


Cast real number to decimal

   <source lang="sql">

1> create table employee( 2> ID int, 3> name nvarchar (10), 4> salary int, 5> start_date datetime, 6> city nvarchar (10), 7> region char (1)) 8> GO 1> 2> insert into employee (ID, name, salary, start_date, city, region) 3> values (1, "Jason", 40420, "02/01/94", "New York", "W") 4> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (2, "Robert",14420, "01/02/95", "Vancouver","N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (3, "Celia", 24020, "12/03/96", "Toronto", "W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (4, "Linda", 40620, "11/04/97", "New York", "N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (5, "David", 80026, "10/05/98", "Vancouver","W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (6, "James", 70060, "09/06/99", "Toronto", "N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (7, "Alison",90620, "08/07/00", "New York", "W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (8, "Chris", 26020, "07/08/01", "Vancouver","N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (9, "Mary", 60020, "06/09/02", "Toronto", "W") 3> GO (1 rows affected) 1> 2> select * from employee 3> GO ID name salary start_date city region


---------- ----------- ----------------------- ---------- ------
         1 Jason            40420 1994-02-01 00:00:00.000 New York   W
         2 Robert           14420 1995-01-02 00:00:00.000 Vancouver  N
         3 Celia            24020 1996-12-03 00:00:00.000 Toronto    W
         4 Linda            40620 1997-11-04 00:00:00.000 New York   N
         5 David            80026 1998-10-05 00:00:00.000 Vancouver  W
         6 James            70060 1999-09-06 00:00:00.000 Toronto    N
         7 Alison           90620 2000-08-07 00:00:00.000 New York   W
         8 Chris            26020 2001-07-08 00:00:00.000 Vancouver  N
         9 Mary             60020 2002-06-09 00:00:00.000 Toronto    W

(9 rows affected) 1> 2> SELECT city, CAST(SUM(Salary) AS dec(12,2)) "Total" 3> FROM Employee 4> GROUP BY City 5> GO city Total


--------------

New York 171660.00 Toronto 154100.00 Vancouver 120466.00 (3 rows affected) 1> 2> 3> drop table employee 4> GO 1>

      </source>
   
  


decimal(15,4)

   <source lang="sql">

15> 16> SET NOCOUNT ON 17> DECLARE @float1 float, @float2 float, @float3 float, @float4 float 18> DECLARE @dec1 decimal(15,4), @dec2 decimal(15,4), 19> @dec3 decimal(15,4), @dec4 decimal(15,4) 20> DECLARE @pointseven decimal(15,4) 21> SET @float1 = 7 22> SET @float2 = 100 23> SET @float3 = .0009 24> SET @float4 = @float1/@float2 - @float3 25> SET @dec1 = 7 26> SET @dec2 = 100 27> SET @dec3 = .0009 28> SET @dec4 = @dec1/@dec2 - @dec3 29> SET @pointseven = .7 30> SELECT @float4 "@float4", @float3 "@float3", 31> @pointseven - @float4 "float diff" 32> SELECT @dec4 "@dec4", @dec3 "@dec3", 33> @pointseven - @dec4 "dec diff" 34> 35> GO @float4 @float3 float diff


------------------------ ------------------------
  6.9100000000000009E-2    8.9999999999999998E-4      0.63089999999999991

@dec4 @dec3 dec diff


----------------- ------------------
           .0691             .0009              .6309

1>

      </source>
   
  


Function returning decimal

   <source lang="sql">

28> 29> CREATE TABLE project (project_no CHAR(4) NOT NULL, 30> project_name CHAR(15) NOT NULL, 31> budget FLOAT NULL) 32> 33> insert into project values ("p1", "Search Engine", 120000.00) 34> insert into project values ("p2", "Programming", 95000.00) 35> insert into project values ("p3", "SQL", 186500.00) 36> 37> select * from project 38> GO (1 rows affected) (1 rows affected) (1 rows affected) project_no project_name budget


--------------- ------------------------

p1 Search Engine 120000 p2 Programming 95000 p3 SQL 186500 (3 rows affected) 1> -- This function computes additional total costs that arise 2> -- if budgets of projects increase 3> 4> CREATE FUNCTION compute_costs (@percent INT = 10) 5> RETURNS DECIMAL(16,2) 6> BEGIN 7> DECLARE @additional_costs DEC (14,2), @sum_budget dec(16,2) 8> SELECT @sum_budget = SUM (budget) FROM project 9> SET @additional_costs = @sum_budget * @percent/100 10> RETURN @additional_costs 11> END 12> GO 1> 2> SELECT project_no, project_name FROM project WHERE budget < dbo.rupute_costs(25) 3> GO project_no project_name


---------------

p2 Programming (1 rows affected) 1> 2> drop function compute_costs 3> drop table project 4> GO 1> 2>

      </source>
   
  


Use select to do the calculation

   <source lang="sql">

12> SET NOCOUNT ON 13> DECLARE @dec1 decimal(38,0) 14> SET @dec1 = CAST(POWER(10,9) as decimal(38,0)) 15> SELECT @dec1 * @dec1 * @dec1 * @dec1 * 10 AS "Large Decimal value", 16> FLOOR(LOG10(@dec1 * @dec1 * @dec1 * @dec1 * 10) 17> + 1) AS "Number of digits" 18> GO Large Decimal value Number of digits


------------------------
 10000000000000000000000000000000000000                       38

1> 2>

      </source>