473,383 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Update a table based on field length

Hello all,

I have a table with reference numbers along with a number of other fields.

The ref numbers start with B and vary in length, 4 - 6 characters, but need to be 6 characters long.

If the ref is 4 characters in length, I need to add 00 after the B. This works fine
Expand|Select|Wrap|Line Numbers
  1.  SET ref = left(ref,1) & "00" & right(ref,3)
If the ref is 5 characters in length, I need to add 0 after the B. This works fine
Expand|Select|Wrap|Line Numbers
  1. SET ref = left(ref,1) & "0" & right(ref,4)
The problem I have is updating the whole table depending on whether the ref is 4 or 5 characters long.

I have tried the following SQL and countless other variations but keep getting a syntax error.

Expand|Select|Wrap|Line Numbers
  1. Update Test
  2. iif(len([ref])<5,SET ref = left(ref,1) & "00" & right(ref,3),iif(len([ref])<6,SET ref = left(ref,1) & "0" & right(ref,4),[ref]))
  3.  
Anyone have any ideas?

Thanks in advance

Gareth
May 4 '14 #1

✓ answered by NeoPa

The following should work fully flexibly :
Expand|Select|Wrap|Line Numbers
  1. SET [Ref]=Format(Val(Mid([Ref],2,5)),'B00000')
It will handle the numeric text as a number and format it the way that you stipulate.

4 1315
Rabbit
12,516 Expert Mod 8TB
The set ref = needs to be outside the iif function.

Another way of doing it would be to prepend a bunch of 0s and take the right 6.
May 4 '14 #2
NeoPa
32,556 Expert Mod 16PB
The following should work fully flexibly :
Expand|Select|Wrap|Line Numbers
  1. SET [Ref]=Format(Val(Mid([Ref],2,5)),'B00000')
It will handle the numeric text as a number and format it the way that you stipulate.
May 4 '14 #3
Thanks both for replying. I tried the one from NeoPa and it worked perfectly :) I had tried the format method but couldn't get the SQL right.

Thanks again.
May 5 '14 #4
NeoPa
32,556 Expert Mod 16PB
There are other approaches to this, of course, but that seemed the most elegant and straightforward once the requirement is fully understood. Thankfully that was clear from your OP, which is why we could help so quickly.

Your mission, should you choose to accept it, is to look at it and see why it makes sense ;-)
May 5 '14 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Perre Van Wilrijk | last post by:
Hello, I have 2 ways of updating data I'm using often 1) via a cursor on TABLE1 update fields in TABLE2 2) via an some of variables ... SELECT @var1=FLD1, @var2=FLD2 FROM TABLE1 WHERE...
5
by: Mike | last post by:
Here is my situation; I have two tables in a MS-SQL DB. One table with dollar amounts and service codes. I have a second table that I want to move some information into from the first table. The...
2
by: Mike Leahy | last post by:
Hello all, This question is related to updating tables - is there any way to calculate or update the values in a column in a table to the values in a field produced by a query result? An...
1
by: Nothing | last post by:
I have a field, Y, in table A that I need to update from table B, matching on comman field X in both tables. Table A has 10K+ records with field X in it (Field X multipul times). Table B has...
0
by: Dan | last post by:
I have an access database that contains a linked dbf table that is based on a shapefile in ARC-Editor. I would like to auto-update a table based on the new records that have been entered into GIS....
3
by: turtle | last post by:
I have Two tables (Table1 and Table2). Both tables have a common field called part number. Table 1 contains an extra field that i would like to update table 2 to match if the part number matches....
4
by: cacanene | last post by:
My question is what will be the fast algorithm to add records in a table based on the value of a field of other table. For example: TABLE2 contains two fields ID and DESCRIPTION TABLE1 contains...
1
by: suchismitaswain | last post by:
Hi, I have a field in jsp page that has some textboxes. but the texboxes are dynamically created depending on the no of rows in a table. When i click the update button in my jsp page it has to get...
1
by: Dauntless P | last post by:
I am very new to VBA. I need to be able to write a function that relates two tables based on a UI. The relationship between the two tables is a many to many relationship, so I have a 3rd table with...
8
stonward
by: stonward | last post by:
Greetings All. I am trying to find the syntax for an SQL Update Table instruction. They're quite straightforward it seems until you try to use variables as criteria. So, I want to update the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.