What's the difference between Cast and Convert?

4

In a Transact-SQL book (Ramalho Series) it is said:

CAST AND CONVERT

  

Implicitly converts one expression from data types to another.   CAST is a synonym for CONVERT.

What do you mean by CAST is synonymous with CONVERT? What is the usage orientation of one and the other? Is there a performance implication between the two?

    
asked by anonymous 21.06.2017 / 13:58

1 answer

3

CONVERT is a specific function of SQL Server and CAST is default ANSI .

There is a conversion table that can be used to decide whether or not to use some conversion function for each case:

Followingtheinformationinthetable,ifitisnecessarytoconvertfromvarchartodatewecandoimplicitly:

DECLARE@dataDATE='2017-06-21';

Ortoconvertadatetimetotimestampyoumustdoitexplicitly:

DECLARE@dataDATETIME=getdate();DECLARE@horaTIMESTAMP=CAST(@dataASTIMESTAMP);

Asforperformance,thereisnorule.Foreachtypethereisaminimumvariationbetweenthetwouses.

References:

  • CAST and CONVERT (Transact-SQL)

21.06.2017 / 15:02
How popular is a JTable using a .txt file? Write .txt file node.js