Solved

Format string in a query

Posted on 2008-10-13
8
180 Views
Last Modified: 2010-04-21
Hello all.  I am trying to write the code to append data to a table, however, there is some concatenation and formatting involved.  
My issue is with the first piece of information.  I am trying to create a field entitled "CRMACCT" which will consist of the "[SiteRevised]-[ACCTREVISED]" while padding the account revised field with leading zeros and adding a hyphon 3 characters to the right.  I am used to the format function in Access which I learning does not apply in SQL server dbs.  Any information would be greatly appreciated.  Thank you in advance!
Nikki28838    
use CRM_Comments_Migration
declare [SITEREVISED] & "-" & set([ACCTREVISED],"0000000-00") AS CRMACCT, IcomsRawData.SiteID, IcomsRawData.Account, IcomsRawData.[Customer Name], IcomsRawData.[Contact Number], IcomsRawData.[Contact Date], IcomsRawData.[Category Code], IcomsRawData.[SubCategory Code], IcomsRawData.Source, IcomsRawData.Type, IcomsRawData.[Contact Status], IcomsRawData.[Contact UserID], IcomsRawData.Notes INTO ICOMSREVISED
FROM IcomsRawData;

Open in new window

0
Comment
Question by:Nikki28838
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 16

Expert Comment

by:brad2575
ID: 22704842
If the leading character string is always the same length you can do this:

select Left('0000000' + left(ACCTREVISED, LEN(ACCTREVISED-2) + '-' + Right(ACCTREVISED, 2), 10)

This will get you your formatted string.
0
 

Author Comment

by:Nikki28838
ID: 22704868
Unless I did something wrong, which I'm certain is the case, I am getting a " The left function requires 2 argument(s)." error.
0
 
LVL 16

Expert Comment

by:brad2575
ID: 22704872
Sorry one part was wrong change it to:
select Right('0000000' + left(ACCTREVISED, LEN(ACCTREVISED-2) + '-' + Right(ACCTREVISED, 2), 10)

sorry did not explain it:

broken up in piecies
gets the right 2 characters of the string
  Right(ACCTREVISED, 2)  

Adds the "-" to the string at the 3rd character from the last
+ '-'  

gets all the characters from the string except the last 2 (that we got above so we could put the "-" before them)
left(ACCTREVISED, LEN(ACCTREVISED-2)

Then the whole thing combined has a LEFT around it and adds "0000000" to it.
This adds the 0's to the front of the string to get the leading zeros and then it does a Right to get ONLY the 10 characters of the string you want and will truncate any extra 0's that we just added.

Right('0000000' + left(ACCTREVISED, LEN(ACCTREVISED-2) + '-' + Right(ACCTREVISED, 2), 10)


If any of these are NUMBER formatted fields in the database you may have to do a CAST on them or you will get an error.

Let me know if they are and I will update the formula.


0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:brad2575
ID: 22704896
If the field can have a NULL value in it this my be causing your issues.  Let me know if it can be NULL?

0
 

Author Comment

by:Nikki28838
ID: 22704922
OK...this is what I have so far.  Now the error is "The left function requires 2 argument(s)".  

use CRM_Comments_Migration
insert into ICOMSrevised 
select Right('0000000' + left(ACCTREVISED, LEN(ACCTREVISED-2) + '-' + Right(ACCTREVISED, 2), 10)AS CRMACCT, IcomsRawData.SiteID, IcomsRawData.Account, IcomsRawData.[Customer Name], IcomsRawData.[Contact Number], IcomsRawData.[Contact Date], IcomsRawData.[Category Code], IcomsRawData.[SubCategory Code], IcomsRawData.Source, IcomsRawData.[Type], IcomsRawData.[Contact Status], IcomsRawData.[Contact UserID], IcomsRawData.Notes INTO ICOMSREVISED
FROM IcomsRawData;

Open in new window

0
 

Author Comment

by:Nikki28838
ID: 22704931
None of the fields will be null as the original table's data is all populated.  Thanks for helping me with this...I always have problems with the syntax being as I am so used to MS Access :(

 
0
 
LVL 16

Accepted Solution

by:
brad2575 earned 125 total points
ID: 22704981
sorry I left off a ")"

try this:

Right('0000000' + left(ProcessID, LEN(ProcessID-2)) + '-' + Right(ProcessID, 2), 10)
0
 

Author Closing Comment

by:Nikki28838
ID: 31505612
Worked!!!  Thank you so much!  I get so frustrated and normally try to figure things out on my own, but, there are not enough hours in the day...lol.  Thanks again for your patience and explanations...this will also make my life easier going forward!  
Nikki28838
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to count the days a record spends in a step 21 56
Trouble with <> 2 26
RAISERROR WITH NOWAIT 2 24
SQL Pivot table 2 43
Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

756 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