How to Union the title in sql query with data? - sql

I want the title for the SQL query.How Can I generate it? In the following code ContractID is int, Contract Number is nvarchar and ClientContractNumber is nvarchar.
I have tried the following Code
SELECT *
INTO #temp
FROM
(SELECT ContractID,ContractNumber, ClientContractNumber
FROM dbo.bsContract
WHERE ContractNumber = 'CR6359-V1') t
SELECT 'Title',' ',' '
UNION ALL
SELECT 'ContractID', 'ContractNumber', 'ClientContractNumber'
UNION ALL
SELECT *
FROM #temp
DROP TABLE #temp
The following error occurs:
Msg 245, Level 16, State 1, Line 7
Conversion failed when converting the varchar value 'Title' to data type int.
The expected output SHOULD BE:
Title
ContactID ContractNumber ClientContractNumber
----------------------------------------------------
6368 cr1234 newContract

use cast for ContractID
SELECT 'Title',' ',' '
UNION ALL
SELECT 'ContractID','ContractNumber','ClientContractNumber'
UNION ALL
SELECT cast( ContractID as varchar(50)),ContractNumber, FROM #temp
DROP TABLE #temp

You need to cast the contract ID to varchar before unioning since union requires all the datatypes to match.
Edit:
SELECT * INTO #temp FROM (SELECT cast(ContractID as varchar) ContractID,ContractNumber,ClientContractNumber
FROM dbo.bsContract WHERE ContractNumber='CR6359-V1') t
SELECT 'Title' ContractID,' ' ContractNumber,' ' ContractNumber
UNION ALL
SELECT 'ContractID' ContractID,'ContractNumber' ContractNumber,'ClientContractNumber' ContractNumber
UNION ALL
SELECT * FROM #temp
DROP TABLE #temp

Related

How to dynamically convert rows to columns in SQL Server

I want to convert rows to columns dynamically, for sample data I have given below query.
create table testtable
(
tableid int primary key identity(1,1),
tableDatetime datetime,
names varchar(50),
tablevalue decimal(18,9)
)
go
insert into testtable
select '2019-06-13 13:56:39.117', 'test1',23.45 union all
select '2019-06-13 13:56:39.117', 'test2',33.45 union all
select '2019-06-13 13:56:39.117', 'test3',10.45 union all
select '2019-06-13 13:56:39.117', 'test4',90.45 union all
select '2019-06-13 14:01:41.280', 'test1',33.45 union all
select '2019-06-13 14:01:41.280', 'test2',53.45 union all
select '2019-06-13 14:01:41.280', 'test3',41.45 union all
select '2019-06-13 14:01:41.280', 'test4',93.45 union all
select '2019-06-13 14:06:42.363', 'test1',30.45 union all
select '2019-06-13 14:06:42.363', 'test2',13.45 union all
select '2019-06-13 14:06:42.363', 'test3',23.45 union all
select '2019-06-13 14:06:42.363', 'test4',73.45
go
select * from testtable
I want to convert data in attached image format
Thanks,
You may try dynamic sql query as per your table structure.
GO
declare #query varchar(max)
set #query = (select stuff( (select distinct ',' + names from testtable for xml path ('')) ,1,1,'') as d)
declare #resquery nvarchar(max)
set #resquery = '
select * from (
select tableDatetime , names , tablevalue from testtable
) as d
pivot ( max(tablevalue) for names in ( ' + #query + ' ) ) as pv'
exec sp_executesql #resquery
GO
Please use this as per your table structure, this will create dynamic column names for your current table data. Which is further use in pivot to convert your rows into columns.
Mark it as accepted, or comment for further query.

multiple column with muptiple rows data to a single row & for the non duplicated data need concadination

Currently data is like below:-
SQL Query:-
declare #t table
(
Id int,
ReportedDate DATE,
[Name] varchar(10)
)
insert into #t
select 1,'2016-01-01','ab' union all
select 2,'2016-01-01','a' union all
select 1,'2016-01-20','hha' union all
select 2,'2016-01-20','jnsjja' union all
select 1,'2016-01-01','jsjb' union all
select 2,'2016-01-01','sjjjwb' union all
select 1,'2016-01-20','bjd' union all
select 2,'2016-01-20','bwjw'
select * from #t order by id, ReportedDate
Expected Result:-
It's working in case we have only 2 columns, one is Id & other one is anything. But for mutiple column I am unable
There are countless examples for this, however, since you have a nicely structured question which is easy to copy and paste....
Select A.ID
,A.ReportedDate
,Name = Stuff((Select Distinct ',' +Name From #t Where ID=A.ID and ReportedDate=A.ReportedDate For XML Path ('')),1,1,'')
From (Select Distinct ID,ReportedDate From #t ) A
Returns
ID ReportedDate Name
1 2016-01-01 ab,jsjb
1 2016-01-20 bjd,hha
2 2016-01-01 a,sjjjwb
2 2016-01-20 bwjw,jnsjja

Dynamic Comma Seperated string into different column

May someone please help me for this strange scenario. i have a data as given below.
DECLARE #TABLE TABLE
(
ID INT,
PHONE001 VARCHAR(500)
)
INSERT TEST
SELECT 1,'01323840261,01323844711' UNION ALL
SELECT 2,'' UNION ALL
SELECT 3,',01476862000' UNION ALL
SELECT 4,'01233625418,1223822583,125985' UNION ALL
SELECT 5,'2089840022,9.99021E+13'
and i am trying to put in seperate column for each comma value. the max number of column depends on the largest comma seperated string.
Expected Output
1|01323840261|01323844711|''
2|''|''|''
3|01476862000|''|''|
4|01233625418|1223822583|125985|
5|2089840022|9.99021E+13|''|
try
select id,T.c.value('t[1]','varchar(50)') as col1,
T.c.value('t[2]','varchar(50)') as col2 ,
T.c.value('t[3]','varchar(50)') as col3 from
(select id,cast ('<t>'+ replace(PHONE001,',','</t><t>') +'</t>'
as xml) x
from #TABLE) a cross apply x.nodes('.') t(c)

substract specific string from data sql

I am new to SQL
If I have a column like this
ID
00001234
00012345
00001235
00123456
I want to see a column of ID without '0' Like this
ID
1234
12345
1235
123456
How can I start? any advice?
In SQL Server you can use:
SELECT ID,
REPLACE(LTRIM(REPLACE(ID, '0', ' ') ), ' ', '0')
FROM mytable
The above can be easily adjusted to any other RDBMS you may use.
Cast it to Bigint and cast it back to varchar
Note:Assumption: RDBMS SQL SERVER, ID is of character type
SELECT * INTO #TAB FROM (
select '00001234' ID
UNION ALL
select '00012345'
UNION ALL
select '00001235'
UNION ALL
select '00123456'
)A
SELECT CAST(CAST(ID AS BIGINT) AS VARCHAR(50)) FROM #TAB

Conversion failed when converting the varchar value 'tblSQLAdminInventory' to data type int

Some of my previous questions may now make sense... Anyway, here's what I have:
create table ##NewTemp1
(
[TableName] [nvarchar](100),
[UniqueName] [nvarchar](100),
[FieldName] [nvarchar](100),
[TransID_80079] [nvarchar](2000),
[TransID_80080] [nvarchar](2000)
)
INSERT INTO ##NewTemp1
SELECT X.TableName, X.UniqueName, X.FieldName, X.TransID_80079, X.TransID_80080
FROM (
SELECT * FROM ##Temp1175443 UNION
SELECT * FROM ##Temp2175443 UNION
SELECT * FROM ##Temp3175443 UNION
... etc ...
SELECT * FROM ##Temp22175443 UNION
SELECT * FROM ##Temp23175443 UNION
SELECT 1 AS TableName, 1 AS UniqueName, 1 AS FieldName, 1 AS TransID_80079, 1 AS TransID_80080 WHERE 1=0
) X
I'm getting this:
Msg 245, Level 16, State 1, Line 1 Conversion failed when converting
the varchar value 'tblSQLAdminInventory' to data type int.
I purposely, specifically set up a temp table with nvarchar fields so that all values would be written to nvarchar fields. Why is it even mentioning an int data type? Can anyone help?
Try single quotes in your last select statement:
SELECT '1' AS TableName, '1' AS UniqueName, '1' AS FieldName, '1' AS TransID_80079, '1' AS TransID_80080
Also consider dropping the WHERE 1=0 or modifying it with single quotes as well.
Those are the only instances of INT that I can see in your code.
Try using quotes around the numbers in the last row:
SELECT '1' AS TableName, '1' AS UniqueName, '1' AS FieldName, '1' AS TransID_80079, '1' AS TransID_80080
The union has complicated rules for determining the type and it is probably deciding that columns should be numbers when they are really strings.