I need a query in Access with the following:
If Column2, Cell("B1") = "A1" and
Column 1, Cell ("A1")= "B1_A1_DTTROB"
THEN Col3 NEEDS TO SHOW "Contains" or "Correct"
Thanks
As #Minty said - Access understands fields and records. It hasn't got a clue about cells, columns or rows. You can, if you must, think of a field as a column and a record as a row - but you will more than likely get corrected every time you say it out loud.
Saying that, I can see you're trying to compare your first field with the second field on the same record and if it's a match return the text.
SELECT IIF(Column2 = [Column 1],"Contains","Correct") AS Col3
FROM MyTableName
This query will return a table containing a single field called Col3. It will have the same number of records as your original table as it compares the two fields on each record.
To return other fields from your table just add them to the Select clause separated by a comma:
Select Column2, [Column 1], ....
Field names can contain spaces, but you have to wrap the field name in square brackets []. You can also use reserved words in fields, but again wrap them in square brackets [Name].... using spaces or reserved words can causes problems further down the line, so easier to use Column1 rather than [Column 1] and strName rather than Name.
In the field properties in the table design view there is a Caption property that lets you give the field an alias name, but I find this confusing when writing queries in SQL View - Design view will show the correct field name.
Related
I'm trying to make a mysql query that checks if a column in a row is contained within another column in the same row. Is there a way to do that kinda of query?
for example:
Key Value runHash
2500 tacos night.2500.293849284
1775 windows day.176555.43035842
I am trying to write a query that will return the second row and not the first because for the first row, Key is in runHash.
I tried to do:
select * from table where key not in runHash
However this doesn't appear to be valid for mysql.
You are looking for like:
where runHash like concat('%', key, '%')
You can put periods in the pattern as well, if those are important for your pattern matching.
I have a table with 20 fields.
1) Is there any way to check all fields if any of them are NULL?
2) For all columns that are nvarchar i want to search if they contain a constant string value e.g. "Missing".
Can i do the above in SSIS without having to check the columns one by one?
There is no Null-inator that would check if any column is null in a row. However, you could concatenate all the columns in the row and if any column is null then the result will be null. So in a derived column task it could look like this:
Field1 + Field2 + (DT_WSTR, 50)Field3...
As demonstrated in field3, all fields that are not string would need to converted.
Similarly, you can find a keyword, like "missing" by concatenating all the fields in a derived column task and using FINDSTRING(). That could looks like this:
FINDSTRING(Field1 + Field2, "Missing",1)
If the value is greater than 0, you have a hit. There a drawbacks though. Given that your columns are nullable, you would need null handling on all the columns unless this test was only performed on rows where there are no nulls. Also, it doesn't tell you which column has the value, so that probably is not very useful unless you are rejecting or quarantining the entire row.
I have a table with two fields, IDCopy and ID. I want to copy the value of ID into IDCopy because ID is a number field and I need a second copy of this field as a text field.
I am used to doing things like this on sql server
UPDATE table SET table.IDCopy= table.ID;
But when I try to run that query in access it asks me for the parameter value of ID. What is the syntax for setting one column in a table to another column in Access?
You can use CStr() to cast the ID number to text. This should work when IDCopy is text and ID is numeric.
UPDATE [table] SET IDCopy = CStr(ID);
I bracketed the table name because table is a reserved word.
If Access still thinks ID is a parameter with this query, then [table] does not include a field named ID.
I want to select everything in a table where the cell's value is not foo. I thought it would be something similar to WHERE NOT NULL, like SELECT * FROM database.table WHERE NOT 'foo';, but that just returned the column headers with no data.
edit: i want to do this without specifying a particular column (NOT 'foo' for all fields).
There's no way to do this without specifying the fields and conditions, as in
select * from table where
col1 != value and
col2 != value and
...
Depending on what you mean by
the cell's value is not foo
you may need to change and (none of the columns in a row match the condition) to or (at least one column does not match the condition).
As #Jim Garrison answers, you must specify the columns to compare your 'foo' value against. Otherwise you're testing a constant value itself, and that can be either false (zero) or true (nonzero).
SELECT * FROM database.table WHERE NOT 'foo' returns no data because 'foo' is a non-false constant value. Therefore NOT 'foo' is false for every row in the table, and so no rows match and the result is an empty set.
Jim gives one syntax for testing each column in turn. Here's another alternative syntax:
SELECT * FROM database.table WHERE 'foo' NOT IN (column1, column2, column3, ...)
However, I agree with the comment from #Mosty Mostacho. In a relational database, it's weird to test for a single value over many columns. Each column should be a different logical type of attribute, so it's uncommon to look for a similar value among many columns (not impossible, but uncommon).
It usually means you're using repeating groups of columns, which violates First Normal Form. Instead, you may need to create a child table, so that all the values you are searching are in a single column, over multiple rows.
It seems like MySQL is not sophisticated enough to do this. Instead I'll have to add an if-test in my php loop to check for foo and break if foo is present. I didn't want to do this as I have to get down into several nested loops before testing a cell's value…
Suppose I have a select query like :
SELECT * FROM tablename
and in the table are columns : field1, field2 and field3
Does anyone know if it's possible to get a resultset with only 1 row with 1 field, with comma separated values of the columns, like this :
"fieldvalue1, fieldvalue2, fieldvalue3"
Problem is that I don't know the column names of the table in advance...
Another problem is that a prepared statement is not the way to go, since all this should be done from within a trigger, and MySQL doesn't allow dynamic cursors/selects inside a trigger.
I have done some research and only came as far as GROUP_CONCATenating the column names correctly. But the problem is, that
SELECT (SELECT GROUP_CONCAT( cols.column_name) FROM (SELECT column_name FROM information_schema.columns WHERE table_name='test_table') as cols) FROM test_table
will return one and the same concatenated string containing the column names, once for each table row, instead of evaluating it as the column names for the outer select statement and returning the actual values.
From what I have read in all of the forums discussing this kind of question (and there were many), there is really no way of getting this to work without prepared statements.
I can only think of one other way to do this, and that would be having a dedicated column on each table, where you concatenate the individual column values on INSERT or UPDATE, so you can simply select this one field instead of the full set of fields.
Seems like you have 3 questions here:
Getting a resultset with 1 row, 1 field: MYSQL has a CONCAT_WS function that works like this:
SELECT CONCAT_WS(',',Field1,Field2,Field3)
That will return "Field1Value, Field2Value, Field3Value"
I'm not sure how you are going to get these column names. Do you need to get them from a sql statement, a string, etc. ? You can get the table names `SHOW COLUMNS FROM tablename'. The Field column will have the column names.
Triggers are available in mysql (added in 5.0.2 I think): http://dev.mysql.com/doc/refman/5.0/en/triggers.html
First, to find out the columns' names in advance, assuming that you have the table's name, you can get them as any other query:
SHOW COLUMNS FROM your_table
Once you have the names you can do:
SELECT CONCAT(field1,',',field2,',',field3) AS newField FROM your_table