Programming

"빈 값 또는 null 값"을 확인하는 가장 좋은 방법

procodes 2020. 6. 15. 22:24
반응형

"빈 값 또는 null 값"을 확인하는 가장 좋은 방법


Postgres SQL 문에서 value가 null 또는 빈 문자열인지 확인하는 가장 좋은 방법은 무엇입니까?

값은 긴 표현식 일 수 있으므로 한 번에 한 번만 작성하는 것이 좋습니다.

현재 나는 다음을 사용하고 있습니다 :

coalesce( trim(stringexpression),'')=''

그러나 조금 추한 것 같습니다.

stringexpression후행 공백이 char(n)있는 char(n)열을 포함하는 또는 표현식 일 수 있습니다 .

가장 좋은 방법은 무엇입니까?


표현 stringexpression = ''은 다음과 같습니다.

TRUE   .. 대 ''(또는 대한 모든 데이터 유형과 공간에서만 이루어지는 문자열 char(n))
NULL   에 대한 .. NULL
FALSE용 .. 어떤 다른

확인하려면 : " stringexpressionis NULL or empty" :

(stringexpression = '') IS NOT FALSE

또는 그 반대의 접근 방식 (읽기가 더 쉬울 수 있음) :

(stringexpression <> '') IS NOT TRUE

작동 모든 문자 유형 시대에 뒤 떨어진 포함 char(n)좀처럼 유용합니다.
비교 연산자에 대한 매뉴얼.

또는trim() 쓸모없는char(n)(아래 참조)없이 이미 가지고있는 표현식을 사용하거나 다른 문자 유형에 대한 테스트에서 공백으로 구성된 문자열을 포함합니다.

coalesce(stringexpression, '') = ''

그러나 상단의 표현이 더 빠릅니다.

반대의 주장 : " stringexpressionnull도 아니고 비어 있지도 않다" 는 것이 더 간단합니다.

stringexpression <> ''

char(n)

다른 문자 유형 등으로이 데이터 유형 혼동하지 마십시오 varchar(n), varchar, text또는"char" 모든 유용한 데이터 유형 (따옴표)를. 이것은 매우 제한된 유용성과 오래된 데이터 유형에 관한 것입니다 : char(n)짧은을 위해 : character(n). 또한, charcharacter짧은 있습니다 char(1)/ character(1)(같은 것).

에서 char(n)(다른 문자열 유형과 달리!)는 빈 문자열은 공백 만 구성된 다른 문자열에서 다르지 않다. 이러한 유형은 유형 정의에 따라 n 개의 공백으로 접 힙니다 char(n). 논리적으로 다음과 같이 작동 char(n)합니다.

coalesce(stringexpression, '') = ''

이것들만큼이나 (다른 문자 유형에서는 작동하지 않을 것입니다) :

coalesce(stringexpression, '  ') = '  '
coalesce(stringexpression, '') = '       '

데모

빈 문자열은 다음으로 캐스팅 할 때 공백 문자열과 같습니다 char(n).

SELECT ''::char(5) = ''::char(5)     AS eq1
      ,''::char(5) = '  '::char(5)   AS eq2
      ,''::char(5) = '    '::char(5) AS eq3;
eq1 | eq2 | eq3
----+-----+----
t   | t   | t  

다음을 사용하여 "널 또는 빈 문자열"을 테스트하십시오 char(n).

SELECT stringexpression 
      ,stringexpression = ''                    AS simple_test
      ,(stringexpression = '')  IS NOT FALSE    AS test1
      ,(stringexpression <> '') IS NOT TRUE     AS test2
      ,coalesce(stringexpression, '') = ''      AS test_coalesce1
      ,coalesce(stringexpression, '  ') = '  '  AS test_coalesce2
      ,coalesce(stringexpression, '') = '  '    AS test_coalesce3
FROM  (
   VALUES
     ('foo'::char(5))
   , ('')
   , (NULL)
   , ('   ')                -- not different from '' in char(n)
   ) sub(stringexpression);
 stringexpression | simple_test | test1 | test2 | test_coalesce1 | test_coalesce2 | test_coalesce3
------------------+-------------+-------+-------+----------------+----------------+----------------
 foo              | f           | f     | f     | f              | f              | f
                  | t           | t     | t     | t              | t              | t
                  |             | t     | t     | t              | t              | t
                  | t           | t     | t     | t              | t              | t

다음과 같이 "널 또는 빈 문자열"테스트 text

SELECT stringexpression 
      ,stringexpression = ''                    AS simple_test
      ,(stringexpression = '')  IS NOT FALSE    AS test1
      ,(stringexpression <> '') IS NOT TRUE     AS test2
      ,coalesce(stringexpression, '') = ''      AS test_coalesce1
      ,coalesce(stringexpression, '  ') = '  '  AS test_coalesce2
      ,coalesce(stringexpression, '') = '  '    AS test_coalesce3
FROM  (
   VALUES
     ('foo'::text)
   , ('')
   , (NULL)
   , ('   ')                -- different from '' in a sane character type like text
   ) sub(stringexpression);
 stringexpression | simple_test | test1 | test2 | test_coalesce1 | test_coalesce2 | test_coalesce3
------------------+-------------+-------+-------+----------------+----------------+----------------
 foo              | f           | f     | f     | f              | f              | f
                  | t           | t     | t     | t              | f              | f
                  |             | t     | t     | t              | t              | f
                  | f           | f     | f     | f              | f              | f

dbfiddle here
이전 SQL 바이올린

관련 :


널과 비어 있는지 확인하려면 다음을 수행하십시오.

coalesce(string, '') = ''

널, 공백 및 공백을 확인하려면 (문자열 자르기)

coalesce(TRIM(string), '') = ''

문자열의 길이를 확인하는 것도 효과적이며 컴팩트합니다.

where length(stringexpression) > 0;

후행 공백이 비어 있으면 더 나은 해결책이 없을 것입니다. COALESCE당신과 같은 문제에 대한 것입니다.


내가 사람들이 사용한 것을 보았습니다 stringexpression > ''. 이것은 가장 빠른 것이 아니지만 가장 짧은 것 중 하나입니다.

MS SQL과 PostgreSQL에서 시도했습니다.


My preffered way to compare nullable fields is: NULLIF(nullablefield, :ParameterValue) IS NULL AND NULLIF(:ParameterValue, nullablefield) IS NULL . This is cumbersome but is of universal use while Coalesce is impossible in some cases.

The second and inverse use of NULLIF is because "NULLIF(nullablefield, :ParameterValue) IS NULL" will always return "true" if the first parameter is null.


If database having large number of records then null check can take more time you can use null check in different ways like : 1) where columnname is null 2) where not exists() 3) WHERE (case when columnname is null then true end)


another way is

nullif(trim(stringExpression),'') is not null

참고URL : https://stackoverflow.com/questions/23766084/best-way-to-check-for-empty-or-null-value

반응형