Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- select * from Tags
- where Name in ('ruby','rails','scruffy','rubyonrails')
- order by Count desc
- select * from Tags
- where '|ruby|rails|scruffy|rubyonrails|'
- like '%|' + Name + '|%'
- string[] tags = new string[] { "ruby", "rails", "scruffy", "rubyonrails" };
- const string cmdText = "select * from tags where '|' + @tags + '|' like '%|' + Name + '|%'";
- using (SqlCommand cmd = new SqlCommand(cmdText)) {
- cmd.Parameters.AddWithValue("@tags", string.Join("|", tags);
- }
- string[] tags = new string[] { "ruby", "rails", "scruffy", "rubyonrails" };
- string cmdText = "SELECT * FROM Tags WHERE Name IN ({0})";
- string[] paramNames = tags.Select(
- (s, i) => "@tag" + i.ToString()
- ).ToArray();
- string inClause = string.Join(",", paramNames);
- using (SqlCommand cmd = new SqlCommand(string.Format(cmdText, inClause))) {
- for(int i = 0; i < paramNames.Length; i++) {
- cmd.Parameters.AddWithValue(paramNames[i], tags[i]);
- }
- }
- cmd.CommandText = "SELECT * FROM Tags WHERE Name IN (@tag0,@tag1,@tag2,@tag3)"
- cmd.Parameters["@tag0"] = "ruby"
- cmd.Parameters["@tag1"] = "rails"
- cmd.Parameters["@tag2"] = "scruffy"
- cmd.Parameters["@tag3"] = "rubyonrails"
- CREATE TYPE dbo.TagNamesTableType AS TABLE ( Name nvarchar(50) )
- string[] tags = new string[] { "ruby", "rails", "scruffy", "rubyonrails" };
- cmd.CommandText = "SELECT Tags.* FROM Tags JOIN @tagNames as P ON Tags.Name = P.Name";
- // value must be IEnumerable
- cmd.Parameters.AddWithValue("@tagNames", tags).SqlDbType = SqlDbType.Structured;
- cmd.Parameters["@tagNames"].TypeName = "dbo.TagNamesTableType";
- select ...
- where '|peanut|butter|' like '%|' + 'pe%ter' + '|%'
- select ...
- where '|butter|peanut|' like '%|' + 'pe%ter' + '|%'
- select ...
- where '|peanut|butter|'
- like '%|' + 'pe%ter' + '|%' escape ''
- select ...
- where '|pe%ter|'
- like '%|' + REPLACE( 'pe%ter' ,'%','%') + '|%' escape ''
- select ...
- where '|pe%t!r|'
- like '%|' + REPLACE(REPLACE( 'pe%t!r' ,'!','!!'),'%','!%') + '|%' escape '!'
- select ...
- where '|p_%t!r|'
- like '%|' + REPLACE(REPLACE(REPLACE( 'p_%t!r' ,'$','$$'),'%','$%'),'_','$_') + '|%' escape '$'
- DECLARE @tags
- SET @tags = ‘ruby|rails|scruffy|rubyonrails’
- select * from Tags
- where Name in (SELECT item from fnSplit(@tags, ‘|’))
- order by Count desc
- CREATE FUNCTION [dbo].[fnSplit](
- @sInputList VARCHAR(8000) -- List of delimited items
- , @sDelimiter VARCHAR(8000) = ',' -- delimiter that separates items
- ) RETURNS @List TABLE (item VARCHAR(8000))
- BEGIN
- DECLARE @sItem VARCHAR(8000)
- WHILE CHARINDEX(@sDelimiter,@sInputList,0) <> 0
- BEGIN
- SELECT
- @sItem=RTRIM(LTRIM(SUBSTRING(@sInputList,1,CHARINDEX(@sDelimiter,@sInputList,0)-1))),
- @sInputList=RTRIM(LTRIM(SUBSTRING(@sInputList,CHARINDEX(@sDelimiter,@sInputList,0)+LEN(@sDelimiter),LEN(@sInputList))))
- IF LEN(@sItem) > 0
- INSERT INTO @List SELECT @sItem
- END
- IF LEN(@sInputList) > 0
- INSERT INTO @List SELECT @sInputList -- Put the last item in
- RETURN
- END
- var inValues = new [] { "ruby","rails","scruffy","rubyonrails" };
- var results = from tag in Tags
- where inValues.Contains(tag.Name)
- select tag;
- string[] names = new string[] {"ruby","rails","scruffy","rubyonrails"};
- var tags = dataContext.Query<Tags>(@"
- select * from Tags
- where Name in @names
- order by Count desc", new {names});
- string[] names = new string[] {"ruby","rails","scruffy","rubyonrails"};
- var tags = from tag in dataContext.Tags
- where names.Contains(tag.Name)
- orderby tag.Count descending
- select tag;
- ALTER FUNCTION [dbo].[fn_sqllist_to_table](@list as varchar(8000), @delim as varchar(10))
- RETURNS @listTable table(
- Position int,
- Value varchar(8000)
- )
- AS
- BEGIN
- declare @myPos int
- set @myPos = 1
- while charindex(@delim, @list) > 0
- begin
- insert into @listTable(Position,Value)
- values(@myPos, left(@list, charindex(@delim, @list) - 1))
- set @myPos = @myPos + 1
- if charindex(@delim, @list) = len(@list)
- insert into @listTable(Position, Value)
- values(@myPos, '')
- set @list = right(@list, len(@list) - charindex(@delim, @list))
- end
- if len(@list) > 0
- insert into @listTable(Position, Value)
- values(@myPos, @list)
- Return
- @Name varchar(8000) = null // parameter for search values
- select * from Tags
- where Name in (SELECT value From fn_sqllist_to_table(@Name,',')))
- order by Count desc
- SELECT ...
- ...
- WHERE tag IN( @tag1, ISNULL( @tag2, @tag1 ), ISNULL( @tag3, @tag1 ), etc. )
- with qry(n, names) as
- (select len(list.names) - len(replace(list.names, ',', '')) - 1 as n,
- substring(list.names, 2, len(list.names)) as names
- from (select ',Doc,Grumpy,Happy,Sneezy,Bashful,Sleepy,Dopey,' names) as list
- union all
- select (n - 1) as n,
- substring(names, 1 + charindex(',', names), len(names)) as names
- from qry
- where n > 1)
- select n, substring(names, 1, charindex(',', names) - 1) dwarf
- from qry;
- select n, substr(name, 1, instr(name, ',') - 1) dwarf
- from (select n,
- substr(val, 1 + instr(val, ',', 1, n)) name
- from (select rownum as n,
- list.val
- from (select ',Doc,Grumpy,Happy,Sneezy,Bashful,Sleepy,Dopey,' val
- from dual) list
- connect by level < length(list.val) -
- length(replace(list.val, ',', ''))));
- select pivot.n,
- substring_index(substring_index(list.val, ',', 1 + pivot.n), ',', -1) from (select 1 as n
- union all
- select 2 as n
- union all
- select 3 as n
- union all
- select 4 as n
- union all
- select 5 as n
- union all
- select 6 as n
- union all
- select 7 as n
- union all
- select 8 as n
- union all
- select 9 as n
- union all
- select 10 as n) pivot, (select ',Doc,Grumpy,Happy,Sneezy,Bashful,Sleepy,Dopey,' val) as list where pivot.n < length(list.val) -
- length(replace(list.val, ',', ''));
- <cfset myvalues = "ruby|rails|scruffy|rubyonrails">
- <cfquery name="q">
- select * from sometable where values in <cfqueryparam value="#myvalues#" list="true">
- </cfquery>
- CREATE FUNCTION dbo.fnParseArray (@Array VARCHAR(1000),@separator CHAR(1))
- RETURNS @T Table (col1 varchar(50))
- AS
- BEGIN
- --DECLARE @T Table (col1 varchar(50))
- -- @Array is the array we wish to parse
- -- @Separator is the separator charactor such as a comma
- DECLARE @separator_position INT -- This is used to locate each separator character
- DECLARE @array_value VARCHAR(1000) -- this holds each array value as it is returned
- -- For my loop to work I need an extra separator at the end. I always look to the
- -- left of the separator character for each array value
- SET @array = @array + @separator
- -- Loop through the string searching for separtor characters
- WHILE PATINDEX('%' + @separator + '%', @array) <> 0
- BEGIN
- -- patindex matches the a pattern against a string
- SELECT @separator_position = PATINDEX('%' + @separator + '%',@array)
- SELECT @array_value = LEFT(@array, @separator_position - 1)
- -- This is where you process the values passed.
- INSERT into @T VALUES (@array_value)
- -- Replace this select statement with your processing
- -- @array_value holds the value of this element of the array
- -- This replaces what we just processed with and empty string
- SELECT @array = STUFF(@array, 1, @separator_position, '')
- END
- RETURN
- END
- SELECT * FROM dbo.fnParseArray('a,b,c,d,e,f', ',')
- and ( {1}==0 or b.CompanyId in ({2},{3},{4},{5},{6}) )
- int origCount = idList.Count;
- if (origCount > 5) {
- throw new Exception("You may only specify up to five originators to filter on.");
- }
- while (idList.Count < 5) { idList.Add(-1); } // -1 is an impossible value
- return ExecuteQuery<PublishDate>(getValuesInListSQL,
- origCount,
- idList[0], idList[1], idList[2], idList[3], idList[4]);
- DECLARE @InputString varchar(8000) = 'ruby,rails,scruffy,rubyonrails'
- SELECT @InputString = @InputString + ','
- ;WITH RecursiveCSV(x,y)
- AS
- (
- SELECT
- x = SUBSTRING(@InputString,0,CHARINDEX(',',@InputString,0)),
- y = SUBSTRING(@InputString,CHARINDEX(',',@InputString,0)+1,LEN(@InputString))
- UNION ALL
- SELECT
- x = SUBSTRING(y,0,CHARINDEX(',',y,0)),
- y = SUBSTRING(y,CHARINDEX(',',y,0)+1,LEN(y))
- FROM
- RecursiveCSV
- WHERE
- SUBSTRING(y,CHARINDEX(',',y,0)+1,LEN(y)) <> '' OR
- SUBSTRING(y,0,CHARINDEX(',',y,0)) <> ''
- )
- SELECT
- *
- FROM
- Tags
- WHERE
- Name IN (select x FROM RecursiveCSV)
- OPTION (MAXRECURSION 32767);
- declare @x xml
- set @x='<items>
- <item myvalue="29790" />
- <item myvalue="31250" />
- </items>
- ';
- With CTE AS (
- SELECT
- x.item.value('@myvalue[1]', 'decimal') AS myvalue
- FROM @x.nodes('//items/item') AS x(item) )
- select * from YourTable where tableColumnName in (select myvalue from cte)
Add Comment
Please, Sign In to add comment