Clonew
Clonew

Reputation: 5

Save SQL query results to a table

I want to save my adjusted query results into a table. For example I have codes 350.8, 351.94 and I have T-SQL code to remove the decimal points resulting in the adjusted results 350,351 etc. I want to save the results into a table, not Excel. Is this possible?

I see you can create a new table but with the same columns, not the new adjusted results. The below doesn't work as SQL Server doesn't recognise adjusted1, 2 and 3.

CREATE TABLE DiagAdj 
(
     encounter_id NUMERIC,
     di_1 INT, 
     di_2 INT, 
     di_3 INT,
     adjusted1 INT,
     adjusted2 INT,
     adjusted3 INT,
);

INSERT INTO DiagAdj (encounter_id, adjusted1, adjusted2, adjusted3)
    SELECT encounter_id, adjusted1, adjusted2, adjusted3
    FROM dbo.Encounters

Decimal places removed. I want to save down adjusted3 results into a table

SELECT 
    encounter_id, di_3, -- now, try to cast to int the remainder, ending right before the decimal
    adjusted3 = TRY_CONVERT(int,LEFT(di_3, COALESCE(NULLIF(CHARINDEX('.', di_3) - 1, -1), 255)))
FROM 
    dbo.Encounters;

Upvotes: 0

Views: 821

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520918

Why don't you just cast each decimal column to integer:

INSERT INTO DiagAdj (encounter_id, adjusted1, adjusted2, adjusted3)
SELECT
    encounter_id,
    CAST(diag1 AS DECIMAL(10,0)),
    CAST(diag2 AS DECIMAL(10,0)),
    CAST(diag3 AS DECIMAL(10,0))
FROM dbo.Encounters;

Upvotes: 1

Related Questions