0

I have a employee table and I am trying to get only comma values in SQL Server.

Emp table:

Eid Ename
1 Peter,J
2 Mike,S
3 ,
4 ,,,,,,

I tried this code:

SELECT
    (LEN(ename) - LEN(REPLACE(ename, ',', ''))
FROM emp;

I am not getting the length 0 if the values contain only commas.

Expected result: I want only 3 and 4 emp ids.

9
  • 1
    WHERE EName NOT LIKE '%[^ ,]%'? Commented Nov 24, 2021 at 14:18
  • 1
    While asking a question, you need to provide a minimal reproducible example: (1) DDL and sample data population, i.e. CREATE table(s) plus INSERT T-SQL statements. (2) What you need to do, i.e. logic and your code attempt implementation of it in T-SQL. (3) Desired output, based on the sample data in the #1 above. (4) Your SQL Server version (SELECT @@version;). Commented Nov 24, 2021 at 14:19
  • I want only comma values. Commented Nov 24, 2021 at 14:20
  • So, just EID 3 then? Commented Nov 24, 2021 at 14:25
  • 1
    @PanagiotisKanavos heh, I'd say the actual problem is "putting CSV data in a column" :) Commented Nov 24, 2021 at 14:39

3 Answers 3

1

If you're after rows that only contain commas you can use some pattern matching:

SELECT ID, ename
FROM dbo.YourTable
WHERE EName NOT LIKE '%[^,]%';

The pattern will match any characters that aren't a comma, and then the NOT LIKE effectively reverses that; meaning that only rows that contain commas (or a zero length string) will be returned. If you want to omit zero length strings too, then add an additional clause in the WHERE to exclude them.

Sign up to request clarification or add additional context in comments.

Comments

0
select x.eid,x.ename 
from
(
select 1 eid,'Peter,J'  ename union
select 2 eid,'Mike,S' ename union
select 3 eid, ','      ename  union
select 4 eid,',,,,,'  ename 
) x
where len(x.ename)>0 and len( replace(x.ename,',','') ) =0

The answer there is condition line "where ...."

Comments

0

Please try the following solution.

SQL #1 is tokenizing the ename column by using XML/Query.

Predicate r[text()] picks up tokens with any value.

SQL #2 is much simpler.

SQL #1

-- DDL and sample data population, start
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, ename VARCHAR(255));
INSERT INTO @tbl (ename) VALUES
('Peter,J'),
('Mike,S'),
(','),
(', , , ,');
-- DDL and sample data population, end

DECLARE @separator CHAR(1) = ',';

SELECT * 
FROM @tbl
CROSS APPLY (SELECT CAST('<root><r><![CDATA[' + 
         REPLACE(REPLACE(ename,SPACE(1),''), @separator, ']]></r><r><![CDATA[') + 
         ']]></r></root>' AS XML).query('
         for $x in /root/r[text()]
         return $x
         ').value('.','VARCHAR(255)')) AS t1(c)
WHERE c = '';

Output

+----+---------+---+
| ID |  ename  | c |
+----+---------+---+
|  3 |       , |   |
|  4 | , , , , |   |
+----+---------+---+

SQL #2

SELECT * 
FROM @tbl
    CROSS APPLY (SELECT REPLACE(ename, ',','')) AS t(c)
WHERE c = '';

Output

+----+---------+---+
| ID |  ename  | c |
+----+---------+---+
|  3 |       , |   |
|  4 | , , , , |   |
+----+---------+---+

4 Comments

All SQL Server versions in mainstream support have STRING_SPLIT. Even SQL Server 2016 is out of mainstream support now
@PanagiotisKanavos, The OP didn't provide a minimal reproducible example. That's why I had to stoop down to earlier versions.
For 2016 you can use JSON instead of XML, which will be faster. For older versions - we're 8 years back already, the OP should make it clear just how old the server is
It's reasonable that we should encourage moving away from lowest common denominator solutions..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.