如何使用 SQL Server 将空格转换为空值? [英] How to convert empty spaces into null values, using SQL Server?

查看:137
本文介绍了如何使用 SQL Server 将空格转换为空值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张表,该表上的列包含一些记录的空格.现在我需要将数据移动到另一个表并用 NULL 值替换空格.

I have a table and the columns on this table contains empty spaces for some records. Now I need to move the data to another table and replace the empty spaces with a NULL value.

我尝试使用:

REPLACE(ltrim(rtrim(col1)),' ',NULL)

但它不起作用.它会将 col1 的所有值转换为 NULL.我只想将那些有空格的值转换为 NULL.

but it doesn't work. It will convert all of the values of col1 to NULL. I just want to convert only those values that have empty spaces to NULL.

推荐答案

你试过了吗?

UPDATE table 
SET col1 = NULL 
WHERE col1 = ''

正如评论者指出的那样,您不必执行 ltrim()rtrim(),并且 NULL 列不会匹配''.

As the commenters point out, you don't have to do ltrim() or rtrim(), and NULL columns will not match ''.

这篇关于如何使用 SQL Server 将空格转换为空值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆