0

I need a query which will retrieve non-negative Integer values only from a given table

Lets say I have a table A and the query select* from A will retrieve all contents from it. Now I want to write a query which will give me only non-negative values.

Assumption: All columns of the table contain negative/positive/zero Integer values as well as characters.

4

6 回答 6

2
Select * from A where col1 >=0 and col2 >=0 and .... and colN>=0

只需将 col1...colN 替换为您的列名。

于 2012-06-19T13:03:40.760 回答
1

我想这就是你所追求的:

SELECT FIELD1  
FROM TABLE
WHERE FIELD1 >= 0

UNION ALL

SELECT FIELD2  
FROM TABLE
WHERE FIELD2 >= 0
于 2012-06-19T13:03:32.960 回答
1

我认为这是唯一的方法:

select * from A
where col1 > 0 and col2 > 0 and col3 > 0 and col4 > 0 ... and coln > 0
于 2012-06-19T13:03:35.203 回答
0

简单地?

SELECT * 
FROM TABLE
WHERE FIELD > 0
于 2012-06-19T13:02:25.930 回答
0

select * from tablea where COLUMN > 0;

于 2012-06-19T13:03:15.253 回答
0

If you are using SQL Server...

    DECLARE @name VARCHAR(50)
    DECLARE @tableName VARCHAR(50)
    DECLARE @whereClause VARCHAR(max)

    SET @tableName = 'TableName' --you can change text to other table name
    SET @whereClause = ' WHERE -1 >= 0'

    DECLARE db_cursor CURSOR FOR  
    SELECT c.name
    FROM sys.columns AS c
    WHERE OBJECT_NAME(c.OBJECT_ID) = @tableName

    OPEN db_cursor   
    FETCH NEXT FROM db_cursor INTO @name   

    WHILE @@FETCH_STATUS = 0   
    BEGIN   
           SET @whereClause = @whereClause + ' OR '  + @name + ' >= 0'
           FETCH NEXT FROM db_cursor INTO @name   
    END   

    CLOSE db_cursor   
    DEALLOCATE db_cursor

    EXEC ('SELECT * FROM ' + @tableName + @whereClause)
于 2012-06-19T13:15:42.650 回答