home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / database / sybase / 718 < prev    next >
Encoding:
Internet Message Format  |  1993-01-21  |  1.9 KB

  1. Path: sparky!uunet!elroy.jpl.nasa.gov!usc!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!ucbvax!mtxinu!sybase!robert
  2. From: robert@sybase.com (Robert Garvey)
  3. Newsgroups: comp.databases.sybase
  4. Subject: Re: Right Justify Char strings
  5. Keywords: right justification, char data, functions
  6. Message-ID: <28249@sybase.sybase.com>
  7. Date: 19 Jan 93 23:19:26 GMT
  8. References: <Dave_Wetzel-140193110749@dwetzel.mis.stratus.com>
  9. Sender: news@Sybase.COM
  10. Organization: The Upstate Sadistics
  11. Lines: 41
  12.  
  13. In article <Dave_Wetzel-140193110749@dwetzel.mis.stratus.com>, Dave_Wetzel@vos.stratus.com (David Wetzel) writes:
  14. |> Okay, it may be brain-damage, but can anyone tell me how to right justify a
  15. |> character string with Sybase (4.8).  There's got to be an easy way,
  16. |> right??
  17.  
  18. There is a way.  Its ease is a matter of opinion.  Using the functions
  19. that are part of Transact-SQL will allow you to do it, like this:
  20.  
  21. 1> SELECT
  22. 2>  col1 = CONVERT( varchar(8),
  23. 3>       REPLICATE(" ", COL_LENGTH("test", "col1") - DATALENGTH(col1))
  24. 4>       + col1 ),
  25. 5>  col2 = convert( varchar(8),
  26. 6>       REPLICATE(" ", COL_LENGTH("test", "col2") - DATALENGTH(RTRIM(col2)))
  27. 7>       + rtrim(col2) )
  28. 8> FROM test
  29. 9> go
  30.  
  31.  col1     col2     
  32.  -------- -------- 
  33.         1        1 
  34.        22       22 
  35.       333      333 
  36.      4444     4444 
  37.     55555    55555 
  38.    666666   666666 
  39.   7777777  7777777 
  40.  88888888 88888888 
  41.  
  42. (8 rows affected)
  43.  
  44. Both col1 and col2 are eight character columns.  The col1 column is
  45. varchar(8).  The rtrim string function is needed for col2 because it is
  46. char(8), so every value is the full eight characters.  There are
  47. probably other ways to accomplish this same result.
  48.  
  49. -- 
  50. Robert Garvey robert@sybase.com {sun,lll-tis,pyramid,pacbell}!sybase!robert
  51.           Sybase, Inc   6475 Christie Ave   Emeryville, CA   94608-1010
  52.  
  53. If Sybase were to pay employees for their opinions, they couldn't afford me.
  54.