표현 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, '') = ''
그러나 상단의 표현이 더 빠릅니다.
반대의 주장은 훨씬 간단합니다 : " stringexpressionis not null or empty" :
stringexpression <> ''
이 데이터 유형에 관한 char(n)짧은 들어, : character(n). ( char/ character에 대한 짧은 char(1)/ character(1).)의 사용이다 포스트 그레스에 낙담 :
대부분의 상황에서 text 또는 character varying대신 사용해야합니다.
char(n)다른 유용한 문자 유형과 혼동하지 마십시오 varchar(n).varchar , text또는"char" (따옴표 포함).
에서 빈 문자열 공백 만 구성된 다른 문자열에서 다르지 않다. 이러한 유형은 유형 정의에 따라 n 개의 공백으로 접 힙니다 . 논리적으로 위의 표현식이 작동합니다.char(n)char(n)char(n) 은 다른 문자 유형에서는 작동하지 않는 과 마찬가지로 작동합니다.
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 base_test
, (stringexpression = '') IS NOT FALSE AS test1
, (stringexpression <> '') IS NOT TRUE AS test2
, coalesce(stringexpression, '') = '' AS coalesce1
, coalesce(stringexpression, ' ') = ' ' AS coalesce2
, coalesce(stringexpression, '') = ' ' AS coalesce3
FROM (
VALUES
('foo'::char(5))
, ('')
, (' ') -- not different from '' in char(n)
, (NULL)
) sub(stringexpression);
결과:
문자열 표현 | base_test | test1 | test2 | coalesce1 | coalesce2 | 합체 3
------------------ + ----------- + ------- + ------- + --- -------- + ----------- + -----------
foo | f | f | f | f | f | 에프
| t | t | t | t | t | 티
| t | t | t | t | t | 티
null | null | t | t | t | t | 티
다음을 사용하여 "널 또는 빈 문자열"을 테스트하십시오 text.
SELECT stringexpression
, stringexpression = '' AS base_test
, (stringexpression = '') IS NOT FALSE AS test1
, (stringexpression <> '') IS NOT TRUE AS test2
, coalesce(stringexpression, '') = '' AS coalesce1
, coalesce(stringexpression, ' ') = ' ' AS coalesce2
, coalesce(stringexpression, '') = ' ' AS coalesce3
FROM (
VALUES
('foo'::text)
, ('')
, (' ') -- different from '' in a sane character types
, (NULL)
) sub(stringexpression);
결과:
문자열 표현 | base_test | test1 | test2 | coalesce1 | coalesce2 | 합체 3
------------------ + ----------- + ------- + ------- + --- -------- + ----------- + -----------
foo | f | f | f | f | f | 에프
| t | t | t | t | f | 에프
| f | f | f | f | f | 에프
null | null | t | t | t | t | 에프
db <> 바이올린 여기에
오래된 sqlfiddle
관련 :
char패딩 (및 결과로 발생하는 공간 낭비) 때문에 사용 은 거의 항상 잘못된 선택입니다. 그러나 그 외에도 : 더 나은 해결책이 없다고 생각합니다.