Jak sprawdzić, czy łańcuch Sql server jest null lub empty

Chcę sprawdzić dane, ale zignorować je, jeśli jest puste lub null. Obecnie zapytanie jest następujące...

Select              
Coalesce(listing.OfferText, company.OfferText, '') As Offer_Text,         
from tbl_directorylisting listing  
 Inner Join tbl_companymaster company            
  On listing.company_id= company.company_id      
Ale chcę mieć towarzystwo.OfferText if listing.Offertext jest pustym łańcuchem znaków, tak samo jak null.

Jakie jest najlepsze rozwiązanie?

Author: digiguru, 2008-12-02

15 answers

Myślę tak:

SELECT 
  ISNULL(NULLIF(listing.Offer_Text, ''), company.Offer_Text) AS Offer_Text
FROM ...
To najbardziej eleganckie rozwiązanie.

I trochę rozbić w pseudo kodzie:

// a) NULLIF:
if (listing.Offer_Text == '')
  temp := null;
else
  temp := listing.Offer_Text; // may now be null or non-null, but not ''
// b) ISNULL:
if (temp is null)
  result := true;
else
  result := false;
 360
Author: Martin Ba,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-27 10:32:37
SELECT
   CASE WHEN LEN(listing.OfferText) > 0 THEN listing.OfferText 
        ELSE COALESCE(Company.OfferText, '') END 
   AS Offer_Text,

... 

W tym przykładzie, jeśli listing.OfferText jest równe NULL, funkcja LEN() powinna również zwracać NULL, ale nadal nie jest to > 0.

Update

Nauczyłem się kilku rzeczy w ciągu 5 i pół roku od opublikowania tego, a teraz robię to znacznie inaczej: {]}

COALESCE(NULLIF(listing.OfferText,''), Company.OfferText, '')

Jest to podobne do zaakceptowanej odpowiedzi, ale ma również alternatywę w przypadku, gdy Company.OfferText jest również null. Żadna z innych aktualnych odpowiedzi za pomocą NULLIF() również tego nie robi.

 39
Author: Joel Coehoorn,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-18 21:45:22
Select              
CASE
    WHEN listing.OfferText is null or listing.OfferText = '' THEN company.OfferText
    ELSE COALESCE(Company.OfferText, '')
END As Offer_Text,         
from tbl_directorylisting listing  
 Inner Join tbl_companymaster company            
  On listing.company_id= company.company_id
 32
Author: Patrick Harrington,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-12-02 14:36:58

Oto inne rozwiązanie:

SELECT Isnull(Nullif(listing.offertext, ''), company.offertext) AS offer_text, 
FROM   tbl_directorylisting listing 
       INNER JOIN tbl_companymaster company 
         ON listing.company_id = company.company_id
 16
Author: Anax,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-07-13 08:30:08

Możesz użyć ISNULL i sprawdzić odpowiedź na znanym wyjściu:

SELECT case when ISNULL(col1, '') = '' then '' else col1 END AS COL1 FROM TEST
 11
Author: Code Trawler,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-03-07 15:26:49

W SQL Server 2012 MASZ IIF, np. możesz go używać jak

SELECT IIF(field IS NULL, 1, 0) AS IsNull

W ten sam sposób możesz sprawdzić, czy pole jest puste.

 10
Author: lkurylo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-18 16:54:15

Użyj funkcji LEN, aby sprawdzić wartości null lub empty. Możesz po prostu użyć LEN (@SomeVarcharParm) > 0. Zwróci false, jeśli wartość jest NULL,", lub''. Dzieje się tak dlatego, że len (NULL) zwraca NULL, a null > 0 zwraca false. Również LEN (' ' ) zwraca 0. Zobaczcie sami:

SELECT 
 CASE WHEN NULL > 0 THEN 'NULL > 0 = true' ELSE 'NULL > 0 = false' END,
 CASE WHEN LEN(NULL) > 0 THEN 'LEN(NULL) = true' ELSE 'LEN(NULL) = false' END,
 CASE WHEN LEN('') > 0 THEN 'LEN('''') > 0 = true' ELSE 'LEN('''') > 0 = false' END,
 CASE WHEN LEN(' ') > 0 THEN 'LEN('' '') > 0 = true' ELSE 'LEN('' '') > 0 = false' END,
 CASE WHEN LEN(' test ') > 0 THEN 'LEN('' test '') > 0 = true' ELSE 'LEN('' test '') > 0 = false' END
 4
Author: Zach Johnson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-14 17:57:50
Select              
Coalesce(NullIf(listing.OfferText, ''), NullIf(company.OfferText, ''), '') As Offer_Text,         
from tbl_directorylisting listing  
 Inner Join tbl_companymaster company            
  On listing.company_id= company.company_id
 3
Author: Muhammad Sharjeel Ahsan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-09-10 05:52:59

To proste połączenie COALESCE i NULLIF powinno załatwić sprawę:

SELECT             
  Coalesce(NULLIF(listing.OfferText, ''), company.OfferText) As Offer_Text
...

Uwaga: Dodaj kolejny pusty łańcuch jako ostatni argument COALESCE, jeśli chcesz, aby Instrukcja zwracała pusty łańcuch zamiast NULL, jeśli obie wartości są NULL.

 3
Author: contactmatt,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-01-26 22:11:06

Oto rozwiązanie, ale nie wiem, czy jest najlepsze....

Select              
Coalesce(Case When Len(listing.Offer_Text) = 0 Then Null Else listing.Offer_Text End, company.Offer_Text, '') As Offer_Text,         
from tbl_directorylisting listing  
 Inner Join tbl_companymaster company            
  On listing.company_id= company.company_id
 2
Author: digiguru,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-12-02 14:39:06
SELECT              
    COALESCE(listing.OfferText, 'company.OfferText') AS Offer_Text,         
FROM 
    tbl_directorylisting listing  
    INNER JOIN tbl_companymaster company ON listing.company_id= company.company_id
 2
Author: Anoop Verma,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-01-15 23:23:04

Ta składnia:

SELECT *
FROM tbl_directorylisting listing
WHERE (civilite_etudiant IS NULL)

Pracował dla mnie w Microsoft SQL Server 2008 (SP3)

 2
Author: Romain Durand,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-14 09:36:26

Aby zapobiec rekordom z wartością Empty LUB Null w wyniku SQL

Możemy po prostu dodać ..... WHERE Column_name != '' or 'null'

 1
Author: Code Crawler,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-07-28 15:02:44
[Column_name] IS NULL OR LEN(RTRIM(LTRIM([Column_name]))) = 0
 0
Author: user3829854,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-17 21:42:22

Wiem, że to stary wątek, ale właśnie widziałem jeden z wcześniejszych postów powyżej i nie jest poprawny. Jeśli używasz LEN () aby określić, czy plik jest NULL czy pusty, musisz użyć go w następujący sposób:

...WHEN LEN(ISNULL(MyField, '')) < 1 THEN NewValue...
 0
Author: Milan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-28 18:02:19