Welcome to plsql4all.blogspot.com SQL, MYSQL, ORACLE, TERADATA, MONGODB, MARIADB, GREENPLUM, DB2, POSTGRESQL.

Wednesday, 13 May 2015

CONVERT Function in SQL Server

When you want to convert an expression from one data type to some other data type then you can use CONVERT function.

Syntax for the CONVERT function is:-

CONVERT (data_type(length),expression,style)

where

data_type(length)     Specifies the target data type.
expression               Specifies the value to be converted.
style                        You can Specifies the output format for the date/time.


Following are some examples for the style:-

We can use GETDATE function to fetch the data as shown below:-

CONVERT(VARCHAR(19),GETDATE())      --May 13 2015 10:45 AM
CONVERT(VARCHAR(11),GETDATE(),6)    --13 May 15
CONVERT(VARCHAR(10),GETDATE(),10)   --13-05-15
CONVERT(VARCHAR(11),GETDATE(),106)  --13 May 2015
CONVERT(VARCHAR(10),GETDATE(),110)  --13-05-2015
CONVERT(VARCHAR(24),GETDATE(),113)  --13 May 2015 10:45:20:104

Like shown above, we can convert INT:-

SELECT CONVERT(int, 10.35);

Output :- 10

We can convert date:-

SELECT CONVERT(datetime, '2015-05-13');

Output :-  '2015-05-13 00:00:00.000'






No comments:

Post a Comment

Please provide your feedback in the comments section above. Please don't forget to follow.