If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > problem with NULL values in sql statement

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-14-04, 07:17
peter1970 peter1970 is offline
Registered User
 
Join Date: Oct 2003
Location: Rotterdam
Posts: 17
problem with NULL values in sql statement

Oke, this is my sql statement i'm using in my acces project


SeLECT ID_PERS, ACHTERNAAM + ',' + VOORLETTERS + ' ' + TUSSENVOEGSEL AS NAAM FROM dbo.RAR_TBL_PERSONEN WHERE (NOT (VOORLETTERS IS NULL)) ORDER BY ACHTERNAAM + ',' + VOORLETTERS + ' ' + TUSSENVOEGSEL

problem is that the field "TUSSENVOEGSEL" does not always contain a value (so it is NULL). In this case the query gives an empty field as a result. (so not just de fields VOORNAAM and ACHTERNAAM together)

Does anybody know how to fix this problem.

thank you
__________________
Peter Dorsman
Reply With Quote
  #2 (permalink)  
Old 01-14-04, 07:45
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Code:
select ID_PERS
     , iif(isnull(ACHTERNAAM),'',ACHTERNAAM+', ')
     + iif(isnull(VOORLETTERS),'',VOORLETTERS+' ')
     + iif(isnull(TUSSENVOEGSEL,'',TUSSENVOEGSEL) as NAAM 
  from dbo.RAR_TBL_PERSONEN 
 where not VOORLETTERS is null
order 
    by NAAM
maybe i did not do the commas and spaces correctly, but this should give you the right idea
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 01-14-04, 08:02
namliam namliam is offline
Registered User
 
Join Date: Jan 2004
Location: The Netherlands
Posts: 421
i am a little out of SQL but how about this?

SeLECT ID_PERS, ACHTERNAAM + ',' + VOORLETTERS + rtrim(' ' + TUSSENVOEGSEL) AS NAAM FROM dbo.RAR_TBL_PERSONEN WHERE (NOT (VOORLETTERS IS NULL)) ORDER BY ACHTERNAAM + ',' + VOORLETTERS + rtrim(' ' + TUSSENVOEGSEL)

Greets from Amsterdam
Reply With Quote
  #4 (permalink)  
Old 01-14-04, 08:40
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
namliam, rtrim(' ' + TUSSENVOEGSEL) will still be null when TUSSENVOEGSEL is null, and then the whole expression it is concatenated to will be null again

there's not much you can do except coalesce it to an empty string

whoops, did i say coalesce

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 01-14-04, 08:46
peter1970 peter1970 is offline
Registered User
 
Join Date: Oct 2003
Location: Rotterdam
Posts: 17
Quote:
Originally posted by r937
Code:
select ID_PERS
     , iif(isnull(ACHTERNAAM),'',ACHTERNAAM+', ')
     + iif(isnull(VOORLETTERS),'',VOORLETTERS+' ')
     + iif(isnull(TUSSENVOEGSEL,'',TUSSENVOEGSEL) as NAAM 
  from dbo.RAR_TBL_PERSONEN 
 where not VOORLETTERS is null
order 
    by NAAM
maybe i did not do the commas and spaces correctly, but this should give you the right idea
When I use this statement, I get an error message, telling me the "isnull" function needs 2 argument.

What's the second argument ?
__________________
Peter Dorsman
Reply With Quote
  #6 (permalink)  
Old 01-14-04, 08:50
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
i think (because i've never done it myself) that if you are running an "access project" then you actually have to use sql server syntax

see the link for coalesce i gave
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 01-14-04, 09:16
peter1970 peter1970 is offline
Registered User
 
Join Date: Oct 2003
Location: Rotterdam
Posts: 17
Talking

found the sollution: want to know

SELECT ID_PERS, ACHTERNAAM + RTRIM(' ' + ISNULL(TUSSENVOEGSEL, N'')) + ', ' + VOORNAAM AS NAAM
FROM dbo.RAR_TBL_PERSONEN
WHERE (NOT (VOORLETTERS IS NULL))
ORDER BY NAAM

thank you all for thinking with me
__________________
Peter Dorsman
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On