Adding a prefix to SQL result - sql

can some one please explain how would I add a prefix to column "Name" in my scenario.
I would like to create UNION between two identical tables.
Both contain a field "Name". In the column name I want to add a prefix "TABLE-A-" or "TABLE-B-".
I know i can do this by N'PREFIX' + Name in the select, but the problem is that some records already have the prefix and some dont. So by using this would create a double prefix for those records...
Any idea?

You can use a CASE expression to conditionally add the prefix:
CASE WHEN Name like 'A1-%' THEN Name ELSE N'A1-' + Name END

case left(Name, 3) when N'A1-' then N'' else N'A1-' end + Name

You can use CASE expressions:
CASE WHEN col1 LIKE 'TABLE-A-%' THEN col1 ELSE 'TABLE-A-' || col1 END
Alternatively, you could use a UNION of 4 selects:
select col1 FROM tab1 WHERE col1 LIKE 'TABLE-A-%'
UNION
select 'TABLE-A-' || col1 FROM tab1 WHERE col1 NOT LIKE 'TABLE-A-%'
UNION
<same stuff for table B>

SELECT 'TABLE-A'+REPLACE(Name,'TABLE-A','')
FROM [TABLE-A]
UNION ALL
SELECT 'TABLE-B'+REPLACE(Name,'TABLE-B','')
FROM [TABLE-B]
Raj

Add a '+' symbol before the countrycode
UPDATE [masters].[country] SET Countrycode = '+' +Countrycode
output:
===========
countrycode
+91,
+355,
....

Related

How to use LIKE with ANY in BigQuery?

I would like to use the LIKE ANY operator to exclude rows based on an array of substrings, but BigQuery does not recognize it.
declare unlaunched_clistings array<string>;
set unlaunched_clistings = {unlaunched_clistings} ;
select * from {p}.simeon_logs.process_errors e
where not e.message like any(unlaunched_clistings)
Error : LIKE ANY is not supported at [8:32]
Is there any workaround for this?
LIKE ANY is not supported, however you can use following 2 ways:
Use LIKE with ORs between them
WITH table AS (
SELECT 'abc' as col union all
SELECT 'xzy' as col
)
SELECT col
FROM TABLE
WHERE (col like '%abc%'
OR col like '%cde%' OR col like '%something%')
User RegEx
WITH table AS (
SELECT 'abc' as col
UNION ALL
SELECT 'xzy' as col
)
SELECT col
FROM TABLE
WHERE REGEXP_CONTAINS(col
, 'abc|cde|something')
Above both will give you abc row.

SQL Search rows that contain strings from 2nd table list

I have a master table that contains a list of strings to search for. it returns TRUE/FALSE if any string in the cell contains text from the master lookup table. Currently I use excel's
=SUMPRODUCT(--ISNUMBER(SEARCH(masterTable,[#searchString])))>0
is there a way to do something like this in SQL? LEFT JOIN or OUTER APPLY would be simple solutions if the strings were equal; but they need be contains..
SELECT *
FROM t
WHERE col1 contains(lookupString,lookupColumn)
--that 2nd table could be maintained and referenced from multiple queries
hop
bell
PRS
2017
My desired results would be a column that shows TRUE/FALSE if the row contains any string from the lookup table
SEARCH_STRING Contained_in_lookup_column
hopping TRUE
root FALSE
Job2017 TRUE
PRS_tool TRUE
hand FALSE
Sorry i dont have access to the DB now to confirm the syntax, but should be something like this:
SELECT t.name,
case when (select count(1) from data_table where data_col like '%' || t.name || '%' > 0) then 'TRUE' else 'FALSE' end
FROM t;
or
SELECT t.name,
case when exists(select null from data_table where data_col like '%' || t.name || '%') then 'TRUE' else 'FALSE' end
FROM t;
Sérgio
You can use a combination of % wildcards with LIKE and EXISTS.
Example (using Oracle syntax) - we have a v_data table containing the data and a v_queries table containing the query terms:
with v_data (pk, value) as (
select 1, 'The quick brown fox jumps over the lazy dog' from dual union all
select 2, 'Yabba dabba doo' from dual union all
select 3, 'forty-two' from dual
),
v_queries (text) as (
select 'quick' from dual union all
select 'forty' from dual
)
select * from v_data d
where exists (
select null
from v_queries q
where d.value like '%' || q.text || '%');

combining 3 tables where combination of 2 columns is not unique

There are 3 (will be up to 6 in the future) tables with the same columns.
I need to unify them, i.e. union on same columns. In addition to this - rows shall not be unique, based on 2 column combination! There are a couple of examples on the net, but all of them show how to exclude unique column values based on WHERE for one column. In my case there are 2 columns (Col1 and Col2 combination).
Here are the schematics:
and
And here is how I imagined final query (for 3-tables) would look like:
SELECT
*
FROM
(
SELECT * FROM table1
UNION
SELECT * FROM table2
UNION
SELECT * FROM table3
)
GROUP BY
Col1, Col2
HAVING
COUNT (*) > 1
What would be a correct way?
P.S. FYI single-column solutions
Multiple NOT distinct
How to select non "unique" rows
How to Select Every Row Where Column Value is NOT Distinct
EDIT:
I have used the code from accepted answer and added additional search criteria:
ON (SOUNDEX(Merged.[name_t1]) = SOUNDEX(Multiples.[name_t1]) OR Merged.[name_t1] LIKE '%' + Multiples.[name_t1] + '%' OR Multiples.[name_t1] LIKE '%' + Merged.[name_t1] + '%')
AND (SOUNDEX(Merged.[name_t2]) = SOUNDEX(Multiples.[name_t2]) OR Merged.[name_t2] LIKE '%' + Multiples.[name_t2] + '%' OR Multiples.[name_t2] LIKE '%' + Merged.[name_t2] + '%')
search col1 and col2:
-by SOUNDEX
-by col1 like (col1 from other table)
-by (col1 from other table) like col1
Here's the basics of a CTE-based approach :
With Merged AS
( -- CTE 1 : All Data in one table
SELECT * FROM table1
UNION ALL
SELECT * FROM table2
UNION ALL
SELECT * FROM table3
)
, Multiples AS
( -- CTE 2 : Group by the fields in common
SELECT Col1, Col2
FROM Merged
GROUP BY Col1, Col2
HAVING Count(*)>1 -- only want Groups of 2 or more
)
SELECT
Merged.*
FROM Merged INNER JOIN Multiples
-- Only return rows from Merged if they're in Multiples
ON Merged.[Col1]=Multiples.[Col1]
AND Merged.[Col2]=Multiples.[Col2]
Something like that works with my own example MS-SQL data, and it looks like SQLite syntax is the same. HTH!

T-SQL Comma delimited value from resultset to in clause in Subquery

I have an issue where in my data I will have a record returned where a column value will look like
-- query
Select col1 from myTable where id = 23
-- result of col1
111, 104, 34, 45
I want to feed these values to an in clause. So far I have tried:
-- Query 2 -- try 1
Select * from mytableTwo
where myfield in (
SELECT col1
from myTable where id = 23)
-- Query 2 -- try 2
Select * from mytableTwo
where myfield in (
SELECT '''' +
Replace(col1, ',', ''',''') + ''''
from myTable where id = 23)
-- query 2 test -- This works and will return data, so I verify here that data exists
Select * from mytableTwo
where myfield in ('111', '104', '34', '45')
Why aren't query 2 try 1 or 2 working?
You don't want an in clause. You want to use like:
select *
from myTableTwo t2
where exists (select 1
from myTable t
where id = 23 and
', '+t.col1+', ' like '%, '+t2.myfield+', %'
);
This uses like for the comparison in the list. It uses a subquery for the value. You could also phrase this as a join by doing:
select t2.*
from myTableTwo t2 join
myTable t
on t.id = 23 and
', '+t.col1+', ' like '%, '+t2.myfield+', %';
However, this could multiply the number of rows in the output if there is more than one row with id = 23 in myTable.
If you observe closely, Query 2 -- try 1 & Query 2 -- try 2 are considered as single value.
like this :
WHERE myfield in ('111, 104, 34, 45')
which is not same as :
WHERE myfield in ('111', '104', '34', '45')
So, If you intend to filter myTable rows from MyTableTwo, you need to extract the values of fields column data to a table variable/table valued function and filter the data.
I have created a table valued function which takes comma seperated string and returns a table value.
you can refer here T-SQL : Comma separated values to table
Final code to filter the data :
DECLARE #filteredIds VARCHAR(100)
-- Get the filter data
SELECT #filteredIds = col1
FROM myTable WHERE id = 23
-- TODO : Get the script for [dbo].[GetDelimitedStringToTable]
-- from the given link and execute before this
SELECT *
FROM mytableTwo T
CROSS APPLY [dbo].[GetDelimitedStringToTable] ( #filteredIds, ',') F
WHERE T.myfield = F.Value
Please let me know If this helps you!
I suppose col is a character type, whose result would be like like '111, 104, 34, 45'. If this is your situation, it's not the best of the world (denormalized database), but you can still relate these tables by using character operators like LIKE or CHARINDEX. The only gotcha is to convert the numeric column to character -- the default conversion between character and numeric is numeric and it will cause a conversion error.
Since #Gordon, responded using LIKE, I present a solution using CHARINDEX:
SELECT *
FROM mytableTwo tb2
WHERE EXISTS (
SELECT 'x'
FROM myTable tb1
WHERE tb1.id = 23
AND CHARINDEX(CONVERT(VARCHAR(20), tb2.myfield), tb1.col1) > 0
)

how to combine two numeric fields?

How to combine two int columns into one.
My table1 is as follows:
name adress1 adress2
hhh 1 2
www 2 3
I want result as follows:
name columnz
hhh 12
www 23
In the upcoming SQL-server you can do:
SELECT name, concat(address1,address2) as columnz
FROM table1
However SQL-server does not allow concat yet, so you'll have the use the '+' operator and a cast.
SELECT
name
,CAST(address1 AS char)+CAST(address2 AS char) as columnz
FROM table1
SQL is not that troublesome about the difference between strings and numbers.
Another option is:
SELECT name, (address1*10+address2) as columnz
FROM table1
SELECT name, CAST(ADRESS1 AS VARCHAR(20)) + CAST(ADRESS2 AS VARCHAR(20)) AS columnz from table1
Try this:
SELECT name, Concat(adress1, adress2) AS columnz FROM table1;
select name, convert(varchar, adress1) + convert(varchar, adress2) as columnz from table1;