concatenate columns with - in sql - sql-server-2008

I am having Table
id1 id2
---- ----
2 16
null 11
1 01
10 32
now i want to concatenate these two columns with '-'
id
----
2-16
11
1-01
10-32
i tried this with simple concat() func
select concat(id1,'-',id2)[id]
it results,
id
---
2-16
-11//need only 11 not -11
1-01
10-32
then tried with concat_ws() but it was not a function in sql server 2008.
then with COALESCE() but it results the same.
it can be done using condition statement.
but is there any other way to do it?

Here is another way using NVL function in Oracle. I think equivalent function in sqlserver is ISNULL
SELECT nvl(id1, '') || '-' || nvl(id2, '')
from table;

Related

I need to display the data, if at least one string present in another column using SQL

I have two columns in a table, column 1 contains tom jerry and column 2 has tom xxxx . I need to fetch the row at least one string is present the other column data
column1 column2
-----------------------------
tom jerry : tom xxx
tiger : tom yyy
tiger lion : lipard
lion tom : tiger lion
23 : 235 452
23 : 23
Expected result:
column1 column2
---------------------------
tom jerry : tom xxx
lion tom : tiger lion
23 : 23
Here is my approach:
Put both columns to another table with identity column (to identify each row)
Write a function to split text by a string (in this case pass a space)
Here you can use built in function if it is higher than SQL Server 2016
Get the identity and the splitted data to 2 tables each for column 1 and 2
Join the 2 tables for matches in Col-Split data, and get the IDs
Now Query for the data in table in above 1
Here is the Fiddle assuming that the code is for SQL Server 2016
In MySQL, you can use a regular expression for the data you have presented:
where column1 regexp replace(column2, ' ', '|') or
column2 regexp replace(column1, ' ', '|')
This may or may not work as stated. For instance, this will match "lion" and "lions" -- that meets what you are describing as I read it. That said, you can modify the patterns to enforce word breaks if you have something slightly different in mind.
You'd be able to achieve this by creating a function wherein you will pass value of column1 and column2. The function will do the heavy-lifting of checking if column1 value (multiple words separated by whitespace) has any of its word matching in column2 value. The pseudo code of function should be something like this -
Function bool IsCol1WordPresentInCol2(varchar2 col1value, varchar2 col2value)
Step 1: Split col1value based on whitespace to get series of words
Step 2: Split col2value based on whitespace to get series of words
Step 3: Outer loop to iterate through all words of col1 (extracted from step 1)
Step 4: Inner loop to iterate through all words of col2 (extracted from step 2)
Step 5: Check if the col1 and col2 words do match. At any point, if any match is found, return true from the function; else false.
Step 6: Based on the returned bool value from this function, you'd be able to identify, in your calling logic, if that record satisfies your condition.
You can use instring function to achieve this. Function depends on the database you use. Below is the query,
select * from table1 where col1 > col2;
select distinct t1.* from table1 t1
inner join table1 t2
on (instr(substring(t1.col1, instr(t1.col1, ' ') -1), col2) > 0);

create columns and rows mysql

I have table data such as this
index user date rank
11 a 1Mar 23
12 b 1Mar 16
13 a 2Mar 24
14 b 2Mar 18
What I would like to achieve via a query is this:
1Mar 2Mar
a 23 24
b 16 18
I don't know if this can be done via a single statement at the command line or if this will have to be done via a form and some scripting. Doing through scripting I can do, but can't see how to do in a single statement.
you can do pivot like below, if you know all possible values for date
or you need to use dynamic sql.
SELECT user,
MAX( CASE WHEN date ='1Mar' THEN rank else NULL end) AS '1Mar',
MAX( CASE WHEN date ='2Mar' THEN rank else NULL end) AS '2Mar'
FROM Table1
GROUP BY user

How to perform union operation between two tables?

After performing union operator between two tables in which the columns in one of the table are empty the result is displaying from second row... i dont why why is this happening ...? can anyone clarify my doubt?
my tables are sample1 and sample2
Both table contains Id, Empname , Location
data in table is
101 Null NUll
102 aaaa sec
data in table2 is
103 bbbb hyd
102 cccc gdv
Query:
(select EmpName,Location
from sample1)
union
(select EmpName,Location
from sample2)
Output
EMPNAME LOCATION
aaaa sec
bbbb hyd
cccc gdv
To remove the null records from the result, try this:
(select EmpName,Location
from sample1
WHERE EmpName IS NOT NULL
AND Location IS NOT NULL)
union
(select EmpName,Location
from sample2
WHERE EmpName IS NOT NULL
AND Location IS NOT NULL)
Result:
EMPNAME LOCATION
vijay ngdv
suresh hyd
ajay hyd
See result in SQL Fiddle.
EDIT:
I guess the record contains empty string or white spaces instead of null. So try this:
(select EmpName,Location
from sample1
WHERE LENGTH(TRIM(EmpName)) >0
AND LENGTH(TRIM(Location)) >0)
union
(select EmpName,Location
from sample2
WHERE LENGTH(TRIM(EmpName)) >0
AND LENGTH(TRIM(Location)) >0)
See result in SQL Fiddle.
Explanation:
LENGTH(TRIM(EmpName)) will return the length of the field EmpName after removing white spaces from it.
How to perform union operation between two tables?
you have to use union operator with following circumstances,
1.You have similar information in multiple tables and you want to retrieve rows from all of
them at once.
2.You want to select several sets of rows from the same table, but the conditions that
characterize each set aren't easy to write as a single WHERE clause. UNION allows retrieval
of each set with a simpler WHERE clause in its own SELECT statement; the rows retrieved by
each are combined and produced as the final query result.
Note : its not the perfect answer for your question but it helps in understanding of union
operator
please see this http://www.mysqlfaqs.net/mysql-faqs/Funtions-and-Operators/How-does-union-work-in-MySQL

Adding columns in MySQL - how to treat NULL as 0

I have simple table (test) where I need to perform some additions (I want to get a row total and a column total).
id var1 var2
1 NULL NULL
2 10 NULL
For column totals, summing works as expected (NULL is ignored in the addition):
SELECT SUM(var1) FROM test
10
For row totals, addition does not ignore NULL (if any column is NULL the result is NULL):
SELECT var1+var2 FROM test
NULL
NULL
What I want it to return is:
SELECT var1+var2 FROM test
NULL
10
Is there a way to get MySQL to treat NULL as 0 in an addition?
You want to use coalesce():
select coalesce(var1, 0) + coalesce(var2, 0)
coalesce() is ANSI standard and available in most databases (including MySQL).
use the IFNULL function
SELECT IFNULL(var1, 0) + IFNULL(var2, 0) FROM test
select
Case when (coalesce(`var1`, 0) + coalesce(`var2`, 0))=0
then NULL
else coalesce(`var1`, 0) + coalesce(`var2`, 0)
end
from test
SQL FIDDLE

SELECT statement with where clause IN is not reading variable in MySQL

I'm running SELECT statement (database MySQL version 5.5.27 under Windows 7) with variable in WHERE clause. It supposed to return 6 records, but it doesn't. Below is a simple test code.
-- Test-I
SET #group_saids := (SELECT REPLACE(
'''ClicPlan - España|ClicPlan - Francia|ClicPlan - UK|ClicPlan - Belgique|ClicPlan - Argentina|Clicplan - Turkey'''
,'|',"','") as aids_list from dual);
select #group_saids from dual;
select sd.aid
FROM said_aid sd
where sd.said in (#group_saids);
-- No records selected;
-- Test-II
select sd.aid
FROM said_aid sd
where sd.said in ('ClicPlan - España','ClicPlan - Francia','ClicPlan - UK',
'ClicPlan - Belgique','ClicPlan - Argentina',
'Clicplan - Turkey');
aid
----
3045
3253
3254
3260
3268
3270
In the code above Test-I, select from table said_aid doesn't return records, but should be 6 records output.
Int the Test-II same query with hard coded IN values return 6 records output.
No ERRORS during execution.
You have to use FIND_IN_SET() , because the IN clause expects literal values, so it won't work with the values inside a string variable, so replace your following line:
where sd.said in (#group_saids);
for this one:
where FIND_IN_SET(sd.said, #group_saids);