Welcome -
Guest!
Login
/
Register Now
|
Search:
Home
Articles
Forum
Interview FAQ
Activities
News
Videos
Poll
Links
People
Groups
SQL Server Articles
Submit Article
Home
»
Articles
»
SQL Server
»
System Functions
RSS Feeds
CAST Function
Posted By:
Sarita Patel
Category:
SQL Server
Views:
22034
This article explains about CAST function in sql server with examples.
CAST function is used to explicitly convert an expression of one data type to another.
Syntax of CAST Function :
CAST (
expression AS data_type
)
expression
is any valid sql server expression.
data_type
is sql server data type to convert expression into that data type.
Examples of CAST Function :
Example 1 : Use of CAST function in select clause
SELECT
CAST ( 10.20 AS INT )
Output
10
Above example converts 10.20 float value into an integer value and returns 10.
Example 2 : Use of CAST function to convert table column value in select clause
SELECT ProductName, UnitPrice,
CAST(UnitPrice AS INT) AS AroundPrice
FROM Products
Output
ProductName UnitPrice
AroundPrice
Queso Manchego La Pastora 38.00
38
Konbu 6.00
6
Tofu 23.25
23
Genen Shouyu 15.50
16
Pavlova 17.45
17
Alice Mutton 39.00
39
Carnarvon Tigers 62.50
63
Teatime Chocolate Biscuits 9.20
9
Above example returns converted unitprice in an integer value named as AroundPrice of table products.
Example 3 : Use of CAST function in where clause
SELECT ProductName, UnitPrice
FROM Products
WHERE
CAST(UnitPrice AS INT) = 10
Output
ProductName UnitPrice
Aniseed Syrup 10.00
Sir Rodney's Scones 10.00
Jack's New England Clam Chowder 9.65
Rogede sild 9.50
Zaanse koeken 9.50
Longlife Tofu 10.00
Above example returns all products whose unit price is approximately or equal to $10.
Share:
Previous Post:
APP_NAME Function
Next Post:
CONVERT Function
Didn't find what you were looking for? Find more on
CAST Function
Or get
search suggestion and latest updates
.
Sarita Patel
author of CAST Function is from
United States
.
View All Articles
Related Articles and Code:
Program that demonstrates type casting
Conditional operator and type cast operator
Program that describes the use of IF CONDITION and PRINT the cast category type
PROGRAM SHOWING THE USE OF A CAST
Program that shows how to perform explicit cast of a variable
Program to read the ballots and count the votes cast for each candidate using an array variable count
Example 4 of using function and local variables in functions
Program of nesting member functions, private member functions and array of objects
Program to read numbers from two files using fscanf function and write it in another file using fprintf function in ascending order
Program which creates an Array of character. Make one function with one argument as a character and that function throw a user defined exception
Write a function to perform string operation i.e. split, join, length, substring using pointer without using any string function
FUNCTION XSTRCHR() WHICH SCANS A STRING FROM START TO END IN SEARCH OF A CHARACTER
Example 5 of using function
Example 6 of using function
Example 7 of using function
Example 8 of using functions
Example of static variable in function
Program that illustrates the use of malloc and calloc functions
Example of recursive function
Program of passing character array to function
Other Interesting Articles in SQL Server:
SUBSTRING Function
Text and Image Functions
>= ( Greater Than or Equal To ) Comparison Operator
ALL Logical Operator
DB_ID Function
UPPER Function
TEXTVALID Function
REVERSE Function
LOWER Function
@@LANGUAGE - LANGUAGE Function
VAR Function
Operators Allowed in the WHERE Clause with examples
Grouping Rows with GROUP BY clause
DIFFERENCE Function
@@PROCID - PROCID Function
ATN2 Function
Examples of % ( Modulo ) Operator
PERMISSIONS Function
<> Or != ( Not Equal To ) Operator
UNICODE Function
Please enter your Comment
*
*
Comment should be atleast 30 Characters.
Please put code inside [Code] your code [/Code].
Please login to post comment
No Comment Found, Be the First to post comment!
SQL Server
View All
Aggregate Functions
Analytical Functions
Configuration Function...
Constraints
Cursor
Cursor Functions
Data Control Language
Data Definition Lang
Data Manipulation Lang
Data Type
Database
Date Time Functions
Functions
Index
Interview FAQ
Joins
Math Functions
Meta Data Functions
Miscellaneous
Operator
Rowset Functions
Security Functions
Select Query
Sequence
Stored Procedures
String Functions
Sub Query
System Functions
System Statistical Fun...
Text and Image Functio...
Transaction Control La...
Trigger
View
Assembly Language
Artificial Intelligence
C Programming
C++ Programming
Visual C++
OOAD
Cobol
Java
Asp.net MVC
Rest and WCF Services
Entity Framework
Knockout.Js
Unix / Linux / Ubuntu
Networking
OOPs Concept
HTML
Dos
SQL
System Analysis & Design
Gadgets
Internet
CSS
Javascript
.Net Framework
Asp.net
C#
VB.Net
Python
Perl
Oracle
Software Engineering
RDBMS Terms
AJAX Framework
Design Pattern
UML
WPF
WCF
SEO
PowerShell
Visual Studio
WWF
BizTalk Server
Azure
General
Testing
Online Certifications
PHP
My SQL
LinQ
Project Management
Silverlight
XML
MS Office
Windows OS
DHTML
Sharepoint