Convert SQL Server result set into string
Test this:
DECLARE @result NVARCHAR(MAX)
SELECT @result = STUFF(
( SELECT ',' + CONVERT(NVARCHAR(20), StudentId)
FROM Student
WHERE condition = abc
FOR xml path('')
)
, 1
, 1
, '')
SQL Server select results as string separated with ','
SQL Fiddle
MS SQL Server 2008 Schema Setup:
CREATE TABLE tblUsers
([name] varchar(7))
;
INSERT INTO tblUsers
([name])
VALUES
('asieh'),
('amir'),
('safoora')
;
Query 1:
SELECT STUFF((
select ','+ name
from tblUsers
FOR XML PATH('')
)
,1,1,'') AS names
Results:
| NAMES |
|--------------------|
| asieh,amir,safoora |
SQL Server convert select a column and convert it to a string
You can do it like this:
Fiddle demo
declare @results varchar(500)
select @results = coalesce(@results + ',', '') + convert(varchar(12),col)
from t
order by col
select @results as results
| RESULTS |
-----------
| 1,3,5,9 |
Convert complete result set into String within SQL (HSQLDB)
You are correct, as there is obviously more than one user and multiple rows are returned from the SELECT.
You can add LIMIT 1
to the SELECT to get the first row. You can also create a string array from the inner SELECT. See the guide.
In HSQLDB each database user has separate access rights to the tables. In a real deployment the user that inserts comments will not even see the existence of the table that contains the password, let alone select from it.
Convert SQL code (as string) into table - dynamic pivot
I got it figured out. The solution is to add ##result in the string sql script as below:
set @query = 'SELECT date, ' + @cols + ' ##result from
(
select date
, amount
, category
from temp
) x
pivot
(
max(amount)
for category in (' + @cols + ')
) p '
select * from ##result
Convert a ResultSet to a String
You can return a String in loadDatabase()
with the text that you want:
public static String loadDatabase()
Inside, instead of printing to console, you just save those results to a String:
StringBuilder resultText = new StringBuilder();
while (rs.next()) {
resultText.append("Username: ").append(rs.getString(1)).append(" Score: ").append(rs.getString(2));
}
return resultText.toString();
You would then need to call loadDatabase()
before view.getInstructions().setText("Scores: ");
and add it to that setText.
Related Topics
How to Grant the Database Owner (Dbo) the External Access Assembly Permission
In Ms SQL Server, How to "Atomically" Increment a Column Being Used as a Counter
How to Use a SQL Window Function to Calculate a Percentage of an Aggregate
Combine Multiple Rows into Multiple Columns Dynamically in SQL Server
How to Create Delphi 4 Structure to Map Column Names in Xls to Column Names in SQL
For Each Quarter Between Two Dates, Add Rows Quarter by Quarter in SQL Server
Postgresql: Check If Schema Exists
Difference Between on Clause and Using Clause in SQL
Access 2007: "Select Count(Distinct ..."
How to Copy Data from One Table to Another in Postgres Using Copy Command
How to Release Possible Postgres Row Locks
Transfer Data Between Databases with Postgresql
Error Trapping Code Using Ado Connections
How to Determine Which Columns Are Shared Between Two Tables