ADO - how to select column from xls file where two or more columns have the same name? - sql

I have an excel file like this:
| | A | B | C | D |
| 1 | Name 1 | Name 2 | Name 3 | Name 2 |
| 2 | Data | Data | Data | Data |
| 3 | Data | Data | Data | Data |
As you can see, headers of two columns have the same name - Name 2.
My question is, is it possible to tell the ADO engine from which column to select data?
Currently my select looks like this:
SELECT [Name 1], [Name 2] FROM [REPORT7_RAW$] WHERE [Name 1] IS NOT NULL
and ADO picks up the data from column which is listed under column B in excel. In other words it takes the first column which have the given name. Unfortunately I have two columns with the same name and I would like to pull out the data from column D. Is it possible?
I could not find any way to select column by its index rather the name.

You will need to change your connection string so that data header names are not used. The normal connection string would look something like this:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myExcel2007file.xlsx;
Extended Properties="Excel 12.0 Xml;HDR=YES";
You need to change the last bit, HDR=YES, to HDR=NO.
With that type of connection, the columns(fields) then become F1, F2, etc., where F1 = column A, F2 = column B, etc.
This is not ideal, since you are now essentially running the query based on the number of the column rather than the name, but with duplicate column names, this is the only way around that.
Per the comment from #barrowc: This format of the connection string will treat your column names as data. So depending on your query, you may need to include code to filter out the row that contains your column names.

Related

Replace rows from table that has specific value of multiple columns?

Let's say I have a table like this called MyTable.
| Column A | Column B || Column C | Column D |
| -------- | -------- || -------- | -------- |
| Cell 1 | Cell 2 || Cell 3 | Cell 4 |
| Cell 5 | Cell 6 || Cell 7 | Cell 8 |
And now I am inserting new row into this table that has format like this:
| Cell 1 | Cell 2 || Cell 3 | Cell Something else |.
What I want to do is replace an existing row from MyTable if the row I am inserting has the same value of the first 3 columns of MyTable (column A, column B, column C). As my real table has 250+ columns and
I want to replace rows if they have same value of 5 columns, I don't think INSERT ON CONFLICT UPDATE is good for this. In my opinion, it would be best to DELETE rows that need to be replaced and just INSERT new ones, but I don't know how to write that query.
I was thinking of INSERT ON CONFLICT UPDATE but firstly: I don't think I can specify more columns in ON CONFLICT part, and secondly: I think that I would need to specify 250 columns in UPDATE part, so that also doesn't work for me.
There is no problem specifying multiple columns in the on conflict clause, you just need a unique constraint on those columns. (see demo). As far as you having 250 columns (a highly questionable design, but another question altogether) you have no way around it you must list every column you want updated.

Can I specify to only match vectors that have one word using tsvectors?

I want to search for words in a tsvector only if they are the only words in the text. is there a way to do that? what about specifying that a word must be the last or the first word? The reason I'm using tsvector is that i need to match stemmed words.
UPDATE: sample data would be a table that contains three rows such as:
id | text | ts_vector
---------------------------------------------
1 | eating | -- this column holds vector data
| |
2 | eating food |
| |
3 | eats |
I want to be able to search for the word eat and only return the one with id = 1 and id = 3 but id 2 should be ignored. Query would look something like this:
to_tsquery('english', 'eat') ## tablename.ts_vector
except this returns all three rows in this example as you know
Thanks!
Try this one.
SELECT * FROM TABLE1 WHERE SUBSTRING(TEXT FROM 1 FOR 3)= 'EAT';

Fill MS-Access Table depending on another column with ComboBox and LIKE statement

I need to fill my MS Access Table with new names; I think an example would explain it the best:
This is my Table1 that I want to add the country names for. Please mind, that the column "Common Name" is something like a "free input field", so it can contain any string (not useful as a key)
| Common Name | Country |
---------------------------------------------------------
| new Samsung Galaxy A3 | |
| used Apple iPhone XS 64 gb in black | |
---------------------------------------------------------
This is my table that contains the new names:
Table2:
| String | Country |
---------------------
| Samsung | Japan |
| Apple | USA |
---------------------
I need access to fill in the "Country" field in Table1. Also it needs to be a combobox so that I can check if the correct row source is set, so
I am looking for a Lookup field, that changes it's row source depending on another column with something like an "LIKE" statement, so i can preserve the relationship. I already searched for other solutions with UPDATE and SELECT WHERE LIKE statements, they didnt work.
Thank you.
You can use a query with a subquery:
SELECT
Table1.[Common Name],
(Select First(Country)
From Table2
Where Table1.[Common Name] Like "*" & Table2.String & "*") AS Country
FROM
Table1;

Open Refine--create new column by looking up values from a pair of columns

I have a table in OpenRefine with columns A, B, and C like this:
A | B | C | D
---|---|---|---
a | 1 | b | 2
b | 2 | |
c | 3 | a | 1
d | 4 | c | 3
I want to create a column D by fetching the values from B corresponding to those in C, using A as an index. Hope that makes sense? I'm not having much luck figuring out how to do this in GREL.
You can use the 'cross' function to look up values across the project. Cross is usually used to look up values in a different OpenRefine project/file, but actually it works the same if you point it back at the same project you are already in.
So - from Col C, you can use "Add new column based on this column" with the GREL:
cell.cross("Your project name","Col A")
You'll get back an array of 'rows' - and if the same value appears in Column A multiple times you could get multiple rows.
To extract a value from the array you can use something like:
forEach(cell.cross("Your project name","Col A"),r,r.cells["Col B"].value).join("|")
The final 'join' is necessary to convert the array into a string which is required to be able to store the result (arrays can't be stored directly)

Gather single rows from multiple tables in Microsoft Access

I have several tables in Microsoft Access 2013, all of which follow the same format of:
ID | Object | Person 1 | Person 2 | Person 3 |
ID | String | Yes/No | Yes/No | Yes/No |
What I would like to do is make a query where I put in a string value for each table and it prints out the entire row, with each string getting its own row, so it looks like:
ID Number | Object | Person 1...
Table 1 ID | Table 1 String | Table 1 Yes/No...
Table 2 ID | Table 2 String | Table 2 Yes/No...
Every time I try, though, it puts all the data into one extremely long row that's impossible to look at. All of my searching has only turned up people trying to do the exact opposite of what I'm doing, though, so I must be missing something obvious. Any tips?