Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Format string in a query

Posted on 2008-10-13
8
Medium Priority
?
185 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
Industry Leaders: 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 500 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

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…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Loops Section Overview
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

824 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