CAST (original_expression AS desired_datatype) : Cast « Data Type « SQL Server / T-SQL

SQL Server / T-SQL
1. Aggregate Functions
2. Analytical Functions
3. Constraints
4. Cursor
5. Data Set
6. Data Type
7. Database
8. Date Timezone
9. Index
10. Insert Delete Update
11. Math Functions
12. Select Query
13. Sequence
14. Store Procedure Function
15. String Functions
16. Subquery
17. System
18. Table
19. Table Joins
20. Transact SQL
21. Transaction
22. Trigger
23. View
24. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
SQL Server / T-SQL » Data Type » Cast 
CAST (original_expression AS desired_datatype)

 


6CREATE TABLE employee
7(
8>    emp_id         varchar(20),
9>    fname          varchar(20)       NOT NULL,
10>    minit          char(1)               NULL,
11>    lname          varchar(30)       NOT NULL,
12>    job_id         smallint          NOT NULL       DEFAULT 1,
13>    job_lvl        tinyint                          DEFAULT 10,
14>    pub_id         char(4)           NOT NULL       DEFAULT ('9952'),
15>    hire_date      datetime          NOT NULL       DEFAULT (getdate())
16)
17> GO
1>
2insert employee values ('1''Jack', 'T''Lee',     2215'9952', '11/11/89')
3insert employee values ('2''Jode', 'M''Devon',   3200'9952', '07/16/91')
4insert employee values ('3''Frac', 'F''Chang',   4227'9952', '11/03/90')
5insert employee values ('4''Like', 'A''Lebihan', 5175'0736', '06/03/90')
6insert employee values ('5''Paul', 'X''Henriot', 5159'0877', '08/19/93')
7insert employee values ('6''Sick', 'K''Ottlieb', 5150'1389', '04/05/91')
8insert employee values ('7''Rita', 'B''Muller',  5198'1622', '10/09/93')
9insert employee values ('8''Mary', 'J''Pontes',  5246'1756', '03/01/89')
10insert employee values ('9''Jane', 'Y''Labrune', 5172'9901', '05/26/91')
11insert employee values ('10','Carl', 'F''Hernadez',5211'9999', '04/21/89')
12> GO

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)
1>
2>
3SELECT lname + '-' + CAST(job_id AS varchar(2)) FROM employee
4> GO

---------------------------------
Lee-2
Devon-3
Chang-4
Lebihan-5
Henriot-5
Ottlieb-5
Muller-5
Pontes-5
Labrune-5
Hernadez-5

(10 rows affected)
1>
2> drop table employee;
3> GO

 
Related examples in the same category
1. CAST(expression AS data type(<length>))
2. CAST(2.78128 AS integer)
3. CAST(2.78128 AS money)
4. CAST('11/11/72' as smalldatetime) AS '11/11/72'
5. select CAST('2002-09-30 11:35:00' AS smalldatetime) + 1 (plus)
6. select CAST('2002-09-30 11:35:00' AS smalldatetime) - 1 (minus)
7. select CAST(CAST('2002-09-30' AS datetime) - CAST('2001-12-01' AS datetime) AS int)
8. The CAST() function accepts one argument, an expression
9. Mixing Datatypes: CAST and CONVERT
10. Cast date type to char
11. CAST can still do date conversion
12. Cast date to different format
13. Cast number to varchar and use the regular expressions
14. Conversion with CONVERT function for computated value with numbers in list
15. Cast int to decimal
16. Convert varchar to number
17. CONVERT() does the same thing as the CAST() function
18. CAST(ID AS VarChar(5))
19. CAST('123.4' AS Decimal)
20. Arithmetic overflow error converting numeric to data type varchar.
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.