SQL Server Tutorial
CONVERT function is used to convert the data type from one type to another.
SQL Server cannot convert the data type implicitly, need to change the data type explicitly.
CONVERT function is used to change the data type from one to another type.
Only we can change the data type to system defined type and cannot use user defined type in the type conversion.
CONVERT(datatype[(length)], expression[, format])
here expression can be column name, value or expression. length is the optional parameter which can be specified with char, varchar data types.
format parameter is optional which can be used to convert date type into string.
SELECT CONVERT(int, '20')Output:
20
here converts string '20' into integer value.
SELECT Id, Name, City, Country, 'Salary'=CONVERT(VARCHAR(10), Salary) + ' USD' FROM EmployeeOutput:
here converts Salary column value into string and concatenates ' USD' in trailing.
format parameter can be used to specify the date format to be used for string formatting.
SELECT 'date'=CONVERT(CHAR(20), GETDATE(), 0)Output:
Nov 26 2017 3:50AM
SELECT 'date'=CONVERT(CHAR(20), GETDATE(), 1)Output:
11/26/17
SELECT 'date'=CONVERT(CHAR(20), GETDATE(), 101)Output:
11/26/2017
SQL Server Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page