Access query if then argument - ms-access

The argument I'm looking for is, if the Members.Status field is equal to LA and the Member.Isresident field is False then the Members.Locality field will fill "LOST" in that field. I attempted to write it this way and am receiving an error of invalid syntax.
Locality: Iif ([Members.status] = "LA" and ([isresident] "False", [members.locality], "LOST")

The parentheses in your example are unbalanced. There are two ( but only one ).
Add an equal sign between [isresident] and "False". And if isresident is Yes/No data type, eliminate the quotes around False.
Re-using the field name as the alias for an expression can get you into trouble. You can avoid trouble there with a different alias such as adjusted_locality instead of Locality. But if you prefer to keep Locality as the alias, bracket it as in the example below.
Since I don't know about the context where you're attempting to use that expression, I'll suggest you try this simple SELECT to work out the syntax of the IIf expression.
SELECT
IIf(m.Status="LA" And m.isresident=False, m.locality, "LOST") AS [Locality]
FROM Members AS m;
You can create a new query, switch to SQL View, paste in that SELECT statement, and then run it to see whether any errors remain.

Related

ISNULL(Column1,Column2) equivalent in MS Access?

I am trying to get something equivalent to ISNULL in Microsoft Access.
I have the NZ function, but I'm not sure how to make it accept a Column name.
SELECT[BANK],[AMOUNT] FROM[BANKDETAILS] WHERE [BANK] = NZ('SBI', BANK]) AND[CITY]=NZ('Delhi', CITY) AND[IFSC]=NZ(4363,IFSC)
How can I achieve my goal?
Just use it like you would IsNull. You're currently reversing the arguments.
The first argument is the column that could be null, the second is the value if it is null:
SELECT [BANK],[AMOUNT] FROM [BANKDETAILS] WHERE [BANK] = NZ([BANK],'SBI') AND[CITY]=NZ(CITY, 'Delhi') AND [IFSC]=NZ(IFSC, 4363)
Do note that comparing columns to themselves is silly, and an ineffective way to test if they're not null. The Nz here has no value.

how to escape single quotes in paramterised query

I have a problem. i am passing parameter in query using IN clause, but it is giving me error because instead of considering it as a two different values it is considering it as a one single value, so how can i handle this on sql level?
select * from table where name in ('abc','xyz'); this will work fine.
instead of this it is considering it as a
select * from table where name in ('abc,xyz'); this will give error
my parameter values are as below
basically i am checking conditional filtering. that why written this logic if values are there then consider this parameter in where clause or else ignore it.
case when '${thera}' <> '''' then
( ccp.name in ('${thera}') ) ELSE 1=1 END
Note : for avoiding conditional filtering any better approach is their then please suggest that to.
You can indeed pass a list of values as a parameter. But you need to use a Custom Parameter component.
For your example, you can create a Custom Parameter named parThera, and then for initial value you can use the expression as you would do in javascript:
['abc','xyz']
Another option is to use a function expression, like:
function (){
return "A list of words".split(" ");
}
Then, on the SQL query, add this parameter, using thera as argument name, and the parameter parThera as value.
Now you can use it inside the SQL like in your first example:
SELECT * FROM TABLE WHERE name IN (${thera})
The parameter will be expanded to 'abc','xyz', thus making it usable inside the IN (...) clause.
Note: on the SQL parameters setup, you need to specify that this parameter is of type StringArray. Otherwise it will not expand correctly.

cfm websql queries error

I have this websql script (http://pastebin.com/gvJseBAn) which doesn't perform correctly.
If I run the statement select * from news where id=0772348890 , I get the error The conversion of the varchar value ' 0017707787068' overflowed an int column.
If I run the statement select * from news where id='0772348890' , I get the error Incorrect syntax near '0772348890'.
If I run the statement select * from news where id="0772348890" , I get Invalid column name '0772348890'
Any other variation of '#0772348890#' or #0772348890# or "#0772348890#" I have tried gives the error "incorrect column" or "incorrect syntax near ..."
Any ideas on how to fix this error, or a better method of creating a simple websql query form?
A) the issue here is that db column will not under any conditions accept "0772348890" as a valid input because it is mismatched. The column is an "int" type (according to your first error), but your value has a padded 0 prependedto the front as in 0 772...
What is the purpose of this zero? Ordinarily prepended zeros appear in fixed length character fields where a space is not allowed. Should the value not be "772348890"?
B) Remember that ColdFusion will escape your single quotes in your query. In your second error example (where you use single quotes), this code:
<cfquery name="runsql" datasource="#Form.datasource#" timeout="30">
#Form.sql#
</cfquery>
Produces this SQL statement:
select * from news where id=''0772348890''
Which would give you your syntax error. If you wish to successfully test your second example you will need to alter your code to:
<cfquery name="runsql" datasource="#Form.datasource#" timeout="30">
#preservesinglequotes(Form.sql)#
</cfquery>
Preservesinglequotes() gets you past the second error issue and MSSQL's implicit conversion may strip off the prepended zero and allow the query to succeed - though I'm not sure will give you what you want.
C) Finally you should probably never do what you are trying to do - at least not in this fashion (sorry to be so direct!). Your opening up your DB to arbitrary queries from a web form. The resulting damage from even casual mistakes could be catastrophic to your data, let alone a malicious user bent on stealing or altering or using your site for malicious purposes. That's my take. :)

Could this simple T-SQL update fail when running on multiple processors?

Assuming that all values of MBR_DTH_DT evaluate to a Date data type other than the value '00000000', could the following UPDATE SQL fail when running on multiple processors if the CAST were performed before the filter by racing threads?
UPDATE a
SET a.[MBR_DTH_DT] = cast(a.[MBR_DTH_DT] as date)
FROM [IPDP_MEMBER_DEMOGRAPHIC_DECBR] a
WHERE a.[MBR_DTH_DT] <> '00000000'
I am trying to find the source of the following error
Error: 2014-01-30 04:42:47.67
Code: 0xC002F210
Source: Execute csp_load_ipdp_member_demographic Execute SQL Task
Description: Executing the query "exec dbo.csp_load_ipdp_member_demographic" failed with the following error: "Conversion failed when converting date and/or time from character string.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
End Error
It could be another UPDATE or INSERT query, but the otehrs in question appear to have data that is proeprly typed from what I see,, so I am left onbly with the above.
No, it simply sounds like you have bad data in the MBR_DTH_DT column, which is VARCHAR but should be a date (once you clean out the bad data).
You can identify those rows using:
SELECT MBR_DTH_DT
FROM dbo.IPDP_MEMBER_DEMOGRAPHIC_DECBR
WHERE ISDATE(MBR_DTH_DT) = 0;
Now, you may only get rows that happen to match the where clause you're using to filter (e.g. MBR_DTH_DT = '00000000').
This has nothing to do with multiple processors, race conditions, etc. It's just that SQL Server can try to perform the cast before it applies the filter.
Randy suggests adding an additional clause, but this is not enough, because the CAST can still happen before any/all filters. You usually work around this by something like this (though it makes absolutely no sense in your case, when everything is the same column):
UPDATE dbo.IPDP_MEMBER_DEMOGRAPHIC_DECBR
SET MBR_DTH_DT = CASE
WHEN ISDATE(MBR_DTH_DT) = 1 THEN CAST(MBR_DTH_DT AS DATE)
ELSE MBR_DTH_DT END
WHERE MBR_DTH_DT <> '00000000';
(I'm not sure why in the question you're using UPDATE alias FROM table AS alias syntax; with a single-table update, this only serves to make the syntax more convoluted.)
However, in this case, this does you absolutely no good; since the target column is a string, you're just trying to convert a string to a date and back to a string again.
The real solution: stop using strings to store dates, and stop using token strings like '00000000' to denote that a date isn't available. Either use a dimension table for your dates or just live with NULL already.
Not likely. Even with multiple processors, there is no guarantee the query will processed in parallel.
Why not try something like this, assuming you're using SQL Server 2012. Even if you're not, you could write a UDF to validate a date like this.
UPDATE a
SET a.[MBR_DTH_DT] = cast(a.[MBR_DTH_DT] as date)
FROM [IPDP_MEMBER_DEMOGRAPHIC_DECBR] a
WHERE a.[MBR_DTH_DT] <> '00000000' And IsDate(MBR_DTH_DT) = 1
Most likely you have bad data are are not aware of it.
Whoops, just checked. IsDate has been available since SQL 2005. So try using it.

Replace IIf with SWITCH in WHERE clause

I have the following query in MS-Access 2003 and it works OK:
SELECT tblDiscounts.DiscountID, tblDiscounts.DiscountPercent, tblDiscounts.DiscountName, tblDiscounts.DiscountDescription
FROM tblDiscounts, qryPropertyPeriodRate_Count_Nested
WHERE (tblDiscounts.DiscountID) = IIf ([qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]=1,1,IIf([qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]=2,2,IIf([qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]=3,3,4)));
I wish to replace the IIf function with the Switch function but whatever I tried didn't work. My best approach is the following:
SELECT tblDiscounts.DiscountID, tblDiscounts.DiscountPercent, tblDiscounts.DiscountName, tblDiscounts.DiscountDescription
FROM tblDiscounts, qryPropertyPeriodRate_Count_Nested
WHERE (((tblDiscounts.DiscountID)=SWITCH ([qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]=1,1, [qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]=2,2, [qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]=3,3, [qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]>3,4)));
but I get a message
Type mismatch in expression
Please advise!
One difference I can see is that if [qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]<1 the nested IIfs will return 4 while the Switch statement will return Null. Check your underlying data to see if that could happen; a Null value might very well mess up the WHERE clause.