CAST and CONVERT -
http://msdn.microsoft.com/en-us/library/ms191530.aspx
Data types can be converted either implicitly or explicitly:
- Implicit conversions are not visible to the user.
SQL Server automatically converts the data from one data type to another. For example, when a smallint is compared to an int, the smallint is implicitly converted to int before the comparison proceeds. - Explicit conversions use the CAST or CONVERT functions.
The CAST and CONVERT functions convert a value (a local variable, a column, or another expression) from one data type to another. For example, the following CAST function converts the numeric value of $157.27 into a character string of '157.27':
Use CAST instead of CONVERT if you want Transact-SQL program code to comply with ISO. Use CONVERT instead of CAST to take advantage of the style functionality in CONVERT.
http://www.thesmartcodes.com/how-to-convert-datatypes-in-sql/
DECLARE @firstname AS VARCHAR(30)
DECLARE @lastname AS VARCHAR(30)
DECLARE @LEVEL AS VARCHAR(30)
DECLARE @pay_amount AS DECIMAL(18, 2)
DECLARE @pay_date AS DATETIME
SET @firstname = ‘Subhash’
SET @lastname = ‘Pande’
SET @LEVEL = ‘First’
SET @pay_amount = 1009.30
SET @pay_date = GETDATE()
SELECT @firstname AS First_Name ,
@lastname AS Last_Name ,
@level AS Levels ,
( CONVERT(VARCHAR, @pay_amount) + ‘ ‘ + ‘$’ ) AS Pay_Amount ,
CONVERT(VARCHAR, @pay_date, 106) AS PayDate
No comments:
Post a Comment