SSMS - every SELECT statement creates extra empty line - how to suppress? - sql

If I set in SQL Management Studio to produce an output to Text (not to grid or to file) and run following:
SET NOCOUNT ON
SELECT '1'
SELECT '2'
SELECT '1'
I get this:
1
2
1
How to remove extra new line after every line?
I set as default in Tools->Options following:
1. Output format as TAB delimited
2. Excluded column headers in the result set
3. Created a New Query
UPDATE:
Previous query is shown for sake of simplicity. Please take this one as a question:
SELECT 1+2
SELECT 'A FOX'
SELECT 1,2,3
Want to see no extra line between lines, e.g:
3
A FOX
1 2 3

You are getting three separate result sets - one for each select.
In order to get a single result set (with duplicates) you need to UNION ALL the selects:
SET NOCOUNT ON
SELECT '1' UNION ALL
SELECT '2' UNION ALL
SELECT '1'
See UNION (Transact-SQL) on MSDN for details.
Update:
What you want to do is not possible. The types and numbers of the fields are different in each select - you can't UNION them.
You can't have a single result set that has different number of columns per row or with different data types per row.

Well, instead of posting comment, I decided to post answer, since it may help someone.
The output of the stored procedure I need to paste in Excel and apply "copy format" from template.
I resolved this problem this way:
Initial statement I have
SELECT 'Report Header'
SELECT ''
SELECT 'Line of report',1
SELECT 'Line of report',23
SELECT * FROM SUBREPORT
SELECT 'SUBTOTAL:',(select count(*) from another_table)
SELECT ''
SELECT 'REPORT GENERATED AT ', GetDate()
I updated to this:
SELECT 'Report Header'
SELECT '[NEWLINE]'
SELECT 'Line of report',1
SELECT 'Line of report',23
SELECT * FROM SUBREPORT
SELECT 'SUBTOTAL:',(select count(*) from another_table)
SELECT '[NEWLINE]'
SELECT 'REPORT GENERATED AT ', GetDate()
Since extra empty lines breaks formatting, I have to remove them.
I took the text output and pasted in Word.
Replaced "^p^p" with "^p", and then "[NEWLINE]" with "" (nothing).
Copy-Paste to Excel and copy format from template.
Done.
I would appreciate if someone can suggest more elegant solution.

Related

How to extract the specific part of string in SQL server?

I have a string ST0023_Lamb_Weston_2017_US in a table from particular column. While selecting the name I need to get only Lamb_Weston_2017_US. I can use
SELECT SUBSTRING('ST0023_Lamb_Weston_2017_US', 8, 20)
But there will be different names in the column. For example ,
ST0023_Lamb_Weston_2017_US
ST0053_PL_Sandbox_Dorgan_US
ST0071_EDA_Austria
ST0071_EDA_Austria
ST10338_Nestle_Soluble_Instant_Cacao_ES
So the above mentioned are the different names available. I need to remove the "ST" part and the number part till first hyphen and return name alone. Please help me with this.
Inside substring function use charindex to pick the starting position of underscore. Plus one is added with charindex to exclude the underscore position and ending position will be considered till the length of the data.
create table data
(
value varchar(100)
)
insert into data
select 'ST0023_Lamb_Weston_2017_US' union
select 'ST0053_PL_Sandbox_Dorgan_US' union
select 'ST0071_EDA_Austria' union
select 'ST0071_EDA_Austria' union
select 'ST10338_Nestle_Soluble_Instant_Cacao_ES'
go
select value, SUBSTRING(value, CHARINDEX('_',value)+1 , LEN(value)) 'Newvalue' from data

Read multiple columns from single table based on our input string

I want to select multiple columns from single table based on my given input string in sql select statement.
Example :
If input="table_medical" i want to select the columns like medi_col1,medi_col2,medi_col2
If input="table_pharmacy" i want to select the columns like medi_phar1,medi_phar2,medi_phar1
sql("select
case when $input="table_medical" then medi_col1) //like this
please help me to complete this.
If you want this in a single query, then the same number of columns is needed -- and have compatible types.
One method uses union all:
select medi_col1, medi_col2, medi_col2
from t
where #input = 'table_medical'
union all
select medi_phar1, medi_phar2, medi_phar1
from t
where #input = 'table_pharmacy';
SET #input="table_medical";
SELECT
CASE WHEN #input="table_medical" THEN medi_col1 ELSE medi_phar1 END as medi_col1,
CASE WHEN #input="table_medical" THEN medi_col2 ELSE medi_phar2 END as medi_col2
CASE WHEN #input="table_medical" THEN medi_col3 ELSE medi_phar3 END as col3
FROM MyTable
Data types for medi_col1 and medi_phar1 needs to be same (and for the rest of columns too)

How to display the whole number only if it starts with two characters otherwise leave it blank. SQL query

I need to display the whole number in a field if it starts with "AB" otherwise do not show/display the number.
Your question is missing code of how you display this (since you wrote you need to display it to the field) so i can't answer you with actual code but here is solution.
If you want to select only rows which column1 starts with AB then use LIKE function. So condition at selecting command is Select * from yourtable where column1 LIKE 'AB%'
If you already selected and displayed data, let's say in datagridview, and you want to fill textbox with string that contains AB then you would go through all rows at specific column and look for it with string.Contains("AB");
So basically you put this command in foreach loop and you have it.
I was wrong. You can use a LIKE, just not in the WHERE clause.
;WITH testdata AS (
SELECT 'aw12354' AS val UNION ALL
SELECT 'a12b344' UNION ALL
SELECT 'AB11111' UNION ALL
SELECT '11AB111' UNION ALL
SELECT '11111AB' UNION ALL
SELECT 'ab22222'
)
SELECT
CASE WHEN val LIKE 'AB%' THEN val ELSE NULL END AS valFull
, CASE WHEN val LIKE 'AB%' THEN SUBSTRING(val,3,len(val)) ELSE NULL END AS valNums
FROM testdata
;
You can also use CLR to build a regex solution, but that is a LOT more involved.

Is using sequence in db2 statements possible?

I am trying to execute following statement in DB2.
This works well.
SELECT NEXT VALUE FOR SCPYMNT.REM_QUERY_NO_SEQ
FROM sysibm.sysdummy1
However, this throws a database error.
SELECT (
CASE WHEN PYMT_SYS = 1 THEN NEXT VALUE FOR SCPYMNT.REM_QUERY_NO_SEQ
WHEN PYMT_SYS = 2 THEN 'dummy'
else 'dummy'
END )
FROM sysibm.sysdummy1
So Db2 gives the error below.
Category Timestamp Message
Statusbar 18.04.2016 11:47:39 DB2 Database Error: ERROR [428F9] [IBM][DB2] SQL0348N "NEXT VALUE FOR SCPYMNT.REM_QUERY_NO_SEQ" cannot be specified in this context. SQLSTATE=428F9
It seems to me there is not a syntax error.Does Db2 not let such queries that consists of case conditions and sequence?
#MichaelTiefenbacher,I put select examples as a demonstration.(What I am really trying to achieve is something like below.
SELECT NAME, QUERYNO
FROM FINAL TABLE (INSERT INTO EMPSAMP (NAME, SALARY, QUERYNO)
VALUES('Mary Smith', 35000.00,
CASE WHEN PYMT_SYS = 1 THEN NEXT VALUE FOR REM_SEQ
CASE WHEN PYMT_SYS = 2 NEXT VALUE FOR EFT_SEQ
));
I think question is more clearer now.
Sequences can be used to generate unique keys or numbers when inserting data into tables.
They are not used to generate unique numbers when selecting data.
For that you could either retrieve the field from the table where you used the sequence at insert time or you can use row_number() in the SELECT.
It also would be helpful to tell a little more what you want to achieve.
I found out that the answer is "No" according to IBM documentation.
NEXT VALUE expressions cannot be specified in the following contexts:
CASE expression
Here is the link
Insert from a union of selects:
SELECT NAME, QUERYNO
FROM FINAL TABLE
(
INSERT INTO EMPSAMP
SELECT
'Mary Smith', 35000.00, NEXT VALUE FOR REM_SEQ
FROM SYSIBM.SYSDUMMY1
WHERE PYMT_SYS = 1
UNION ALL
'Mary Smith', 35000.00, NEXT VALUE FOR EFT_SEQ
FROM SYSIBM.SYSDUMMY1
WHERE PYMT_SYS = 2
)

Sql Server 2005 Copy Multiple Result Sets

How Can i copy multiple result sets at the same time?
for example if i were to execute the below code:
select 'a'
select 'b'
select 'c'
how can i copy all 3 results so i can then paste them into excel? I tried holding down shift and selecting all 3 result sets but it only copies the last one i select.
Try this:
select 'a'
union all
select 'b'
union all
select 'c'
This will return one result set that you can copy and paste wherever you'd like.
You can select
Query'>'Results to'> 'Results to text' or
Query'>'Results to'> 'Results to file'