how to combine two numeric fields? - sql

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;

Related

Like Operator for checking multiple words

I'm struggling for a like operator which works for below example
Words could be
MS004 -- GTER
MS006 -- ATLT
MS009 -- STRR
MS014 -- GTEE
MS015 -- ATLT
What would be the like operator in Sql Server for pulling data which will contain words like ms004 and ATLT or any other combination like above.
I tried using multiple like for example
where column like '%ms004 | atl%'
but it didn't work.
EDIT
Result should be combination of both words only.
Seems you are looking for this.
`where column like '%ms004%' or column like '%atl%'`
or this
`where column like '%ms004%atl%'
;WITH LikeCond1 as (
SELECT 'MS004' as L1 UNION
SELECT 'MS006' UNION
SELECT 'MS009' UNION
SELECT 'MS014' UNION
SELECT 'MS015')
, LikeCond2 as (
SELECT 'GTER' as L2 UNION
SELECT 'ATLT' UNION
SELECT 'STRR' UNION
SELECT 'GTEE' UNION
SELECT 'ATLT'
)
SELECT TableName.*
FROM LikeCond1
CROSS JOIN LikeCond2
INNER JOIN TableName ON TableName.Column like '%' + LikeCond1.L1 + '%'
AND TableName.Column like '%' + LikeCond2.L2 + '%'
Try like this
select .....from table where columnname like '%ms004%' or columnname like '%atl%'

Adding a prefix to SQL result

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,
....

How to group 3 columns into a single one in SQL

I have a table that has three columns: myColumn, myColumn2 and myColumn3. The three columns can have a different value but they are all the same type.
I need to do a join such that I merge all the items in the three columns and then group by
If I had a single column, it would be done like this:
select distinct (myColumn), COUNT(myColumn) as 'TotalF'
from FormularioCorto
where idEvento = #idEvento and myColumn <> ''
group by myColumn
The question is:
How do I do a TSQL statement that will allow for me to join the three columns and then group by?
Edit: here is the sample data
I have a table that has 3 columns with the same type
columna columnb columnc
a a b
b a b
c c d
I need to run a tsql that will merge the 3 columns and group by, to get a result like
newcolumn count
a 3
b 3
c 2
d 1
Any ideas?
Thanks in advance
It is hard to tell without some sample data and desired output but a guess would be something like:
SELECT MyCol as MyColumn, Count(*) as 'TotalF'
FROM
(
SELECT myColumn MyCol from FormularioCorto
WHERE idEvento = #idEvento AND myColumn <> ''
UNION ALL
SELECT myColumn2 MyCol from FormularioCorto
WHERE idEvento = #idEvento AND myColumn2 <> ''
UNION ALL
SELECT myColumn3 MyCol from FormularioCorto
WHERE idEvento = #idEvento) AND myColumn3 <> ''
) A
GROUP BY MyCol;
Or for just the example data and result:
SELECT NewColumn, Count(*) as Count FROM
(
SELECT columna as NewColumn FROM SomeTable
UNION ALL
SELECT columnb as NewColumn FROM SomeTable
UNION ALL
SELECT columnc as NewColumn FROM SomeTable
) A
Group by NewColumn
I think you mean concatenate rather than JOIN. JOIN has a very specific meaning with SQL. Use the + operator to concatenate string fields.
CONCAT(`myColumn`, `myColumn2`, `myColumn3`) as joinedField
The CONCAT() function concatenates fields and strings.
Info:
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat
Note: You should reconsider your structure though, as this will prove to be painfully slow as you add more records.
select cast(myColumn as nvarchar) + CAST(COUNT(myColumn)as nvarchar) as 'TotalF'
from FormularioCorto
where idEvento = #idEvento and myColumn <> ''
group by myColumn
If I am understanding your question right, then you simply want 2 columns (one identifying the id you are counting and one giving the count for the identifier. If this is correct, simply use your existing query for each column and union the results.
For example;
SELECT myColumn AS Id, COUNT(myColumn) AS IdCount
FROM FormularioCorto
where idEvento = #idEvento and myColumn <> ''
GROUP BY myColumn
UNION
SELECT myColumn2 AS Id, COUNT(myColumn2) AS IdCount
FROM FormularioCorto
where idEvento = #idEvento and myColumn2 <> ''
GROUP BY myColumn2
etc etc.
Try this:
SELECT myColumn1 + myColumn2 + myColumn3 AS AllThreeFields
FROM FormularioCorto
WHERE 1=1
GROUP BY myColumn1 + myColumn2 + myColumn3
It won't be fast but it should work. If you need to delimit the data in the columns with other characters you can try this:
SELECT myColumn1 + ' ' + myColumn2 + ' ' + myColumn3 AS AllThreeFields
FROM FormularioCorto
WHERE 1=1
GROUP BY myColumn1 + myColumn2 + myColumn3
If the column types are not the same (you said they were), you will need to cast them to a similar type.

SQL Query Distinct Column Concatenate Other Column

I have a MS SQL query that joins multiple tables and an example of the results are:
EmailAddress Column2
--------------------- ----------------
sean#abc.com Value1
sean#abc.com Value2
test#abc.com Value5
What I really want to achieve are the following results:
EmailAddress Column2
--------------------- ------------------
sean#abc.com Value1, Value2
test#abc.com Value5
There are other columns that are identical for each row with the same email address. Can anyone help me with the SQL to return distinct email addresses and concatenate the column2 information?
Thanks for any feedback.
Try the answer to this question
In SQL Server 2005+:
WITH q AS
(
SELECT 'sean#abc.com' AS address, 'Value1' AS value
UNION ALL
SELECT 'sean#abc.com', 'Value2'
UNION ALL
SELECT 'test#abc.com', 'Value5'
)
SELECT (
SELECT CASE WHEN ROW_NUMBER() OVER (ORDER BY value) > 1 THEN ', ' ELSE '' END + value
FROM q qi
WHERE qi.address = qo.address
FOR XML PATH('')
)
FROM (
SELECT DISTINCT address
FROM q
) qo
Same answer from Jeremy, but I would use the other answer with XML PATH trick

How to get result from a column with combined data?

data:
id bb
1 14,35
2 5,11,12,125,36
3 3,23,45,15,1
4 651
5 5,1,6
6 1,7
For example, i wan't get id which with value '1'. So id(3,5,6) should return , but not others with '14' or '11'.
DB: Mysql
This is not the most efficient solution but it might give you what you want off the table:
select id from MyTable where bb like '%,1,%' union
select id from MyTable where bb like '1,%' union
select id from MyTable where bb like '%,1' union
select id from MyTable where bb like '1'
cheers
select * from test where find_in_set('1',bbb)
or
select * from test where bbb REGEXP '(^|,)1(,|$)'
Am I missing something?
SELECT *
FROM MyTable
WHERE (id = 3) or (id = 5) or (id = 6)
you can do like this
select * from mytable where id like '14%' or '11%'