Joining table A and table B, both having column1 as common - sql

I have 2 tables - TableA and TableB. Both have column1 as common column.
But in TableA data in column1 is numeric like 201 and in TableB data in column1 is in words like two hundred one.
None of the other columns is common.
How can I join these tables? Can I use to_char(todate(column1,'j'),jsp) for TableA?

In Oracle it is possible using this to_char function, but in SQL you will need to write a function for this conversion and then pass this function in your query. One such example is number_to_string function found on this post.
Converting Numbers to Words in MYSQL result! Using Query
Using this function you can write a condition like
number_to_string(TableA.numValue) = TableB.stringValue in your JOIN to obtain the desired results.

First, it would be appropriate to have some function which convert integer to words, and then you can use standard join clause.
This link can help you
or
this link

Related

How to join SQL column elements together?

I have an SQL table with the column named expenses like this
I want to write a query to return all the elements of this column separated by a comma. For the above example output should be:-
hotel,taxi,food,movie
How can I do this?
You can use the group_concat aggregate function:
SELECT GROUP_CONCAT(expenses)
FROM my_table

PostgreSQL match string to dynamically entered string

I have a varchar field in my database table A let's call it store_name, this field gets its value from entity A, now entity B enters store_name into a different database table B now I want to get all records in table A where the store_name matches the values in table B.
How would you recommend me doing the query as I don't control the values of those 2 fields?
What do you think about PostgreSQL fuzzystrmatch?
The tables contain thousands of records.
Thanks
Assuming that both table A and table B are in the same database. And I guess since you don't control insertion of data, you are not sure if the values are of same case or there may be a spelling mismatch.
Case 1: If the problem is only of case-mismatch, you can use ilike:
Select a.store_name
from a, b
Where a.store_name ilike b.store_name
Case 2: If you also want to check for spelling mismatch, but words sound similar, then after installing postgresql-contrib package and creating extension fuzzystrmatch, you can use:
Select a.store_name
from a, b
Where a.store_name ilike b.store_name OR
soundex(a.store_name) = soundex(b.store_name)
If you are dealing with names, which may not always be in English, it may be more appropriate to use metaphone or dmetaphone function instead of soundex.
Documentation: Fuzzystrmatch
If you want matching you can use a straight up join.
Select a.store_name
from a
join b on a.store_name = b.store_name;
If you want to use fuzzy matching just use the various functions available in the join criteria. Documentation here
Note: there are some limitations to Fuzzy string matching so i would advise testing each out on values that you either know match or don't.

SQLite WHERE-Clause for every column?

Does SQLite offer a way to search every column of a table for a searchkey?
SELECT * FROM table WHERE id LIKE ...
Selects all rows where ... was found in the column id. But instead to only search in the column id, I want to search in every column if the searchstring was found. I believe this does not work:
SELECT * FROM table WHERE * LIKE ...
Is that possible? Or what would be the next easy way?
I use Python 3 to query the SQLite database. Should I go the route to search through the dictionary after the query was executed and data returned?
A simple trick you can do is:
SELECT *
FROM table
WHERE ((col1+col2+col3+col4) LIKE '%something%')
This will select the record if any of these 4 columns contain the word "something".
No; you would have to list or concatenate every column in the query, or reorganize your database so that you have fewer columns.
SQLite has full-text search tables where you can search all columns at once, but such tables do not work efficiently with any other queries.
I could not comment on #raging-bull answer. So I had to write a new one. My problem was, that I have columns with null values and got no results because the "search string" was null.
Using coalesce I could solve that problem. Here sqlite chooses the column content, or if it is null an empty string (""). So there is an actual search string available.
SELECT *
FROM table
WHERE (coalesce(col1,"") || coalesce(col2,"") || coalesce(col3,"") || coalesce(col4,"")) LIKE '%something%')
I'm not quite sure, if I understood your question.
If you want the whole row returned, when id=searchkey, then:
select * from table where id=searchkey;
If you want to have specific columns from the row with the correct searchkey:
select col1, col2, col3 from table where id=searchkey;
If you want to search multiple columns for the "id": First narrow down which columns this could be found in - you don't want to search the whole table! Then:
select * from table where col1=searchkey or col2=searchkey or col3=searchkey;

Bigquery how to query multiple tables of the same structure?

I have datasets of the same structure and i know I can query them like this, they are named by date:
SELECT column
FROM [xx.ga_sessions_20141019] ,[xx.ga_sessions_20141020],[xx.ga_sessions_20141021]
WHERE column = 'condition';
However I actually want to query various months of this data... so instead of listing them all in the same way as above, is there syntax that you can use that looks like:
SELECT column
FROM [xx.ga_sessions_201410*] ,[xx.ga_sessions_201411*]
WHERE column = 'condition';
Take a look at the table wildcard functions section of the BigQuery query reference. TABLE_DATE_RANGE or TABLE_QUERY will work for you here. Something like:
SELECT column
FROM TABLE_DATE_RANGE(xx.ga_sessions_,
TIMESTAMP('2014-10-19'),
TIMESTAMP('2014-10-21'))
WHERE column = 'condition';

joining scalar valued function to a table taking that column generated by SVF as in input

i have scalar valued function which is called and it gives a new column values in the table, now I want to join this new column value to a column of new table which I have added as derived table in sql server 2005.
You can certainly use that function in a JOIN, something like this:
SELECT {fieldlist}
FROM {tablename} a
JOIN {tablename} b ON b.{fieldname} = dbo.{functionname}({parameters})
But, as Joe stated, a code sample would make this a bit less theoretical and more useful for the rest of the community.