Solved

Format string in a query

Posted on 2008-10-13
8
178 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
  • 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

786 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