query with IIF statement - ms-access

I am responsible for producing a set of name badges for an upcoming class reunion. Have everything set up and have been able to produce the correct output for those attending the reunion.
What I want to do is place parenthesis around the maiden names of the married women attending the reunion. Right now the maiden name is being displayed on the badge, but without parenthesis.
I have placed the following expression in the MaidenName field of the query I have written, but nothing is happening, at least this expression did not produce any error messages when I ran it.
IIf([MaidenName]="IsNull",[MaidenName]=" ",([MaidenName]=("("+[MaidenName]+")")))
When I entered the expression the first time the IsNull was without quotes. When I ran the
the query the quotes were place around the IsNull statement. The query ran, but there were no parenthesis around the maiden name on the output.

You would probably like to use the IsNull() function, and clean a little bit that Iif syntax:
IIf(IsNull([MaidenName]), " ", "(" & [MaidenName] & ")")

I recommend using the SQL-native is null comparison instead of the IsNull() function. A SQL-native way will always be faster and more portable than a VBA function.
iif(MaidenName is null, '', ' (' & MaidenName & ')')
Plus I think it's easier to read.

Related

type error when trying to update a table field name query

I have a query where i am trying to update field by taking the left number of characters before a space.
I'm not very good with Access VBA, so I'm trying to do this via a query.
my data is a list of SKUs, where I want to update the same field (sku) with a shorter SKU number, by using the Left$ function along with the InStr function to take all characters to the left of a space in the number.
test sku
E349CAJ6 OBROBRO
E357CAJ6 OBROSID
E329CAJ6 OWHIBRO
E358CAJ6 ONO SID
Note that the space isn't always in position 9, sometimes it varies. I was trying to use the following Query update value: Left$([IMPORT - EFF ORDERS]![SKU], InStr([IMPORT - EFF ORDERS]![SKU]," ",1))
The InStr, identifies the starting position based on the space, to use for the Left function.
The SKU field is a Short Text type field.
However, when I run the query, I get a "Type Conversion" error and none of the records will update.
I have wracked my brain to try to figure this one out and would appreciate an expert's fresh eyes on it.
Thank you so much in advance !
The ,1 is in wrong argument, really don't need it. If you want to use Compare argument then also use Start argument. Without explicit Start and Compare parameters, function will use defaults.
Left([IMPORT - EFF ORDERS]![SKU], InStr(1, [IMPORT - EFF ORDERS]![SKU], " ", 1))
InStr() in this case is returning a value that will be used as a length parameter, not a starting position. Start position for Left is first character.

How to use "IS" and "=" in MySQL in the right way

Trying to figure out when to use "is", "=" since it seems like these two don't work the same way in SQL.
I've tried to switch these two in different commands, and I thought I'd receive the same result, but seems like SQL would only recognize one of them with particular functions
WHERE event_date **=** '2013-12-22'
And I tried to use "IS" instead of "=" for the above command
WHERE event_date **IS** '2013-12-22'
Then I got an error code, also in another practice question. I wanted to use "IS" before "BETWEEN", that failed too.
WHERE
affected_customers BETWEEN 50000
AND 150000
I tried to put
WHERE
affected_customers IS BETWEEN 50000
AND 150000
IS word cannot just be added before another operator.
IS operator is used for checking value against boolean
IS NULL and IS NOT NULL are used for checking values against NULLs.
SQL operators don't necessarily need to be use like how you construct an English sentence.
IS operator is usually use with the NULL operator, hence IS NULL and IS NOT NULL - used to checked if a value is NULL.
affected_customers BETWEEN 50000 AND 150000 is enough and logical, you'll get use to it as you go along with your programming life.
Best of luck to your journey!

What is the purpose of using WHERE COLUMN like '%[_][01][7812]' in SQL statements?

What is the purpose of using WHERE COLUMN like '%[_][01][7812]' in SQL statements?
I get some result, but don't know how to use properly.
I see that it is searching through the base, but I don't understand the pattern.
Like selects strings similar to a pattern. The pattern you're looking at uses several wildcards, which you can review here: https://www.w3schools.com/SQL/sql_wildcards.asp
Briefly, the query seems to ba matching any row where COLUMN ends in an _ then a 0 or a 1, then a 7,8,1, or 2. (So it would match 'blah_07' but not 'blah_81', 'blah_0172', or 'blah18')
First thing as you might be aware that where clause is used for filtering rows.
In your case (Where column Like %[_][01][7812]) Means find the column ending with [_][01][7812] and there could be anything place of %
declare
#searchString varchar(50) = '[_][01][7812]',
#testString varchar(50) = 'BeginningOfString' + '[_][01][7812]' + 'EndofString'
select CHARINDEX(#searchString, #testString), #testString, LEN(#testString) as [totalLength]
set #testString = '[_][01][7812]' + 'EndofString'
select CHARINDEX(#searchString, #testString), #testString, LEN(#testString) as [totalLength]
set #testString = 'BeginningOfString' + '[_][01][7812]'
select CHARINDEX(#searchString, #testString), #testString, LEN(#testString) as [totalLength]
Although you've tagged your post MySQL, that code seems unlikely to have been written for it. That LIKE pattern, to me, resembles Microsoft SQL Server's variation on the syntax, where it would match anything ending with an underscore followed by a zero or a one, followed by a 7, an 8 a 1 or a 2.
So your example 'TA01_55_77' would not match, but 'TA01_55_18' would, as would 'GZ01_55_07'
(In SQL Server, enclosing a wildcard character like '_' in square brackets escapes it, turning it into a literal underscore.)
Of course, there may be other RDBMSes with similar syntax, but what you've presented doesn't seem like it would work on the data you've got if running in MySQL.

Hyphen in fieldname causes INSERT statement in MS-Access to fail

I've been given an MS-Access application to maintain and being more acquainted with Oracle as dbms I bump into issues now and then..
Today it looks like MS-Access has problems when a hyphen is used for a column name...
The following insert statement was coupled to the NotInList event to add an extra entry to a listbox.
INSERT INTO tblProductInfo ( ProductInfo-Product )
"SELECT """ & NewData & """ AS ProductInfo-Product;"
But it's not working (anymore? first time the issue is reported, not sure if the original developer tested it out).
I've tested it out with a single-record append query and it looks like the hyphen is the culprit and I just cannot find a way to escape that..
INSERT INTO tblProductInfo ( ProductInfo-Product ) VALUES ("myData")
The error given is "Syntax error in INSERT INTO statement"
There does not seem any other way to specify the MS-Access fieldname, is it? (square brackets are only used for SELECT statements,
So... I'm calling for the wisdom of the Stackoverflow gods and am hoping someone knows how to solve this...
Thanks in advance !!
You need square brackets on that:
"INSERT INTO tblProductInfo ([ProductInfo-Product]) Values (""" & NewData & """)"
Or better yet, avoid odd characters and spaces in field and table names.
Square brackets are used in any sql statement where the field or table name is problematical:
It is a reserved word
It contains a space
It includes a special character
You can even use them with DDL:
Create Table tblProductInfo ( [ProductInfo-Product] Text(50))

how to delete everything after a blank space in access

I have a table->column first-name with values:
Juan
Manuel l.
Richard Wit
I'm trying to do a query that return
Juan
Manul
Richard
I want to eliminate after a space.
Assuming you want SQL, try
SELECT MID(name + " a", 1, INSTR(name + " a", " ")-1) AS FirstName FROM myTable
In VBA it would be similar, since Mid() and InStr() work exactly the same as in Access-SQL
Really, though, it would be better to just store first and last name in separate fields so you don't have to do this sort of finagling in the SQL.
SELECT Left([First-Name], InStr([First-Name] & ' ', ' ') - 1) As CleanFirstName
FROM table
Keep in mind that running this will be inefficient since you will be executing VBA functions against every record in your query. Depending on how you are using this, you may want to have the query return the full field and do the processing after the fact.