Add header title above the query result in SQL Server - sql-server

How can I add heading title for each query?
I want to display it as a report with a heading above the query result of the table.
Something like this
HEADING
_____________________________
Alexis M. Smith | PHP 140,500
Johnny K. Black | PHP 50,000
James P. Blonde | PHP 30,000
I am using sql server management 2012 Studio and vb.net 2010
I don't have Business Intelligence to create a report from the sql server to vb.net 2010

You may can't add header in your query result in SQL-Server and show the result in your application..
But if you are asking to show the result in SQL-Server only, then this might help you..
--You have to add one select query before your actual query like this.
select '' as 'Your Heading Here' where 1!=1
select * from your_table

Select b.a AS Heading
FROM
( Select Concat('Mr. Xyz', ' | ', 'PHP' , ' | ' , '50000') AS a
) B
This is the closest I could think.

Related

SQL Server 2014 Management Studio and excel 2013, some rows have misplaced columns

When I copy results with headers in SQL Server 2014 and paste into Excel 2013, save results as, or even just ctrl+c and ctrl+v into Excel, some rows will be split into two rows, when they should only be one.
Database Entry
colA colB colC
------------------------------------
1 hello 'return'ay test
2 test1 test
Excel paste from 2008:
colA colB colC
------------------------------------
1 hello ay test
2 test1 test
Excel paste from 2014:
colA colB colC
------------------------------------
1 hello
ay test
2 test1 test
I've found that it's most likely caused by users entering a 'return' key press on the front-end which is fine. If I use SQL Server 2008, they all stay within the same row.
Is there an option to allow SQL Server 2014 to act like SQL Server 2008 when coming across a 'return' in a single column?
Edit:
Is there a solution that doesn't involve me writing sql commands replacing every output column's tabs and carriage returns? Or going over every table's columns? This is viable for the project I'm working on now but to do this for every procedure already made and any future projects would be a HUGE project in itself.
Try replacing Tabs and Carriage Returns with spaces in your results.
SELECT REPLACE(REPLACE(colA,CHAR(13),' '),CHAR(9),' ') AS 'colA'
, REPLACE(REPLACE(colB,CHAR(13),' '),CHAR(9),' ') AS 'colB'
, REPLACE(REPLACE(colC,CHAR(13),' '),CHAR(9),' ') AS 'colC'
FROM myTable
Carriage returns (13) and Line feeds (10) are the usual culprits as you suspect, slight alteration to #EMUEVIL solution
SELECT REPLACE(REPLACE(colA,CHAR(13),''),CHAR(10),'') AS 'colA'
, REPLACE(REPLACE(colB,CHAR(13),''),CHAR(10),'') AS 'colB'
, REPLACE(REPLACE(colC,CHAR(13),''),CHAR(10),'') AS 'colC'
FROM myTable
Solution:
I downloaded and installed sql server management studio 2016
SQL server management studio 2016
Under Tools > Options > Query Results > SQL Server > Results to Grid
There is a checkbox that says Retain CR/LF on copy or save, make sure that is checked.
All grid results should be fine now for excel.

Oracle/SQL Server Database Link Date Format Issue

I have an Oracle hs database link set up between SQL Server 2012 and Oracle 11g.
When Selecting date columns in Oracle from the SQL Server database the date comes through fine but as soon as it has to pass through any function then the date gets cropped from 10 to 5 characters.
For example:
Select Input_Date from schema.table#Database
would return 2000-08-18
Select Len(Input_Date) from schema.table#Database
would return 5
(Select Input_Date from schema.table#Database1
Union
Select Input_Date from schema.table#Database2)
would return 2000-
I am at a loss at what to do, at first the select statement also returned 2000- but then I changed the NLS_DATE_FORMAT parameter which allows a view of the full date in a select statement but has not fixed any of the other issues.
When I select dump(input_date,1016) from schema.table#Database I get
Typ=1, Len=10, CharacterSet=AL16UTF16.
I would really appreciate some help as there seems to be very little information about this online.
Many thanks.

SQL Server accessing rows

I have a database where one table is as follows:
Owner Type | Owner Name
---------------------------------
Testing | abc, def, xyz
Testing2 | ppp
Testing3 | jkl, mno, pqr
In the SQL Report (SSRS), I would like to display something like this:
Testing owners = abc, def, xyz
Testing2 owners = ppp
Testing3 owners = jkl,m mno, pqr
How do I build a report in this way, using SQL Server Report Builder?
I understand that a simply query would work using the regular SQL Query mode. However, the difficulty I am facing is that there is only a single table cell where I need to enter the expression to be evaluated (there are more things in the table and the report, this is only a subset). I'm not sure how I can get that done.
There are at least 2 ways to do this
I would recommend doing the catenation in the report itself. Assuming you have already used the wizard to create the dataset, and the wizard has both raw columns in your report already, right click on the OwnerType detail row text box and select Expression (fx). You can then project the required display, e.g.:
=Fields!Owner_Type.Value + " owners = " + Fields!Owner_Name.Value
And then change the column name and delete the second column entirely.
The other way to do this is by projecting this directly in a Sql Query, e.g. by providing the following query by using the configure option on the DataSource:
select Owner_Type + ' owners = ' + Owner_Name as OwnerRow
from [dbo].[Own];
(you could also create a proc or view in SqlServer to do this projection and then bind the report to the proc / view).
If you
SELECT [Owner Type], [Owner Name] FROM Table
You can group by [Owner type] in a table/matrix in your SSRS report and get something that looks like
Testing
Testing2
Testing3
Then you can add what you need after that on a new column f.ex. But grouping is the way to display all [Owner type].
Edit: Suggestion to filter:

How to make a morphological search?

Good day.
I have query:
SELECT *
FROM Firm
WHERE name LIKE 'АВТОМОБИЛЬ%'
Tell me please how use morphological search with this query on MsSQL 2008 ?
Declare #find NVarchar(20)='test'
SELECT *
FROM Firm
WHERE name LIKE '%'+#find+'%' OR phone LIKE '%'+#find+'%'
Just run the below Query
SELECT *
FROM Firm
WHERE name LIKE N'АВТОМОБИЛЬ%'
-- ^ Magical Code
SQL Fiddle

How can I concatinate a subquery result field into the parent query?

DB: Sql Server 2008.
I have a really (fake) groovy query like this:-
SELECT CarId, NumberPlate
(SELECT Owner
FROM Owners b
WHERE b.CarId = a.CarId) AS Owners
FROM Cars a
ORDER BY NumberPlate
And this is what I'm trying to get...
=> 1 ABC123 John, Jill, Jane
=> 2 XYZ123 Fred
=> 3 SOHOT Jon Skeet, ScottGu
So, i tried using
AS [Text()] ... FOR XML PATH('') but that was inlcuding weird encoded characters (eg. carriage return). ... so i'm not 100% happy with that.
I also tried to see if there's a COALESCE solution, but all my attempts failed.
So - any suggestions?
Answering an old post, just thought it needed an update for newer versions of SQL Server:
For SQL Server 2017 use STRING_AGG(expression, seperator)
GROUP_CONCAT is MySQL.
Prior to SQL 2017, you can also do something like (snipped from our current code base on SQL Server 2016):
SELECT CarId, NumberPlate,
(STUFF(( SELECT ', ' + b.Owner
FROM Owners b
WHERE b.CarId = a.CarId
FOR XML PATH('')
)
,1,2,'')) AS Owners
FROM Cars a
ORDER BY NumberPlate
Links to STRING_AGG
https://database.guide/the-sql-server-equivalent-to-group_concat/
https://learn.microsoft.com/en-us/sql/t-sql/functions/string-agg-transact-sql?view=sql-server-2017
Link to STUFF:
https://learn.microsoft.com/en-us/sql/t-sql/functions/stuff-transact-sql?view=sql-server-2017
and finally links to FOR XML:
https://learn.microsoft.com/en-us/sql/relational-databases/xml/for-xml-sql-server?view=sql-server-2017
Try the solution to this question:
How to create a SQL Server function to "join" multiple rows from a subquery into a single delimited field?
:)
Use GROUP_CONCAT
SELECT CarId, NumberPlate
(SELECT GROUP_CONCAT(Owner)
FROM Owners b
WHERE b.CarId = a.CarId) AS Owners
FROM Cars a
ORDER BY NumberPlate

Resources