问题
So I inserted few records in a table manually (without using Stored procedure) and rows were fine under a column "COLMN". Then I tried to automate it by using dynamic-SQL in stored procedure. Unfortunately I ended up appending rubbish records (entered float in a column which was meant to be Char). How can I delete these selected rubbish records which don't belong? Here is how my table looks like:
...And I want to delete those numeric records (from column "COLMN") I planned to achieve this by :Altering the table and adding a column for sequence, updating the sequence column, sorting it and accordingly deleting the top or bottom N rows and lastly dropping the column. Something like this:
ALTER TABLE PROD_CE_WORK_SPACE.NPVAZ_CVM_CHECK_TEST_CASE_5 ADD SEQ_NUM INT;
UPDATE PROD_CE_WORK_SPACE.NPVAZ_CVM_CHECK_TEST_CASE_5
SET SEQ_NUM = ROW_NUMBER() OVER(ORDER BY COLMN)
But we can't use analytical functions like that.
Let me know if there is an easy way to do it.
Thank you
Piyush
回答1:
The easiest way to find numeric strings is probably comparing upper and lower case of the column name, if it's numeric both will be the same:
SELECT * FROM PROD_CE_WORK_SPACE.NPVAZ_CVM_CHECK_TEST_CASE_5
WHERE LOWER(COLMN) = UPPER(COLMN) (CASESPECIFIC)
OR COLMN IS NULL
If this returns the correct rows simply change to a DELETE.
回答2:
Why don't you just try to locate the wrong rows in a WHERE condition?
COLMN
must be a string, because a good content is 'STRETCH_DOLLAR_ADJUSTED'.
From the table cutout you show above, you start with:
SELECT
*
FROM NPVAZ_CVM_CHECK_TEST_CASE_5
WHERE COLMN IS NULL
OR LEFT(COLMN,1) IN (
'-'
, '+'
, '0'
, '1'
, '2'
, '3'
, '4'
, '5'
, '6'
, '7'
, '8'
, '9'
);
Refine the WHERE condition until you get the rows you intend, and then use that WHERE condition in a DELETE statement.
Or am I getting something wrong?
Marco the Sane .
来源:https://stackoverflow.com/questions/41875937/how-to-delete-records-from-a-column-which-belong-to-a-different-data-type