In SQL,How to pick columns which doesnt have 0 value? - sql

I have pivot query which will result one row of record. I will have to further filter that one row of record. One row of record has hour based columns and so, i have 24 columns for each hour.
How to pick columns which has only values
Lets say we have 5 columns
Col1 Col2 Col3 Col4 Col5
100 0 0 20 0
Col1, Col4 are eligible. I need total these two columns.
Col1 Col4 Total
100 20 120
create table #t
(
Col0 int,
Col1 int,
Col2 int,
Col3 int,
Col4 int,
Col5 int,
Col6 int,
Col7 int,
Col8 int,
Col9 int,
Col10 int,
Col11 int
)
insert into #t values
(0,100,0,0,0,0,20,10,0,0,0,0)
select * from #t
-- Expected Result
select Col1, Col6, Col7 from #t

You can not do that using a static pivot table, dynamic perhaps. It is not known when a pivot table is defined if all the values are null or not and by the time the data is piped in it is too late. You can either create a reporting project that supports omitting empty column groups or look for a more dynamic query alternative.

Related

Separated string into specific temp table colums

I am using a RIGHT(LEFT()) method of stripping a string as each character needs to be put into its own holder so I can access it and use it for a report (each character needs to be in its own box for some reason).
There are 16 characters usually but for space and to save repition I've slimmed down the code.
What I am trying to do is put the separated character value into the corresponding column of the temp table - how is this best achieved?
I have no other use for this data once used I'll destroy it.
Code
CREATE table #StringSeparate
(
col1 varchar(1),
col2 varchar(1),
col3 varchar(1),
col4 varchar(1),
col5 varchar(1),
col6 varchar(1),
col7 varchar(1),
col8 varchar(1),
)
declare #string varchar(16)
set #string = 'tpg22052015-1204'
SELECT
LEFT(#string,1),
RIGHT(LEFT(#string,2),1),
RIGHT(LEFT(#string,3),1),
RIGHT(LEFT(#string,4),1),
RIGHT(LEFT(#string,5),1),
RIGHT(LEFT(#string,6),1),
RIGHT(LEFT(#string,7),1),
RIGHT(LEFT(#string,8),1)
INTO
#String Separate
Just do it like:
CREATE table #StringSeparate
(
col1 varchar(1),
col2 varchar(1),
col3 varchar(1),
col4 varchar(1),
col5 varchar(1),
col6 varchar(1),
col7 varchar(1),
col8 varchar(1),
)
INSERT INTO #StringSeparate
SELECT
LEFT(#string,1),
RIGHT(LEFT(#string,2),1),
RIGHT(LEFT(#string,3),1),
RIGHT(LEFT(#string,4),1),
RIGHT(LEFT(#string,5),1),
RIGHT(LEFT(#string,6),1),
RIGHT(LEFT(#string,7),1),
RIGHT(LEFT(#string,8),1)
Or don't create temp table and do this:
SELECT
LEFT(#string,1) col1,
RIGHT(LEFT(#string,2),1) col2,
RIGHT(LEFT(#string,3),1) col3,
RIGHT(LEFT(#string,4),1) col4,
RIGHT(LEFT(#string,5),1) col5,
RIGHT(LEFT(#string,6),1) col6,
RIGHT(LEFT(#string,7),1) col7,
RIGHT(LEFT(#string,8),1) col8
INTO
#StringSeparate
It will automatically create that temp table, because INTO creates table.
Depending on you RDBMS I suppose I might prefer SUBSTRING:
INSERT INTO #StringSeparate
SELECT
LEFT(#string,1),
SUBSTRING(#string,2,1),
SUBSTRING(#string,3,1),
...
RIGHT(#string,1)
I made a big insert of your statement.
INSERT INTO #StringSeparate
VALUES
((LEFT(#string,1)),
(RIGHT(LEFT(#string,2),1)),
(RIGHT(LEFT(#string,3),1)),
(RIGHT(LEFT(#string,4),1)),
(RIGHT(LEFT(#string,5),1)),
(RIGHT(LEFT(#string,6),1)),
(RIGHT(LEFT(#string,7),1)),
(RIGHT(LEFT(#string,8),1)))

How to get NULL as values for invalid column

I'm using Sql Server 2012. Consider the table schema to be,
create table A (col1 int, col2 int)
I'm trying to execute this query,
select col1, col2, col3, col4 from A
I get execution error as col3 and col4 are not in table.
But is there any way, these 2 columns can be displayed in result with NULL as value for every row, even though it's not available in the table?
Use an alias for each one of these 2 columns:
select col1, col2, null as col3, null as col4 from A
Try below select query..
select col1, col2, null as col3, null as col4 from A
Cast Null as desired data type:
select col1, col2, cast(null as int) as col3, cast(null as int) as col4 from A

Select Column is its contain value SQL Server

I have one Audit Table which have Multiple Column,
Lets Say
Create table TestTable ( ID int, Col1 varchar(10), Col2 varchar(10), Col3 varchar(10), Col4 varchar(10), Col5 varchar(10), Col6 varchar(10), Col7 varchar(10));
insert into TestTable values(1,'Ram',null,null,null,null,null,null);
insert into TestTable values(2,null,1,null,'2',null,null,null);
insert into TestTable values(3,null,1,null,'2',null,null,null);
Now I want IF User run below Query
Select *[]* From TestTable where ID=1
Then Output should come like this.
**ID Col1
1 Ram**
and User run below query
Select *[]* From TestTable where ID=2
Then Output should come like this.
**ID Col2 Col4
1 1 2
Column is should not be static, If have value in row that column should come.
Any Idea how to get that results, I need to select the columns dynamically.
It is not impossible to do, but the query to generate that kind of result would be complex and require dynamic generation of the SQL statement.
Filtering which columns should be displayed should rather be the job of your frontend. It would also be hard for any other system to rely on a result set with a dynamic number of column.

T-SQL Stored Procedure [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a script that is currently importing data from one table and inserting in to another table. Now I have to modify the script in such a way that if the value of the field is A then the code must appear in the column 10 else if B then the code should appear in Column 50.
Table 1:
Id Field 1 Field 2 Field 3 Field 4
1 A
2 B
Table 2:
Id Field 1 Field 2 Field 3 Field 4....Field 10.....Field 50
1 0/Null
2 1
Could anyone suggest me a case statement to implement this requirement.
Any advice is greatly appreciated!
Thank You, New Bee
You could use a case expression when selecting the data: http://technet.microsoft.com/en-us/library/ms181765.aspx
On the insert for Field 10 you can use a CASE statement, i.e.
CASE Field2 WHEN 'A' THEN '0\Null' ELSE '' END
and similarly for your Field 50
You can write the T-SQL with case statement which is:
`
Create table #T(
col1 int, col2 varchar(2), col3 int, col4 int, col5 int, col6 int, col7 int, col8 int,
col9 int, col10 int, col11 int)
GO
Create table #T1(
col1 int, col2 varchar(2), col3 int, col4 int, col5 int, col6 int, col7 int, col8 int,
col9 int, col10 int, col11 int, )
Insert into #t
Select 1,'B',5,null,null,null,null,null,null,null,null union all
Select 2,'A',5,null,null,null,null,null,null,null,null union all
Select 112,'B',5,null,null,null,null,null,null,null,null union all
Select 41,'B',5,null,null,null,null,null,null,null,null union all
Select 15,'A',5,null,null,null,null,null,null,null,null
Select * from #t
Insert into #t1
Select col1, col2, col3,col4,col5,col6,col7, col8
col9,CASE WHEN COL2 ='A' Then Col2 Else 'B' End col10, col11
from #t
`

Switching 1 row with few columns into 1 column with few rows in MS SQL SERVER

i've got 1 row with many columns:
col1|col2|col3|...
and i want to have 1 column with many rows, like that:
col1
col2
col3
..
UNPIVOT if you're using version 2005+
http://www.tsqltutorials.com/unpivot.php
If SQL Server 2000 or lower...
How to rotate a table in SQL Server: http://support.microsoft.com/kb/175574
Take a look at UNPIVOT:
CREATE TABLE Table1 (col1 INT, col2 INT, col3 INT, col4 INT);
INSERT INTO Table1 (col1, col2, col3, col4) VALUES (1,2,3,4);
SELECT col, value FROM Table1
UNPIVOT (value FOR col IN (col1, col2, col3, col4)) AS unpvt
Result:
col value
col1 1
col2 2
col3 3
col4 4
If you don't want to know which value came from which column, only select value:
SELECT value FROM Table1
UNPIVOT (value FOR col IN (col1, col2, col3, col4)) AS unpvt