Solved

Format string in a query

Posted on 2008-10-13
8
182 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
Create CentOS 7 Newton Packstack Running Keystone

A bug was filed against RDO for the installation of Keystone v3. This guide is designed to walk you through the configuration for using Keystone v3 with Packstack. You will accomplish this using various repos and the Answers file.

 
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

617 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