Results 1 to 5 of 5

Thread: writing SQL statement

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2006
    Posts
    977

    writing SQL statement

    Hi
    I have an SQL table that contains a Flag field type int(allow null) and many other fields

    Field Type
    Flag int (allow null)
    ....

    If I don't assign a value to Flag it appears<NULL>
    I want to write an SQL statement that returns to me all records having an int value in the Flag
    For example
    ArticleID Title Category Flag
    12 hi Economics <NULL>
    13 H R U LAW 1
    14 GUG LAW 5
    15 HUN POL <NULL>

    So I want the SQL statement to return only the 2records having ArticleID=13 and 15 because the other 2 records are not assigned a value in the Flag field

    Thanks

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2006
    Posts
    977

    Re: writing SQL statement

    So I want the SQL statement to return only the 2records having ArticleID=13 and 15 because the other 2 records are not assigned a value in the Flag field
    Sorry I want to correct my question:
    I want to display the records having ArticleID=13 and ArticleID=14

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,651

    Re: writing SQL statement

    SELECT fields FROM tablename WHERE `int` <> NULL


    (Or it might be IS NOT NULL. Not sure)

  4. #4
    New Member
    Join Date
    Apr 2005
    Posts
    9

    Re: writing SQL statement

    SELECT * FROM [TABLE NAME] WHERE ArticleID=13 OR ArticleID=14

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    17,882

    Re: writing SQL statement

    If this is MS SQL Server then whenever you check the FLAG field for a value in a where clause wrap it in the ISNULL function

    Code:
    Where IsNull(Flag,0)<>0
    This function takes a column (the first parameter) and if it is null replaces it's value with the second column (in this case I specified 0).

    Fields that can contain a NULL cannot be easily checked in a WHERE clause, due to the way NULL is never "equal" to anything, thus is "not equal" to everything (or something like that - it's a bit early here!).

    Some people say to use the COALESCE() function instead of ISNULL() in order to remain ANSI-standard, but I prefer ISNULL myself.

    Why are you allowing NULL values in this column? Why not make the value 0 anyway?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width