Solved

Format string in a query

Posted on 2008-10-13
8
174 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 16

Expert Comment

by:brad2575
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:Nikki28838
Comment Utility
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
Comment Utility
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
Comment Utility
sorry I left off a ")"

try this:

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

Author Closing Comment

by:Nikki28838
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 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

16 Experts available now in Live!

Get 1:1 Help Now