Wrox Home  
Search P2P Archive for: Go

  Return to Index  

access thread: Left Pad ? Len?


Message #1 by "mike" <MIKEZCG@Y...> on Fri, 30 Aug 2002 21:33:14
I have a table AcctNum , AcctName

I would like to select the first 30 char of name add 5 spaces and concat 
to acct num.  But what if the Name is only 10 char?  is there a pad right 
in access or something or how could i sql this if no pad?

Select Left(Name, (30-Len(Name)))
From COA 

??? Any ideas Appreciated

M~
Message #2 by "Hamilton. Tom" <hamiltont@s...> on Fri, 30 Aug 2002 14:21:27 -0700
Extract the length as x, then concatenate space(x) to pad, something like
 
? "A" & space(6) & "B"  
returns
A      B
Therefore, by subtracting the actual length from the desired length you know how many spaces to pad.  I use a derivation to "pre" or
"post" pad strings with some character.  This function, called sPad follows
 
Function sPad(TextIn As String, 
                       iSetLen As Integer, 
                       sPrePost As String, _
                      Optional sPadChar As String) As String
' TextIn is your input string
' iSetLen is the target length to make the return
' sPrePost is where you want the padding to occur
' sPadChad if you want to pad with something other than a space character

Dim iLen As Integer, iPads As Integer, i As Integer
  
  If Len(sPadChar) = 0 Then sPadChar = " "  ' Default pad character is ' ' (space)
  iLen = Len(TextIn)
  iPads = iSetLen - iLen
  sPad = TextIn
  If iPads < 0 Then Exit Function           ' Already > 0 desired length
    
  For i = 1 To iPads
    If sPrePost = "Pre" Then
      sPad = sPadChar & sPad
    Else
      sPad = sPad & sPadChar
    End If
  Next i
End Function


	-----Original Message----- 
	From: mike [mailto:MIKEZCG@Y...] 
	Sent: Fri 8/30/2002 2:33 PM 
	To: Access 
	Cc: 
	Subject: [access] Left Pad ? Len?
	
	

	I have a table AcctNum , AcctName 

	I would like to select the first 30 char of name add 5 spaces and concat 
	to acct num.  But what if the Name is only 10 char?  is there a pad right 
	in access or something or how could i sql this if no pad? 

	Select Left(Name, (30-Len(Name))) 
	From COA 

	??? Any ideas Appreciated 

	M~ 
	--- 


Message #3 by "Richard Lobel" <richard@a...> on Fri, 30 Aug 2002 14:43:21 -0700
Mike,
It depends on what your desired end result is when the name is less than
30 characters long. Do you still want the name, 5 spaces and the
account? Or do you want the name area to take 30 spaces regardless? If
the former, just use Left(Name,30) because if the string is less than 30
the Left function will return the string. If the latter use
iNum = 30
Format(Left(Name,30),"!" & String(iNum,"@"))

Richard Lobel
President
NoClassroom.com
Live Software training 
Right over the Internet 
richard@n...
Tel:  (xxx) xxx-xxxx
Fax:  (xxx) xxx-xxxx

*****ORIGINAL MESSAGE*****
I would like to select the first 30 char of name add 5 spaces and concat

to acct num.  But what if the Name is only 10 char?  is there a pad
right 
in access or something or how could i sql this if no pad?


  Return to Index