Return True if specific value exists in table - sql - sql

I want to create an SQL query that will return True if a specific value exists in a specific column; if not, then it will return False.
I know that I can create something like 'SELECT something FROM somewhere WHERE something'. In this case I don't want to select anything, just to check.
My question is how can I do it.

You can use the IIf function:
SELECT IIf(something = 'Some value', True, False) FROM somewhere;

In Access, you can use a DCount expression to count the number of rows where your something field contains 'some value'. Here is an example copied from the Immediate window:
Debug.Print DCount("*", "somewhere", "something='some value'")
1
Debug.Print DCount("*", "somewhere", "something='BOGUS'")
0
You could adapt that to give you True when the count is greater than zero or False for count of zero:
Debug.Print (DCount("*", "somewhere", "something='some value'") > 0)
True
Debug.Print (DCount("*", "somewhere", "something='BOGUS'") > 0)
False
If you want to do it from a query, this one will return -1 for True and zero for False:
SELECT (DCount("*", "somewhere", "something='some value'") > 0) AS value_exists;
Or you could use a Format expression to display those values as strings: "True"; or "False":
SELECT Format(DCount("*", "somewhere", "something='some value'") > 0, 'True/False') AS value_exists;

As the name implies, DLookup is for this:
SomevalueExists = Not IsNull(DLookup("Id", "somewhere", "somefield = somevalue"))

try this:
select case when x is null then false else true end
from (select max(somecol) x
from sometable
where somecol = somevalue) a

just use
select count(*) from tableName where columnName = '$variableInput';
if you plan on reusing this you might as well make it a prepared statement that you can call upon through whichever interface you design to work with your database. If the returned value is greater than zero you know it to be true, for instance
if(preparedStatement($variableInput)>0)
{
$flag = true;
}
else
{
$flag = false;
}

Related

read comma separated integers in INI file

I have a INI file that looks like this !
[columnNumber]
Number1=2,4
this is my code
the ini file is read using function defined here
the columberNumber is taken as string "2,4" i want to split this and pass it into my select case object mytmp looping for all values in Number1 pass it to mytmp in select
columnNum = ReadIni(file, "columnNumber", "Number1")
mytmp = columnNum
x = Split(mytmp, ",")
For k = 0 To UBound(x)
'mytmp1 = Split(array_colnum, ",")
'mytmp2 = Search(array_col)
'mytmp1 = x(k)
'mytmp2 = x(k)
Next k
this is my select case
select case i...<does something>
select case mytmp
Could somebody guide me in doing this!
Updated : I want to put in select case i the values got from [columnNumber] my ReadIni function reads from [columnNumber section] the number1={2,4} i want to split this and store in a variable, the variable is read from select case variable
It's not completly clear what exactly you trying to do...
what is "I" variable? what it should contain?
mytmp = "2,4"
x = Split(mytmp, ",")
For k = LBound (x) To UBound (x)
'## Get value and store in variable "I".
i = x (k)
Select Case i
Case "2"
Response.Write "yep, it's 2"
Case "4"
Response.Write "yep, it's 4"
End Select
Next

CharIndex exact match?

I have a parameter being passed into a usp that is a varchar(max). This parameters is something like the following:
'1,20,3,40,0'
'1,5,30,0,5,9'
'0,50,40,8,9'
'1,10,2,3,4'
I need to figure out if the parameter has a value of just 0 in it anywhere. I was going to use:
CASE WHEN CHARINDEX('0',#DispIDs) > 0
THEN convert(bit,1)
ELSE convert(bit,0)
END
But this still returns a true on values like 40, 30, etc. I only want to mark it as true if the param contains the 0. So the last example would return false, the others would return true.
Search for it surrounded with commas, and wrap the initial string with commas to match (to cover 0 at the ends of the string):
CASE WHEN CHARINDEX(',0,',',' + #DispIDs + ',') > 0 THEN convert(bit,1) ELSE convert(bit,0) END

Optional conditions in postgreSQL query

I need to create a procedure with optional arguments and use them only if they are not null
My current query looks like:
SELECT * FROM sth WHERE
(arg1 IS NULL OR sth.c1 = arg1) AND
(arg2 IS NULL OR sth.c2 = arg2) AND
(arg3 IS NULL OR sth.c3 > arg3) AND
(arg4 IS NULL OR sth.c4 < arg4)
I'm thinking of a way to make it look better / shorter. My first shot is:
SELECT * FROM sth WHERE
COALESCE(sth.c1 = arg1, 't') AND
COALESCE(sth.c2 = arg2, 't') AND
COALESCE(sth.c3 > arg3, 't') AND
COALESCE(sth.c4 < arg4, 't');
but I'm not sure if this looks any better. Do you know any useful tricks for this?
Keep it the way it is. Using coalesce will prevent the query planner from doing its job properly, and you'll end up with sucky query plans.
Best I'm aware, the following expressions will use a btree index:
col = 'val'
col is null
The following expressions will not use a btree index:
col is [not] 'val'
(col = 'val') is [not] <true | false | null>
col is [not] distinct from 'val'
coalesce(col, 'val') = 'val'
coalesce(col = 'val', <true | false | null>)
Ok, I think that this query is the best idea for this purpose
SELECT * FROM sth WHERE
NOT (sth.c1 = arg1) IS FALSE AND
NOT (sth.c2 = arg2) IS FALSE AND
NOT (sth.c3 > arg3) IS FALSE AND
NOT (sth.c4 < arg4) IS FALSE;
it doesn't use any functions so the query planner should work fine just as before
it just uses simple expressions where:
1.
true = true // true
true = false // false
true = null // null
2.
false is false // true
true is false // false
null is false // false
3.
not true // false
not false // true
so it will return true if expression is true OR null

Output/Print variable, in code, in if statement

I am trying to achieve the following:
The user is shown an excel spread sheet with a list of assumption which they can change.
Title | Value |
Input01 | 10 | =
Input02 | 2 | >=
Input03 | 800 | >=
Input04 | 4 | >=
Input05 | 2 | <=
There is an If .. Then Statement that pulls in data if the assumption are met. However if an assumption is blanc, it should not be included in the If .. Then Statement.
If x = Input01Value And y >= Input02Value _
And z >= Input03Value And a >= Input04Value _
And b <= Input05Value Then
User ommits Input03
If x = Input01Value And y >= Input02Value _
And a >= Input04Value And b <= Input05Value Then
Now I could check to see if each value exist, and then follow it by another If statement with the appropriate variables. But this seems a bit redundant.
I was wondering if something like the following is possible:
Input 01 = ""
If Input01Value != "" Then Input01 = "x = " & Input01Value
'Then use join or something similar to join all of them ..
And Then use this Input01 directly in the If .. Then statement. This way when a variable is empty the And .. are not included and the If statement will not fail.
Eg. (I know this doesn't work, just illustrating the scenario)
VBA: If Input01 Then
Result while compiling: If x = Input01Value Then
Please Note, I know I could do something like the following:
If Boolean And Variable2 > 4 Then and then have Boolean and Variable2 populate with a value in the cell, however the issue with this is that if the user, for example, decides to omit the Variable2 (which is reasonable) it will fail. eg. If (Boolean = True) And > 4 Then.
Hope my question is clear, thanks for the help.
What about using a function which operates on a select case depending on a string operator and two values?
Function conditionalString(condition As String, x As Variant, y As Variant) As Boolean
Select Case condition
Case "="
If (x = y) Then
conditionalString = True
Else
conditionalString = False
End If
Exit Function
Case ">="
conditionalString = (x >= y)
Exit Function
Case "<="
conditionalString = (x <= y)
Exit Function
Case ">"
conditionalString = (x > y)
Exit Function
Case "<"
conditionalString = (x < y)
Exit Function
Case Else
conditionalString = False
End Select
End Function
You could then just have another function, say "check if value isn't blank" before calling all assumptions.
Expanding on my comment, you can use something like this to test each row of input.
Function TestIt(testValue,inputOperator,inputValue) As Boolean
If Len(inputValue)=0 Then
TestIt=True 'ignore this test: no value supplied
Else
TestIt=Application.Evaluate(testValue & inputOperator & inputValue)
End If
End function

Refactoring of Case Statement and Dynamic Operator

I have a case statement which compares data by getting parameter such as "eq,ne,gt,..." and so on. Actually this method will filter data by getting filter parameter and filter value. How can I refactor below code? Thanks.
For iRow As Integer = 1 To ......
.......
columnData = rowData(HeaderIndex)
Case "eq"
If Not (columnData = filterData) Then
arrayDel(iRow) = True
droppedRows += 1
End If
Case "ne"
If Not (columnData <> filterData) Then
arrayDel(iRow) = True
droppedRows += 1
End If
Case "gt"
If Not (columnData > filterData) Then
arrayDel(iRow) = True
droppedRows += 1
End If
Case "ge"
If Not (columnData >= filterData) Then
arrayDel(iRow) = True
droppedRows += 1
End If
One way might be to have a function that took your string and the column and filter data and simply returned the relevant boolean value.
Case "eq"
return (columnData = filterData)
Case "ne"
return (columnData <> filterData)
Case "gt"
return (columnData > filterData)
Case "ge"
return (columnData >= filterData)
This is a lot easier to look at and leaves out the duplicated lines (to be called now in just one place after calling our new function).