| |
|
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.
|
 |

01-14-04, 07:17
|
|
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
|
|

01-14-04, 07:45
|
|
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
|
|

01-14-04, 08:02
|
|
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 
|
|

01-14-04, 08:40
|
|
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

|
|

01-14-04, 08:46
|
|
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
|
|

01-14-04, 08:50
|
|
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
|
|

01-14-04, 09:16
|
|
Registered User
|
|
Join Date: Oct 2003
Location: Rotterdam
Posts: 17
|
|
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
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|