2

For example, I have a list of values in a string:

'a', 'c', 'b', 'd'

From the data table, I got a column result like:

Result
'a'
'b'

how to write a sql which will return the values not in the table: 'c', 'd'

or

NewResult
'c'
'd'

?

It is also ok if other simple tools than sql can be used. I only need the result. Thanks!

4
  • 4
    What queries have you tried so far? What have you found to work, and what isn't working for you? Commented May 3, 2013 at 15:54
  • i am stuck with making 'a', 'c', 'b', 'd' to a column. Do I have to create a temp table to do this? Then I have to insert things between 'a', 'c', 'b', 'd'... actually the string is a much more longer string... I read something about pivot/unpivot, but still no clue... Commented May 3, 2013 at 15:59
  • @vincebowdren so what you think I should try? If there is not other solution, I will write some C# code to do it. but first, I hope sql itself can solve it... Commented May 3, 2013 at 16:04
  • I think you should search for some sql functions which can do the data manipulation you need, and try them out. Maybe you find a sql tutorial on string variables and functions, and see what you can teach yourself? Commented May 3, 2013 at 16:11

3 Answers 3

2
Create FUNCTION F_SplitAsTable 
(
@txt varchar(max)
)
RETURNS 
@tab TABLE 
(
 ID Varchar(2000)
)
AS
BEGIN
    declare @i int
    declare @s varchar(2000)
    Set @i = CHARINDEX(',',@txt)
    While @i>1
        begin
          set @s = LEFT(@txt,@i-1)
          insert into @tab (id) values (@s)
          Set @txt=RIGHT(@txt,Len(@txt)-@i)
          Set @i = CHARINDEX(',',@txt)
        end
    insert into @tab (id) values (@txt) 
    RETURN 
END
GO

Declare @a table (Ch varchar(10))
insert into @a Values ('a')
insert into @a Values ('b')


Select s.* from dbo.F_SplitAsTable('a,b,c,d') s
left join @a a on a.Ch=s.ID
where a.Ch is NULL
Sign up to request clarification or add additional context in comments.

1 Comment

it works after I changed 'a', 'c', 'b', 'd' to 'a,c,b,d', though I do not know if you can handle 'a', 'c', 'b', 'd' as parameters too :)... thanks for the solution!
2

Step 1: Load the search values in a temp table.

DECLARE @Search table (SearchFor char(1)  not null)

INSERT @Search values ('a'), ('b'), ('c'), ('d')

(There are any number of ways to set this up, this is just the fastest to type)

Run a query like so:

SELECT SearchFor
 from @Search
except select SearchIn
 from DataTable

(Again, there are many forms that "in a not in b" queries can take.)

This will return everything in the first set (your temp table) that is not also found in the second set.

1 Comment

is there an easy way to make ('a'), ('b'), ('c'), ('d') from 'a', 'c', 'b', 'd'?
1

use the not in clause in your queries.

select myCol from myTable where myCol not in ('c','d')
select myCol from myTable where myCol not in (select myCol from otherTable)

6 Comments

These will not provide the desired answer.
Yes, sadly someone sent me a string... not something like myCol... but thanks anyway
The question is a bit ambiguous, so it's hard to tell right now.
Can't you just treat the string as a CSV, and import it into your database? Then the query would work.
The second would work if "otherTable" was a temp table populated by a split of the string inserted into a column by that name in that temp table
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.