Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 615 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use NULL or empty string in SQL

#1
I would like to know how to use NULL and an empty string at the same time in a `WHERE` clause in SQL Server. I need to find records that have either null values or an empty string. Thanks.
Reply

#2
SELECT *
FROM TableName
WHERE columnNAme IS NULL OR
LTRIM(RTRIM(columnName)) = ''
Reply

#3
You can simply do this:

SELECT *
FROM yourTable
WHERE yourColumn IS NULL OR yourColumn = ''
Reply

#4
Select *
From Table
Where (col is null or col = '')

Or

Select *
From Table
Where IsNull(col, '') = ''


Reply

#5
SELECT *
FROM Table
WHERE column like '' or column IS NULL OR LEN(column) = 0
Reply

#6
You could use [`isnull`](

[To see links please register here]

) function to get both `null` and empty values of a text field:

SELECT * FROM myTable
WHERE isnull(my_nullable_text_field,'') = ''
Reply

#7

--setup
IF OBJECT_ID('tempdb..#T') IS NOT NULL DROP TABLE #T;
CREATE TABLE #T(ID INT NOT NULL IDENTITY(1,1) PRIMARY KEY, NAME VARCHAR(10))
INSERT INTO #T (Name) VALUES('JOHN'),(''),(NULL);
SELECT * FROM #T
1 JOHN
2 -- is empty string
3 NULL

You can examine `''` as `NULL` by converting it to `NULL` using `NULLIF`

--here you set '' to null
UPDATE #T SET NAME = NULLIF(NAME,'')
SELECT * FROM #T
1 JOHN
2 NULL
3 NULL

or you can examine `NULL` as `''` using `SELECT ISNULL(NULL,'')`

-- here you set NULL to ''
UPDATE #T SET NAME = ISNULL(NULL,'') WHERE NAME IS NULL
SELECT * FROM #T
1 JOHN
2 -- is empty string
3 -- is empty string

--clean up
DROP TABLE #T
Reply

#8
This is ugly MSSQL:

CASE WHEN LTRIM(RTRIM(ISNULL([Address1], ''))) <> '' THEN [Address2] ELSE '' END
Reply

#9
To find rows where col is `NULL`, empty string or whitespace (spaces, tabs):

SELECT *
FROM table
WHERE ISNULL(LTRIM(RTRIM(col)),'')=''

To find rows where col is `NOT NULL`, empty string or whitespace (spaces, tabs):

SELECT *
FROM table
WHERE ISNULL(LTRIM(RTRIM(col)),'')<>''
Reply

#10

**Some [sargable][1] methods...**

SELECT *
FROM #T
WHERE SomeCol = '' OR SomeCol IS NULL;

SELECT *
FROM #T
WHERE SomeCol = ''
UNION ALL
SELECT *
FROM #T
WHERE SomeCol IS NULL;

SELECT *
FROM #T
WHERE EXISTS ((SELECT NULL UNION SELECT '') INTERSECT SELECT SomeCol);


**And some non-sargable ones...**


SELECT *
FROM #T
WHERE IIF(SomeCol <> '',0,1) = 1;

SELECT *
FROM #T
WHERE NULLIF(SomeCol,'') IS NULL;

SELECT *
FROM #T
WHERE ISNULL(SomeCol,'') = '';


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through