Sql-server – Concat digits into xxx-xxx form

concatsql serversubstring

Example, I get data from eclipse and insert into 'abc' table.

so, now how I can get the data from the 'abc' table and concat the digit?

Example: I enter 123456 then in SQL need to concat into format 123-456

I created procedure and alter procedure.
I can insert but I can't select and concat and insert.
I want it to be in a column.

this is create procedure

USE abc
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE TEST

    @cd VARCHAR(14)

AS
BEGIN
    SET NOCOUNT ON;

    INSERT INTO abc (a) VALUES (@cd)

FROM abc

END
GO

here is the alter procedure

USE [abc]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[abc]

    @cd VARCHAR(14)

AS
BEGIN

    SET NOCOUNT ON;

    INSERT INTO abc(a) VALUES (@cd)
     Select CONCAT(SUBSTRING(abc, 1,3), '-',
    SUBSTRING(abc,4,3)
from abc

END
GO

Best Answer

I'm a little confused about what you're really trying to do.

Your INSERT procedure has a rogue FROM abc in it which would error. Your INSERT statement should simply be:

INSERT INTO abc (a) VALUES (@cd);

Now, if you're wanting to change the data that's already the table, use UPDATE.

 UPDATE abc SET a = CONCAT(SUBSTRING(a, 1,3), '-', SUBSTRING(a,4,3));

Or if you're wanting to insert a new row that's concatenated, use the variable:

INSERT INTO abc (a) VALUES (CONCAT(SUBSTRING(@cd, 1,3), '-', SUBSTRING(@cd,4,3)));

or

INSERT INTO abc (a)
CONCAT(SUBSTRING(a, 1,3), '-', SUBSTRING(a,4,3));