what is the difference between CAST and CONVERT Function?

  • what is the difference between CAST and CONVERT Function?

    which one is preferred?

  • Use the conversion functions, CAST and CONVERT, to convert expressions of one data type to another data type when these conversions are not performed automatically by Microsoft® SQL Server™ 2000. These conversion functions are also used to obtain a variety of special data formats. Either of the conversion functions can be used in the select list, in the WHERE clause, and anywhere an expression is allowed.

    Use CAST rather than CONVERT if you want Transact-SQL program code to comply with SQL-92. Use CONVERT rather than CAST to take advantage of the style functionality in CONVERT.

  • Apart from the datetime formatting capabilities of CONVERT, CAST and CONVERT do identical jobs.

    Personally I prefer to use CAST in all situations except where I want to format datetime to a specified character string. I then treat CONVERT as a datetime-specific routine. This is just because I prefer to use ANSI standard code where I have a choice - other people may prefer to use CONVERT in all cases.

    Original author: https://github.com/SQL-FineBuild/Common/wiki/ 1-click install and best practice configuration of SQL Server 2019, 2017 2016, 2014, 2012, 2008 R2, 2008 and 2005.

    When I give food to the poor they call me a saint. When I ask why they are poor they call me a communist - Archbishop Hélder Câmara

  • EdVassie (4/22/2008)


    Apart from the datetime formatting capabilities of CONVERT, CAST and CONVERT do identical jobs.

    Not quite true.

    Conversions of float, real, money, or smallmoney to character data types also use styles.

    Check BOL fo details.

    _____________
    Code for TallyGenerator

  • vyas (4/21/2008)


    Use the conversion functions, CAST and CONVERT, to convert expressions of one data type to another data type when these conversions are not performed automatically by Microsoft® SQL Server™ 2000. These conversion functions are also used to obtain a variety of special data formats. Either of the conversion functions can be used in the select list, in the WHERE clause, and anywhere an expression is allowed.

    Use CAST rather than CONVERT if you want Transact-SQL program code to comply with SQL-92. Use CONVERT rather than CAST to take advantage of the style functionality in CONVERT.

    ANSI 92 is only good if software vendors follow it. Try CAST from a VARCHAR to a DATE/TIME in Oracle and see what happens....

    People spend way too much time following a standard that isn't followed by the people writing the RDBMS's. Portability is a myth.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Just been testing some data and found this difference between cast and convert.

    When you cast a number to real of size -1.18E-39 you expect an conversion error, so removing a zero should be OK. You have to remove zero's until -1.18E-36 before the error is not thrown.

    Using convert you can use up to -1.18E-38 before the number is changed into a 0 (zero)

    select cast(-0.00000000000000000000000000000000000000118 as real) as [cast result]

    select convert(real, '-0.00000000000000000000000000000000000000118') as [convert result]

    (take away zero's from cast until you get a result)

    Does anyone knows why this is?

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply