Combine rows of data into one cell, separated by quotes and comma - vba

I have the following data:
2
3
4
5
6
7
8
9
10
apple
green
hi
I would like to consolidate all of this into one value, like so:
'2','4','5','6','7','8','9','10','apple','green','hi'
So I need to take data from rows, paste them into an Excel file, run this macro and get them all in one cell (with commas and quotes). I will then put them in a WHERE IN clause in an SQL statement for faster searches. If there is an easier way or a tool, please let me know.
Here is the intended use in SQL:
Select *
from Blah..Table
where IDs in
(
'2','4','5','6','7','8','9','10','apple','green','hi'
)

I wrote a user defined function called JoinRange that does that. You can get the code here
http://www.dailydoseofexcel.com/archives/2012/03/31/joinrange-update/
You would use it like
=joinrange(A1:A12,,"','","'","'")
which gives the result
'2','3','4','5','6','7','8','9','10','apple','green','hi'
You can also call it from VBA.

Related

Break up text string in SQL

I'm using SQL Server 2019 (v15) and am trying to split up URLs so I have each individual part.
i.e,
URL
Column 1
Column 2
Column 3
Column 4
Column 5
Column 6
store.example.com
com
example
store
NULL
NULL
NULL
demo.m.en.store.example.com
com
example
store
en
m
demo
I've tried a few things but always end up getting in a long REVERSE / CHARINDEX / SUBSTRING cycle. Tried using STRING_SPLIT, but couldn't work out the PIVOT to get it to look like the above.
I've also used PARESENAME, but this will not work for longer strings.
Note: I'm trying to do this on a large scale

Select entries with substring in a specific position of the word

I am trying to write an SQL query where my goal is to select all the entries that contains a substring in a specific position of a word (the entries are phrases with multiple words).
To make it clearer, suppose that I have these two entries (values inside a column, call it phrase):
1 - "Jamming in New York"
2 - "bosbessenjam 30g"
3 - "30g bosbessenjam"
4 - "Tranches de jambon fumé"
I want to select all the rows that contains a word that ends with "jam". So I want the second and third row.
I tried using LIKE '%jam%', however it just check the overall string and not the single word. So LIKE '%jam' returns the third row, but not the second.
Any idea on how to do this?

Splitting Data in a Column

I get some data that comes in to the table.
This table is currently only displaying after I merged all the tables. So you currently see:
Table 1
union
Table 2
union
Table 3
The issue I have is now i have one column in there where it contains data like this:
AA2B133
I want to split this column, so in the current column it tries to keep the first 3 charachters and the other 4 charachters it goes into another column.
What is the best way or simplest way of doing this.
Thank you
You can use the RIGHT() and LEFT() functions to split the data into multiple columns.
For example:
SELECT LEFT(data,3), RIGHT(data,4)
FROM (SELECT 'AA2B133' AS data) A
Will return two columns with the breakout you requested.
You can use Java to get the 3 first caracters using this
String substring (String s, int start, int len)
so if you want to get the three first charachters for example you can do like this
String substring ("AA2B133",0,2)

Using a query in Microsoft Access to compare two fields and find multiple matching values

I have a problem I am trying to solve using a query instead of VBA.
I have two fields which we'll call "FPC" and "Code". Both fields contain numbers. An FPC value will match a Code value. What I want to make sure is that once an FPC value matches a Code value, the same FPC value does not match up with a DIFFERENT Code Value and vice versa - Once a Code Value is used, I don't want the Code Value to match up with more than one FPC.
It is important to note that there are duplicate values used in both fields.
Here is an example:
FPC CODE
1 12
1 12
1 14
2 16
3 11
3 11
4 17
5 19
6 16
There are two errors here:
1. The FPC "1" is matched up with two different Code Values.
2. The Code "16" is matched up with two different FPC values.
Please let me know your suggestions. I was thinking a query would help, and then running VBA to pull the results (there is tens of thousands of records).
What is the purpose of the query? Just to identify problems? If yes then something like
select FPC, count(distinct(CODE)) from tableName where count(distinct(CODE)) >1 group by FPC
(and the converse query for CODE vs. FPC) should be OK.

SQL Query - How to not include some results

I apologize if this question was asked before I just couldn't correctly formalize it. I have a code column in a table and want to query it but remove some elements with some particular code. Say I want to take elements with code starting from 4 but not include the elements with code whose 6-th number is 9 (1121290).
The code column contains string of numbers with max-length of 8 char. and I want to take almost everything that starts with 4 except elements that start with 411, 427 and 428
Yes you can give query like this:--
I have column element with 6 digit codes.
I am fetching element whose 1st digit is 4 or 6th is 9.
we have to use % and _ for fetching..
SELECT element FROM "table" WHERE element LIKE '%4____9%';
try this it will work.
Everything that starts with 4 except elements that start with 411, 427 and 428 :-
SELECT element FROM "table" WHERE element LIKE '%4__';
1st digit is 4 and 6th is not 9: I tested this one it is working fine try this :-
SELECT element
FROM "table"
WHERE element NOT LIKE '%_____9' and element LIKE '%4_____'
It might be easiest to simply spell out each condition in the where clause:
WHERE code like '4____9%' AND code NOT LIKE '411%' AND code NOT LIKE '427%' AND code NOT LIKE '428%'
The extra conditions won't hurt the query's efficiency much; it's going to have to scan every single row starting with 4 anyway.
Assuming that your "CODE" field is varchar and you are using SQL Server,
you can use the below Query
select * from yourTable
where code like '4%'
AND CHARINDEX('9',code)<>6
Here is a more compact version.
`SELECT * FROM table_name WHERE code IN ('4%') and code NOT IN ('411%','427%', '428%','_____9%');`