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:
  • 855 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I select from list of values in SQL Server

#11
In general :

SELECT
DISTINCT
FieldName1, FieldName2, ..., FieldNameN
FROM
(
Values
( ValueForField1, ValueForField2,..., ValueForFieldN ),
( ValueForField1, ValueForField2,..., ValueForFieldN ),
( ValueForField1, ValueForField2,..., ValueForFieldN ),
( ValueForField1, ValueForField2,..., ValueForFieldN ),
( ValueForField1, ValueForField2,..., ValueForFieldN )
) AS TempTableName ( FieldName1, FieldName2, ..., FieldNameN )


In your case :

Select
distinct
TempTableName.Field1
From
(
VALUES
(1),
(1),
(1),
(2),
(5),
(1),
(6)
) AS TempTableName (Field1)
Reply

#12
I know this is a pretty old thread, but I was searching for something similar and came up with this.

Given that you had a comma-separated string, you could use `string_split`

select distinct value from string_split('1, 1, 1, 2, 5, 1, 6',',')

This should return

1
2
5
6

String split takes two parameters, the string input, and the separator character.

you can add an optional where statement using `value` as the column name

select distinct value from string_split('1, 1, 1, 2, 5, 1, 6',',')
where value > 1
produces

2
5
6

Reply

#13
> **Select user id from list of user id:**
>
> SELECT * FROM my_table WHERE user_id IN (1,3,5,7,9,4);
Reply

#14
If it is a list of parameters from existing SQL table, for example ID list from existing Table1, then you can try this:

select distinct ID
FROM Table1
where
ID in (1, 1, 1, 2, 5, 1, 6)
ORDER BY ID;

Or, if you need List of parameters as a SQL Table constant(variable), try this:

WITH Id_list AS (
select ID
FROM Table1
where
ID in (1, 1, 1, 2, 5, 1, 6)
)
SELECT distinct * FROM Id_list
ORDER BY ID;

Reply

#15
I create a function on most SQL DB I work on to do just this.

CREATE OR ALTER FUNCTION [dbo].[UTIL_SplitList](@parList Varchar(MAX),@splitChar Varchar(1)=',')
Returns @t table (Column_Value varchar(MAX))
as
Begin
Declare @pos integer
set @pos = CharIndex(@splitChar, @parList)
while @pos > 0
Begin
Insert Into @t (Column_Value) VALUES (Left(@parList, @pos-1))
set @parList = Right(@parList, Len(@parList) - @pos)
set @pos = CharIndex(@splitChar, @parList)
End
Insert Into @t (Column_Value) VALUES (@parList)
Return
End

Once the function exists, it is as easy as


SELECT DISTINCT
*
FROM
[dbo].[UTIL_SplitList]('1,1,1,2,5,1,6',',')
Reply

#16
Using `GROUP BY` gives you better performance than `DISTINCT`:

SELECT *
FROM
(
VALUES
(1),
(1),
(1),
(2),
(5),
(1),
(6)
) AS A (nums)
GROUP BY A.nums;
Reply

#17
Available only on SQL Server 2008 and over is row-constructor in this form:
You could use

SELECT DISTINCT *
FROM (
VALUES (1), (1), (1), (2), (5), (1), (6)
) AS X(a)

For more information see:

- [MS official](

[To see links please register here]

)
-

[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