1

I have a code like the following:

INSERT INTO [TranslateValidate]..[Policy] ([BirthDate],[FirstName],[LastName])
  SELECT
    [Table2309].[DOB],
    SUBSTRING(Full_Name, CHARINDEX(',', Full_Name) + 2, LEN(Full_Name)),
    SUBSTRING(Full_Name, 0, CHARINDEX(',', Full_Name))  
  FROM [Table2309] AS [Table2309]
  WHERE [Table2309].[clientid] = (SELECT
    MIN(clientid)
  FROM Table2309 T
  WHERE T.Date_of_Birth = Table2309.Date_of_Birth
  AND T.Substring(Full_Name, CHARINDEX(',', Full_Name) + 2, LEN(Full_Name)) = Table2309.Substring(Full_Name, CHARINDEX(',', Full_Name) + 2, LEN(Full_Name))
  AND T.Substring(Full_Name, 0, CHARINDEX(',', Full_Name)) = Table2309.Substring(Full_Name, 0, CHARINDEX(',', Full_Name))

This would have an error message

Cannot find either column "c" or the user-defined function or aggregate "c.Substring", or the name is ambiguous.

If I add [] for Substring part, this would should error message

Invalid column name 'Substring(Full_Name,...

How do I resolve this problem?

3
  • 1
    That statement isn't valid. To start with you are missing the table in your INSERT clause, you have a trailing comma (,) after your 3rd column expression (and there is at least one more issue) Commented Apr 29, 2022 at 16:00
  • Also, rather than T.Substring(... try substring(T.FullName ... Commented Apr 29, 2022 at 16:01
  • As for the problem, functions are used like you have in the SELECT; they aren't used like methods in C#. I'm not sure why you have mixed up both syntax formats, when you've initially used SUBSTRING correctly. Commented Apr 29, 2022 at 16:01

1 Answer 1

1

You have an extra comma at the end of your select statement:

SELECT
    [Table2309].[DOB],
    SUBSTRING(Full_Name, CHARINDEX(',', Full_Name) + 2, LEN(Full_Name)),
    SUBSTRING(Full_Name, 0, CHARINDEX(',', Full_Name))**,**

Remove this comma and it should work fine.

Sign up to request clarification or add additional context in comments.

1 Comment

That is part of the problem but definitely not all of the issues. The insert statement doesn't have a table it is inserting into, just a list of columns.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.