SQL Server Performance

Issue in cast operator to varchar

Discussion in 'SQL Server 2005 General Developer Questions' started by sqldev, Aug 2, 2007.

  1. sqldev New Member

    Hi,
    I am facing a problem in using convert/cast functions. If I simply give varchar as my datatype in the function, it returns only the first 30 characters. When I use varchar with the predefined length, then it gives the entire content. Please tell me, why it happens like this and can we configure this somewhere?
    select convert(varchar, '123456789012345678901234567890123456789012345678901234567890')
    select cast('123456789012345678901234567890123456789012345678901234567890' as varchar
    select convert(varchar(500), '123456789012345678901234567890123456789012345678901234567890')
    select cast('123456789012345678901234567890123456789012345678901234567890' as varchar(500))
    Regards, Deva
  2. Adriaan New Member

    This is standard behaviour when you do not supply the maximum length for the variable - VARCHAR equals VARCHAR(30).
    In SQL 2005, use VARCHAR(MAX) if the string can be anything over 8000 characters.
  3. sqldev New Member

    Thank you Adriaan, it is helpful.
    Regards,
    Deva
  4. Madhivanan Moderator

    IMO, it should throwout the error saying "Length is not specified" :)
  5. FrankKalis Moderator

    Yes, it should throw an error.
    Btw, try that with CHAR. CHAR itself is a shortcut for CHAR(1).
  6. Madhivanan Moderator

    [quote user="FrankKalis"]
    Yes, it should throw an error.
    Btw, try that with CHAR. CHAR itself is a shortcut for CHAR(1).
    [/quote]
    [:)]
    It wont throw the error
    I edit my comment as I wish It should throw an error....................
    select cast('this .............is ...........long .............text' as varchar)
    select cast('this .............is ...........long .............text' as char)

Share This Page