Add selected values from multi column into one column separated by ',' - sql

I want to select values from multiple columns into one column. I have 2 separate columns from which i want to get name,address,state,zip in following format in SQL Server 2008
Name (new line)
address,state,zip
Name (new line)
address,state,zip
Query:
select
name + char(13) + concat(address,',', state,',', zip)
from
tbl1
join
tbl2 on....
I am not able to get the desired output. I get concat is not a recognized built in function name.

You could use + operator and cast the zip field as varchar directly like this:
For example:
select 'Dara Singh' + char(13) + '1234 Main Street' + ',' + 'NY' + ','
+ cast(95825 as varchar(10))
This is how your query would look:
select name + char(13) + [address] + ',' + [state] + ',' + cast([zip] as varchar(10))
from tbl1 join tbl2 on....

Related

Manipulating duplicate values?

I have a table, with an ID, FirstName & Lastname.
I'm selecting that using the following query:
SELECT USER_ID as [ID], First_P + ' ' + Last_P as FullName FROM Persons
It's working fine. I'm basically having a list of ID's and full names.
Full names could be the same. How is it possible for me to find them and add the ID on the Full name cell as well? only when the names are the same.
Example:
1 John Wick (1)
50 John Wick (50)
I haven't found any similar questions to be honest, at least not for MSSQL. So If there are any, feel free to link me.
please take a look my answer. I used nested query to identify number of duplicated names
SELECT
ID,
IIF(NUMBEROFDUPS =1, NAME, CONCAT(NAME, ' (', ID, ')')) AS NAME
FROM
(
SELECT
ID,
CONCAT(First_P, ' ', Last_P) AS NAME,
COUNT(*) OVER (PARTITION BY First_P,Last_P) AS NUMBEROFDUPS
FROM
Table1
) tmp;
You can use outer apply to group the items via First_P + ' ' + Last_P
and then add case for multiple items.
The select stuff should look like:
SELECT USER_ID as [ID], p1.First_P + ' ' + p1.Last_P + case when cnt.FullName is not null
then '(' + p2.[sum] + ')' else '' end as FullName FROM Persons p1
outer apply (select First_P + ' ' + Last_P as FullName,count(1) as [sum]
from Persons p2
where p2.First_P + ' ' + p2.Last_P = p1.First_P + ' ' + p1.Last_P
group by First_P + ' ' + Last_P
having count(1) > 1) cnt

SQL query for a carriage return and Line Feed SQL 2008

SQL query is only bringing back 1 Row #1
select * from email_table
where emailAddress like '%'+char(13)+'%'
Need to bring back first two rowsing with the carriage return in SQL 2008.
You should be able to just add a literal new line in your SQL:
select *
from #email_table
where emailAddress like '%
%'
You can do this to troubleshoot:
/*DECLARE #email_table TABLE (emailaddress varchar(2000))
INSERT INTO #email_table(emailaddress) SELECT '#1gmail.com ' + CHAR(13) + CHAR(10)
INSERT INTO #email_table(emailaddress) SELECT '#2gmail.com' + CHAR(13) + CHAR(10)
INSERT INTO #email_table(emailaddress) SELECT '#3gmail.com'
*/
SELECT *,CONVERT(VARBINARY(MAX),emailaddress) AS [Binary Representation]
FROM #email_table
--WHERE emailAddress LIKE '%' + CHAR(13) + '%'
The last characters of the [Binary Representation] row should be 0D0A (or 0D000A00 if the EmailAddress field is NVARCHAR), if they are not, look up what characters they actually are.

Concat column values with comma as the separator

I am trying to figure out the result of the below query on how to concat all the columns into a single string with comma separated values. The number of values can be dynamic.
SELECT 'Sc4','Sc5','Sc8','Sc7','Sc2'
I want the result to be as below:
Sc4,Sc5,Sc8,Sc7,Sc2
I have tried using stuff but did manage to concat the string but unable to insert comma in between.
Below is what i have tried
SELECT Stuff((SELECT 'Sc4','Sc5','Sc8','Sc7','Sc2' FOR XML PATH('')), 2, 0, '');
UPDATE
I would like to rephrase my question, is there any way the out shown below can be converted to csv
i assume Sc4, Sc5 etc are your column name and not string constant ?
SELECT Stuff(
(
SELECT ',' + Sc4 + ',' + Sc5 + ',' + Sc8 + ',' + Sc7
+ ',' + Sc2
FROM yourtable
FOR XML PATH('')
), 1, 1, '');
Try concatenation like
SELECT 'Sc4' + ',' + 'Sc5' + ',' + 'Sc8' + ',' + 'Sc7' + ',' + 'Sc2'

Query display names

I have a 2 columns in a SQL Server table.
One is characters like 'Attn: firstname lastname', and the other column has a number associated with that person.
I can't figure out how to query them so the info comes out as:
Lastname, Firstname - number
If you are in SQL Server you can do this
Select Col1 + ' - ' + Col2
From dbo.{Table}
Where {Conditions}
If you mean you need to drop the "attn:" you can do something like this
Select substring(Col1, 5,len(Col1)-5) + ' - ' + Col2
From dbo.{Table}
Where {Conditions}
Instead of hardcoding the index of ':', you can do something like this
SELECT REPLACE(RIGHT(Col1, (LEN(Col1) - CHARINDEX(':', Col1)-1)),' ',', ') + ' - ' + Col2
To test this you can run the following
DECLARE #column1string AS VARCHAR(30), #column2number AS VARCHAR(10)
SET #column1string = 'Attn: Firstname Lastname'
SET #column2number = '12345678'
SELECT REPLACE(RIGHT(#column1string, (LEN(#column1string) - CHARINDEX(':', #column1string)-1)),' ',', ') + ' - ' + #column2number

SELECT statement issue - Joining of data of different fields in one cell of Gridview

I am using following SELECT statement for Gridview:
SelectCommand="SELECT ID, Date, Train, I_R, Dir_Ind, Detn, Rly, DiV, Loco, Shed, locoClass, loco_type,
(maj_sch_type + ','+ ' ' + maj_sch_place +',' +' '+ (CAST(maj_sch_dt as VARCHAR(11)) + ' '+'/'
+ ' ' + min_sch_type +',' + ' ' + min_sch_place + ',' + ' '+(CAST(min_sch_dt as VARCHAR (11))) )) as "major",
Equipt, I_R, reason
FROM PunctualityMain Order by Date ASC"
With the above, I am getting results in single cell from 6 (3 + 3) different fields of SQL data as
POH, KPA, Jan 2 2012 / IB, Shed, Apr 18 2012
Issue is this that whenever data in any group of 3 fields (before or after '/') is blank, Gridview cell is remain completely blank. If both group of fields having data than Gridview dispaly is OK as mentioned above. Can SELECT statement be modified for showing data in Gridview with any one group of fields are empty?
If you try to concatenate a null string with a non-null string, the result will be null.
Select NULL + 'test'
this will return NULL.
Also, I think your code didn't copy paste extremely well into your question, but you need to use the ISNULL() function. I think you can get the point. You'll probably have to clean up the code a bit for it to run. Just wrap every field that you are concatenating with ISNULL(MyField, '')
SELECT
ID,
Date,
Train,
I_R,
Dir_Ind,
Detn,
Rly,
DiV,
Loco,
Shed,
locoClass,
loco_type,
(isnull(maj_sch_type, '') + ', ' + isnull(maj_sch_place, '') +', '+ (isnull(CAST(maj_sch_dt as VARCHAR(11), '')) + ' / ' + isnull(min_sch_type, '') +', ' + isnull(min_sch_place, '') + ', '+ (isnull(CAST(min_sch_dt as VARCHAR (11)), '')) )) as "major",
Equipt,
I_R,
reason
FROM PunctualityMain
Order by Date ASC
Reference
IsNull