SQL Server CONVERT() Function




SQL Server CONVERT() Function

The CONVERT() function is a general function that converts an expression of one data type to another.
The CONVERT() function can be used to display date/time data in different formats.

Syntax

CONVERT(data_type(length),expression,style)

Value
Description
data_type(length)
Specifies the target data type (with an optional length)
expression
Specifies the value to be converted
style
Specifies the output format for the date/time (see table below)


Example

The following script uses the CONVERT() function to display different formats. We will use the GETDATE() function to get the current date/time:

CONVERT(VARCHAR(19),GETDATE())
CONVERT(VARCHAR(10),GETDATE(),10)
CONVERT(VARCHAR(10),GETDATE(),110)
CONVERT(VARCHAR(11),GETDATE(),6)
CONVERT(VARCHAR(11),GETDATE(),106)
CONVERT(VARCHAR(24),GETDATE(),113)

The result would look something like this:
Nov 04 2014 11:45 PM
11-04-14
11-04-2014
04 Nov 14

04 Nov 2014
04 Nov 2014 11:45:34:243

Why we can't execute a stored procedure from a User Defined function(UDF)

Functions cannot "touch" any database but read them only. Stored procedures can do anything and everything with databases. You ...