Solved

Remove the last zero character form a string

Posted on 2009-04-09
12
367 Views
Last Modified: 2012-05-06
Hi;
I have a string field full of data like this" 18 10 6 4 1 0"
I mean that is the last character always space then zero "0"
I need function  to remove this zero  at all
please
0
Comment
Question by:Mohammad Alsolaiman
  • 6
  • 5
12 Comments
 
LVL 5

Assisted Solution

by:brandonvmoore
brandonvmoore earned 50 total points
ID: 24113557
Use the Left() and Len() functions:

select left(fieldname, len(fieldname) - 1)
or
select left(fieldname, len(fieldname) - 2)

Use "-1" if you want to leave the space on the end or "-2" if you want to get rid of the space and the 0.
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 24113567
what if you had something ending with 10, is that a possibility?
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 24113573
if always space then 0 then brandon's post will work for you. If not always space and 0 then try this

if last two chars is space and 0 then strip it off else leave as as

mystrippedfield: iif(right$(myfield,2) = " 0",left$(myfield,len(myfield)-2),myfield)

this can be put in your sql

0
 
LVL 65

Expert Comment

by:rockiroads
ID: 24113575
is this stripping just for display purposes or do you want to update the data? what Ive posted is for select. If you want to update the data, it is like this

update mytable
set myfield = left$(myfield,len(myfield)-2)
where len(nz(myfield,""))>1 and right$(myfield,2) = " 0"

Need to actually take into account any fields that are null or have 1 char in them so added len(nz(myfield,""))>1
same would be needed in the select as well I guess


0
 

Author Comment

by:Mohammad Alsolaiman
ID: 24113600
no i want some thing fore fields i declare, but not in sql statement
0
 
LVL 65

Accepted Solution

by:
rockiroads earned 450 total points
ID: 24113609
sorry? fields you declare. Do you want vba

dim mynewstring as string

mynewstring = iif(right$(myfield,2) = " 0",left$(myfield,len(myfield)-2),myfield)

Otherwise Im not sure what you mean
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:Mohammad Alsolaiman
ID: 24113636
yes u r wright,
but there some thing else
i think the zero is in the first character,  and not as i mention before .
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 24113658
so it is zero and space
or space and zero

if zero and space

if len(myoldstring)>1 and left$(myoldstring,2) = "0 " then
    mynewstring = myoldstring
else
    mynewstring = myoldstring
end if

to replace myoldstring only

if len(myoldstring)>1 and left$(myoldstring,2) = "0 " then
    myoldstring = mid$(myoldstring,3)
end if


to replace space and zero, change "0 " to " 0"
0
 

Author Closing Comment

by:Mohammad Alsolaiman
ID: 31568826
wonderful
0
 

Author Comment

by:Mohammad Alsolaiman
ID: 24113667
thanks a lot rocki
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 24113694
No probs
in case you got null strings, just add the check for the length
0
 

Author Comment

by:Mohammad Alsolaiman
ID: 24114277
clear
thanks again
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now