Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2004
    Location
    London, UK
    Posts
    71

    Unanswered: how do i query a table and if it doesn't have a value return something anyway?

    how do i query a table and if it doesn't have a value for a field return something anyway?

    e.g.

    (table 1)
    id title
    --------
    1 a
    2 b
    3 c
    4 (null)

    e.g. if null return 'not assigned'

    ?

  2. #2
    Join Date
    Jan 2004
    Location
    Aberdeen, Scotland
    Posts
    1,067
    Here is a way you can do this

    Code:
    SELECT 
    	CASE
    	WHEN Title IS NULL THEN 'Not Assigned'
    	ELSE Title
    	END AS Title
    FROM Table1

  3. #3
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    simpler way:

    select coalesce(title,'Not Assigned') as title from table1
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

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