SQL finding value - sql-server

I would like to find F20300000000 in this string:
0xE90300000000EA0300000000EB0300000000EC0300000000ED0300000000EE0300000000EF0300000000F00300000000F10300000000F20300000000F30300000000F40300000000F60300000000F70300000000E90B00000000010C000000000D0C000000003E0C000000005E0C000000005F0C00000000630C00000000811B000000008B1B00000000951B000000009F1B00000000A91B00000000B31B00000000BD1B00000000C71B00000000
I've used already the wildcard like
LIKE '%F20300000000%' then I didn't get any result.
to make it clear, when my condition is true then it will show the name of the person who has the F20300000000 in their field, so my problem now is it seems I don't know how to find F20300000000 from the given value.
my query:
select C.Name
FROM
[SERVER01].[dbo].[character_table] AS C,
[SERVER01].[dbo].[achievement] AS T
WHERE C.CharacterIdx = T.CharacterIdx and T.AchievementData LIKE '%F20300000000%';
AchievementData Data Type is varbinary(4800)

Your column is likely to be binary so you should cast it to string:
select C.Name
FROM
[SERVER01].[dbo].[character_table] AS C,
[SERVER01].[dbo].[achievement] AS T
WHERE C.CharacterIdx = T.CharacterIdx and CONVERT(varchar(max), T.AchievementData, 2) LIKE '%F20300000000%';

Related

Find rows where the value contains a word from a list of varchar(50)

I am collecting data from a t-sql stored procedure to import into c# program. I would like to narrow down the data first.
I have data that has three field that describes the three values that follows them. I need to find only the fields that have one of a dozen keywords in the description.
I was using something that UNION all the fields with values, then
...
AND (
TEXT1234.AccountValue LIKE '%word1%'
OR TEXT2345.AccountValue LIKE '%word1%'
OR TEXT3456.AccountValue LIKE '%word1%'
OR TEXT1234.AccountValue LIKE '%word2%'
OR TEXT2345.AccountValue LIKE '%word2%'
OR TEXT3456.AccountValue LIKE '%word2%'
OR TEXT1234.AccountValue LIKE '%word3%'
OR TEXT2345.AccountValue LIKE '%word3%'
OR TEXT3456.AccountValue LIKE '%word3%'
...
Now I am trying to do something like this:
declare #wordList table (Word varchar(50))
insert into #wordList values ('%word1%'),('%word2%'),('%word3%')...
...
SELECT *
FROM [DB1].dbo.Table_info
WHERE Account = 'TEXT1234'
AND AccountValue LIKE (SELECT * FROM #wordList)
...
(This is one of a number of similar UNION pieces. Which is why I would like to use a list of words, to shorten the code, and contain the words in one place in case of future changes.)
OR something Like:
SELECT *
FROM [DB1].dbo.Table_info
WHERE Account = 'TEXT1234'
AND AccountValue CONTAINS (SELECT * FROM #wordList)
...
Expected output:
TEXT1234_Account TEXT1234_AccountValue Acct1234 Acct1234_Value
TEXT1234 word3 something something ACCT1234 48
TEXT3456_Account TEXT3456_AccountValue Acct3456 Acct3456_Value
TEXT3456 Something word1 something ACCT3456 48
Please let me know if you need more code to analyze this...
(I am failing at keeping this brief already.)
Use EXISTS:
SELECT *
FROM [DB1].dbo.Table_info tbl
WHERE tbl.Account = 'TEXT1234'
AND EXISTS
(
SELECT 0
FROM #wordList wl
WHERE tbl.AccountValue LIKE wl.Word
)

Search for a certain name followed by any number (or no number)

I want to search for a field matching a name followed by a number of variable size (including no number), but I can't see how to use the wildcards on PATINDEX and LIKE to detect an unknown number of digits.
This is the regexp I would like to check : MYNAME[1-9]*
It has to recognize MYNAME, MYNAME5, MYNAME12, MYNAME275, ...
It shouldn't recognize ANOTHERNAME, MYNAMEXX12, MYNAME12X5, MYNAME12X
PATINDEX and LIKE don't recognize the * on the regexp to indicate a variable number of digits.
Do you know of a way to search for a pattern where a part has a variable size ?.
Thank you.
DECLARE #Test TABLE (
Field VARCHAR(32)
)
INSERT #Test( Field )
VALUES
('MYNAME'),
('MYNAME5'),
('MYNAME12'),
('MYNAME275'),
('MYNAME275TEXT')
SELECT *
FROM #Test
WHERE (
( Field = 'MYNAME'
OR Field LIKE 'MYNAME[0-9]%'
)
AND Field NOT LIKE 'MYNAME[0-9]%[^0-9]%'
)
Bit of a wild card (geddit?) guess, however, perhaps this?
SELECT *
FROM (VALUES('MYNAME'),('MYNAME5'),('MYNAME12'),('MYNAME275'),('MYNAME654A'))V(N)
WHERE V.N = 'MYNAME'
OR (V.N LIKE 'MYNAME[0-9]%'
AND V.N NOT LIKE 'MYNAME[0-9]%[^0-9]');

mysql's ORDER BY FIELD equivalent in Solr

I'm using Solr 5.2. Is there a parameter that let you sort the order of the returned result by the specific field value? For example, in mysql I use ORDER BY FIELD to sort the result in specific order:
SELECT id,txt FROM `review`
order by FIELD(a.id,2,3,5,7) ;
I have read the sort section in the document but it doesn't seem to have any mention of a similar parameter.
I'm not sure Solr can do exactly what you want. The closest you might get is a range query. A range query looks like this:
your_field:[valueA TO valueB]
You can achieve custom sort in solr using ^=
Locate Constant Score with ^= in https://cwiki.apache.org/confluence/display/solr/The+Standard+Query+Parser
q=id:(2^=4 3^=3 5^=2 7^=1)
You can run
array = [2,3,5,7]
var string = "q=id:(";
for(i=0;i<array.length;i++){
string += array[i]+"^=" + (array.length-i) + " ";
}
string+=")";
// string => q=id:(2^=4 3^=3 5^=2 7^=1)

Dapper: How to get value from DapperRow if column name is "count(*)"?

I have a dynamic result from Dapper query that contains records like this:
{DapperRow, billing_currency_code = 'USD', count(*) = '6'}
I'm able to access 'USD' by using rowVariable.billing_currency_code
To get '6' value I tried rowVariable["count(*)"] and rowVariable.kv["count(*)"] and unfortunately nothing works...
I can't change the count(*) column name in my case
How to get the '6' value from the rowVariable of type DapperRow in such case?
If the column name genuinely is "count(*)", then you can cast the row to a dictionary:
var data = (IDictionary<string,object>)row;
object value = data["count(*)"];
For that to work (at least, in SQL Server), your query would need to be something like:
select count(*) as [count(*)]
However, in most cases the column doesn't have a name, in which case: fix your query ;p
Actually, I'd probably say fix your query anyway; the following would be much easier to work with:
select count(*) as [Count]
Suppose Your Data as below
var Details={DapperRow, billing_currency_code = 'USD', count(*) = '6'}
as The columns is coming dynamically
var firstRow= Details.FirstOrDefault();
To get the heading columns of the data
var Heading= ((IDictionary<string, object>)firstRow).Keys.ToArray();
To get the value of the data by using key
var details = ((IDictionary<string, object>)firstRow);
var vallues= details[Heading[0]];

Using a function in a query that returns a string or a null

I want to join 2 tables 'addresses' and 'user_info' on user_id and app_id
(which is a number, or it is null), like these 2 examples:
select * from user_info
left outer join addresses on addresses.user_id = user_info.user_id
and addresses.app_id is null
select * from user_info
left outer join addresses on addresses.user_id = user_info.user_id
and addresses.app_id = 1234
What the app_id should be is complicated and I have written a function to return it. It returns a string, which would be for example "is null" or "= 1234".
I'm trying to call it with this syntax:
select * from user_info
left outer join addresses on addresses.user_id = user_info.user_id
and addresses.app_id dbo.fnGetAppId(addresses.user_id)
I get this error:
Msg 4145, Level 15, State 1, Line 3 An
expression of non-boolean type
specified in a context where a
condition is expected, near 'dbo'.
I'd like to keep the query very simple as it is without having to determine if the function is returning a null or not.
Can you suggest the best way to keep the calling query very simple?
(I'm in sql server 2005.)
NULL != NULL. If either address.app_id = NULL or fnGetAppID = NULL, the comparison will fail. I would write the comparison as:
coalesce(address.app_id, 'NULLSTRING') = coalesce(dbo.fnGetAppID(addresses.user_id), 'NULLSTRING')
It looks like you're just missing an = sign
addresses.app_id dbo.fnGetAppId(addresses.user_id)
rather than
addresses.app_id = dbo.fnGetAppId(addresses.user_id)
So if fnGetAppId is null then this query looks like the following?
select * from user_info left outer join addresses on addresses.user_id = user_info.user_id and null
I doubt that is what you want. :)
You may want to do a simple check in your logic, before calling the query, to properly handle a null for fnGetAppId and as Clyde mentioned you also need an = sign for a non-null
.
As James Black pointed out, you have AND where you probably want WHERE. Beyond this, I suggest you make the function a boolean one (passing address.app_id to it as one more argument), so it can perform an IS NULL or = 1234 as appropriate (Bill's COALESCE solution is clever indeed, but putting the appropriate comparison inside the function is more straightforward, IMO).

Resources