Solved

Problem tranforming field for adding to database

Posted on 2008-06-13
2
161 Views
Last Modified: 2010-04-21
I have the following piece of code:

      INSERT INTO dbo.crmContactTypes
      (
      ContactRef, Contact_Type, Volunteer, Region, Owner
      )
      SELECT DISTINCT
                                      TOP (100) PERCENT InitialData.right('00000' + cast(Contact_Migration_Ref as varchar(5)),5), Contact_Type.CRM_Contact_Type_ID,
                                      crmContactData.Volunteer_Flag, InitialData.[PSYBT Area], Owner.CRM_User_ID
      FROM         InitialData INNER JOIN
                                      Contact_Type ON InitialData.Code = Contact_Type.Code INNER JOIN
                                      Owner ON InitialData.Owner = Owner.Owner LEFT OUTER JOIN
                                      crmContactData ON InitialData.Contact_Migration_Ref = crmContactData.ContactRef COLLATE Latin1_General_CI_AI
      WHERE     (InitialData.LstNm IS NOT NULL) AND (InitialData.Contact_Migration_Ref IS NOT NULL) AND
                                      (Contact_Type.CRM_Contact_Type_ID <> 99) OR
                                      (InitialData.Contact_Migration_Ref IS NOT NULL) AND (Contact_Type.CRM_Contact_Type_ID <> 99) AND (InitialData.Position IS NOT NULL)

but when I run it I receive the following error:

Msg 156, Level 15, State 1, Procedure BusinessContactsUpdate, Line 114
Incorrect syntax near the keyword 'right'.

Now I know where the error is located: InitialData.right('00000' + cast(Contact_Migration_Ref as varchar(5)),5) but the problem I have is that the data being selected here is being translated as an int even though it's nvarchar. The data in this field is something like 01234 or 00070 but the starting 0s are being removed.

I need to be able to ensure that the starting 0s are not being removed from the code and assumed I'd be able to put something like the above code into my statement. I've used code like this before to perform the transformation but for some reason it's not working this time around.

Any ideas appreciated.
0
Comment
Question by:Steven O'Neill
2 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 500 total points
ID: 21777103
Hello u101440,

Why do you have "InitialData." in there, Right is a keyword not a method.

      SELECT DISTINCT
                                      TOP (100) PERCENT Right('00000' + cast(Contact_Migration_Ref as varchar(5)),5), Contact_Type.CRM_Contact_Type_ID,
                                      crmContactData.Volunteer_Flag, InitialData.[PSYBT Area], Owner.CRM_User_ID

Should work.

Regards,

TimCottee
0
 
LVL 2

Author Closing Comment

by:Steven O'Neill
ID: 31466866
You know when you've been coding for a very looooong number of hours and the simpliest solutions always go by you...moved the initialdata table reference in the statement and all works. Thanx
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Change variables in SQL table 6 81
SQL Trigger or Function that updates table with old values 5 37
Query to Add Late Tolerance 10 60
Query 14 54
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

932 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

11 Experts available now in Live!

Get 1:1 Help Now