Working with Cast and Convert Sql-server function

vishalneeraj-24503
Posted by in Sql Server category on for Intermediate level | Points: 250 | Views : 4852 red flag

In this article we will learn about Working with Cast and Convert Sql-server function.

Introduction

In this article, we will see how to use Cast and Convert in-built Sql Server function. The Cast and Convert functions provide similar functionality.They are used to convert a value from one data type to another.


They explicitly converts an expression of one data type to another. Cast and Convert provide similar functionality.

Syntax

Using CAST:

CAST(expression AS data_type)

Using CONVERT:

CONVERT(data_type[length],expression[,style])

Where,
Express :- Is any expression.
Data_Type :- Is any Sql Server Data Type
Length :- Ca be any length to store value.

Objective

Working with Cast and Convert Sql-Server function.

Using the code


We can understand the Working of Cast and Convert function by an Example:-



Output:


Cast and Convert can also be used in conjunction with each other to achieve certain effects.

For instance,a typical way to produce a char variable with the current date would be to use:

Output:

We can also use Cast and Convert as

Above both query will give us the same output as:-


Cast and Convert can also be used to Format Date-Time as shown below:-

Output:


One more example of Date and Time as

Output:

We can also use Cast and Convert function as String to Numeric Conversion as:

Output:

Conclusion

So,today,we learned about Cast and Convert function and their Usage.

Reference

http://technet.microsoft.com/en-us/library/aa226054%28v=sql.80%29.aspx

Page copy protected against web site content infringement by Copyscape

About the Author

vishalneeraj-24503
Full Name: vishal kumar
Member Level: Platinum
Member Status: Member,MVP
Member Since: 11/5/2013 5:58:17 AM
Country: India

http://www.dotnetfunda.com

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)