空或空检查字符串变量

时间:2013-04-18 10:11:16

标签: sql sql-server-2008 null null-check

if((isnull(@value,''))='')

我想知道上面的代码是否可以检查变量是null还是空。

9 个答案:

答案 0 :(得分:35)

是的,那段代码确实如此。

您也可以使用:

if (@value is null or @value = '')

编辑:

添加@valueint值的信息后,您需要:

if (@value is null)

int值永远不能包含值''

答案 1 :(得分:9)

使用这种方式更好

if LEN(ISNULL(@Value,''))=0              

此字段为emptyNULL

答案 2 :(得分:7)

是的,您也可以使用基于ANSI SQL标准的COALESCE(@value,'')=''

SELECT CASE WHEN COALESCE(@value,'')='' 
    THEN 'Yes, it is null or empty' ELSE 'No, not null or empty' 
    END AS IsNullOrEmpty

DEMO

答案 3 :(得分:1)

是的,它有效。请查看以下示例。假设@value不是int

WITH CTE 
AS
(
    SELECT NULL AS test
    UNION
    SELECT '' AS test
    UNION
    SELECT '123' AS test
)

SELECT 
    CASE WHEN isnull(test,'')='' THEN 'empty' ELSE test END AS IS_EMPTY 
FROM CTE

结果:

IS_EMPTY
--------
empty
empty
123

答案 4 :(得分:1)

试试这个:

ISNULL(IIF (ColunmValue!='',ColunmValue, 'no units exists') , 'no units exists') AS 'ColunmValueName' 

答案 5 :(得分:1)

    IF (LEN(@value) > 0) PRINT 'Variable is not null and not empty'

答案 6 :(得分:0)

你可以尝试

<column_name> is null

where子句中。

答案 7 :(得分:0)

你可以试试这个......

DECLARE @value Varchar(100)=NULL
IF(@value = '' OR @value IS NULL)
  BEGIN
    select 1
  END
ELSE
  BEGIN
    select 0
  END

答案 8 :(得分:0)

Starting program: /usr/bin/clang-tags -d . -s chfiles
Missing separate debuginfos, use: dnf debuginfo-install glibc-2.30-5.fc31.x86_64
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
db_path: .
src_files_list_file: chfiles
parse: (null)
[New Thread 0x7fffef179700 (LWP 1885)]

Thread 2 "clang-tags" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7fffef179700 (LWP 1885)]
0x00007ffff6814200 in clang::NestedNameSpecifier::getKind() const () from /lib64/libclangAST.so.9
Missing separate debuginfos, use: dnf debuginfo-install clang-libs-9.0.1-2.fc31.x86_64 libedit-3.1-30.20191211cvs.fc31.x86_64 libffi-3.1-23.fc31.x86_64 libgcc-9.2.1-1.fc31.x86_64 libstdc++-9.2.1-1.fc31.x86_64 llvm-libs-9.0.1-5.fc31.x86_64 ncurses-libs-6.1-12.20190803.fc31.x86_64 zlib-1.2.11-19.fc31.x86_64
(gdb) c
Continuing.
libclang: crash detected during parsing: {
  'source_filename' : '(null)'
  'command_line_args' : ['clang', '/usr/bin/c++', '--driver-mode=g++', '-DBOOST_TEST_DYN_LINK', '-DFMT_SHARED', '-DSEASTAR_API_LEVEL=2', '-DSEASTAR_DEBUG', '-DSEASTAR_DEBUG_SHARED_PTR', '-DSEASTAR_DEFAULT_ALLOCATOR', '-DSEASTAR_HAS_MEMBARRIER', '-DSEASTAR_HAVE_ASAN_FIBER_SUPPORT', '-DSEASTAR_HAVE_GCC6_CONCEPTS', '-DSEASTAR_HAVE_HWLOC', '-DSEASTAR_HAVE_LZ4_COMPRESS_DEFAULT', '-DSEASTAR_HAVE_NUMA', '-DSEASTAR_SHUFFLE_TASK_QUEUE', '-DSEASTAR_THREAD_STACK_GUARDS', '-DSEASTAR_TYPE_ERASE_MORE', '-I/mnt/hgfs/seastar/include', '-I/mnt/hgfs/seastar/gen/include', '-g', '-fvisibility=hidden', '-UNDEBUG', '-Wall', '-Werror', '-Wno-error=deprecated-declarations', '-gz', '-std=gnu++17', '-U_FORTIFY_SOURCE'[Thread 0x7fffef179700 (LWP 1885) exited]
, '-Wno-maybe-uninitialized', '-Wno-error=unused-result', '-fsanitize=address', '-fsanitize=undefined', '-fno-sanitize=vptr', '-fconcepts', '-o', 'CMakeFiles/seastar_testing.dir/src/testing/entry_point.cc.o', '-c', '/mnt/hgfs/seastar/src/testing/entry_point.cc'],
  'unsaved_files' : [],
  'options' : 0,
}
clang-tags.c:376 err:1 
[Inferior 1 (process 1881) exited with code 01]
bt:
0x00007ffff6814200 in clang::NestedNameSpecifier::getKind() const () from /lib64/libclangAST.so.9
(gdb) bt
#0  0x00007ffff6814200 in clang::NestedNameSpecifier::getKind() const () from /lib64/libclangAST.so.9
#1  0x00007ffff6b6158d in clang::Sema::ShouldEnterDeclaratorScope(clang::Scope*, clang::CXXScopeSpec const&) () from /lib64/libclangSema.so.9
#2  0x00007fffefdba1c2 in clang::Parser::ParseDirectDeclarator(clang::Declarator&) () from /lib64/libclangParse.so.9
#3  0x00007fffefdaaa03 in clang::Parser::ParseDeclaratorInternal(clang::Declarator&, void (clang::Parser::*)(clang::Declarator&)) () from /lib64/libclangParse.so.9
#4  0x00007fffefdbc22a in clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&, clang::DeclaratorContext, clang::SourceLocation*, clang::Parser::ForRangeInit*) ()
   from /lib64/libclangParse.so.9
#5  0x00007fffefe55c02 in clang::Parser::ParseDeclOrFunctionDefInternal(clang::Parser::ParsedAttributesWithRange&, clang::ParsingDeclSpec&, clang::AccessSpecifier) ()
   from /lib64/libclangParse.so.9
#6  0x00007fffefe561d7 in clang::Parser::ParseDeclarationOrFunctionDefinition(clang::Parser::ParsedAttributesWithRange&, clang::ParsingDeclSpec*, clang::AccessSpecifier) ()
   from /lib64/libclangParse.so.9
....