Need results in a fixed field length regardless of acutal size

  • I have an issue with selecting out a query for a field where I need

    it to be 6 characters long, even if its 3 or 4 or 5.

    COALESCE (CAST(table.value AS CHAR(6)), '')

    this doesn't seem to work properly.

    (in a nutshell the values need to be padded with spaces if the

    values do not equal 6 characters)

    Any suggestions?

  • look up stuff in bol

  • thats stuff the function

  • STUFF delete's data (if thats what you were advising). Thks for the craptastic response....

  • No - he was referring more to the "...and inserts another set of characters at a specified starting point."...

    However, you may be better off using "Space()" for your padding...







    **ASCII stupid question, get a stupid ANSI !!!**

  • Along these lines...

    DECLARE @string VarChar(6)

    SET @string = 'abcd'

    SELECT 'abcd' + SPACE(6 - LEN(@string)) String, DATALENGTH(@string) BeforePadding, DATALENGTH('abcd' + SPACE(6 - LEN(@string))) AfterPadding







    **ASCII stupid question, get a stupid ANSI !!!**

  • Thanks for the reply, I can find nothing better to give me a boost in my day than have someone flame me when I offer asistance that is both free and works. Nice one!

  • Thks, I'll try fooling with spacing nested within IF statments, to determine which data rows need the padding.

  • Andrew...know how you must feel but if you incorporate it as part of the "Monday Blues" then it makes it easier to live with...

    A...maybe next time you won't be so quick to jump to conclusions...







    **ASCII stupid question, get a stupid ANSI !!!**

  • Hi,

    Use this Query,

    select 'ABC' + space(6 - len('ABC'))

     

    Regards,

    Amit Gupta,

     

  • No, but this should work a lot better

    CAST(COALESCE (table.value, '') AS CHAR(6))

    In your one, if value had been null you'd have got an empty string. SPACE will also work, as will STUFF. I tested this on sysobjects (select cast(name as CHAR(20)) FROM sysobjects) and got as list of object names padded to 20 characters

    p.s. None of us here owe you help, we're not paid for this. Sarcastic comments are unnecessary and uncalled for and will encourage people not to help you.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • And how is this different from what I posted ?!?!







    **ASCII stupid question, get a stupid ANSI !!!**

Viewing 12 posts - 1 through 11 (of 11 total)

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