Concatenation in SQL Server - sql

I am getting an error
Error converting data type varchar to numeric
when I run this code:
SELECT CONCAT('$', ' ', a * 0.05 + b * 12) AS value
Variables a and b are defined as money datatype

Try casting the arithmetic expression to text before including it as a parameter in the CONCAT() function:
SELECT CONCAT('$', ' ', CAST(a * 0.05 + b * 12 AS varchar(20))) AS value;

Related

Multiple integer rows into delimited comma for In Clause

I am able to get the values of loanids which are rows of integers into a comma delimited list. However I am unable to convert this list to be used for an In Clause
Declare #LoanNums AS NVARCHAR(Max) = ''
select
DISTINCT
#LoanNums =
stuff((
select ',' + CONVERT(NVARCHAR(max), u.LoanId)
from #tmpLoan u
where u.LoanId = u.LoanId
order by u.LoanId
for xml path('')
),1,1,'')
from #tmpLoan
This now outputs 132254986,132255002,132255018,249736374,249892143 which looks fine however when I put this variable into an IN clause
DECLARE #SQL AS NVARCHAR(MAX) = ''
SELECT #SQL = 'Select * from myOtherTable Where LoanId in (''' + #LoanNums + ''')'
if outputs this SQL
Select * from myOtherTable WHERE LoanId in
('132254986,132255002,132255018,249736374,249892143')
and I get the error
Conversion failed when converting the varchar value
'132254986,132255002,132255018,249736374,249892143' to data type int
How can I create a proper formatted comma delimited list to be used correctly in an IN Clause?
isn't Select * from myOtherTable WHERE LoanId in
('132254986,132255002,132255018,249736374,249892143') needed to be
Select * from myOtherTable WHERE LoanId in
('132254986','132255002','132255018','249736374','249892143');
^ ^ ^ ^ ^ ^ ^ ^

Dynamic pivot with time datatype in SQL Server

SELECT
PARTNO, BATCHNO, ' + #IdleHoursColumns + '
INTO
TEMPTABLE_IH
FROM
[dbo].[PRODUCTION_IH]
PIVOT
(SUM(IDLETIME))
FOR REASON IN (' + #IdleHoursColumns + ')) AS P'
--printed query
SELECT
PARTNO, BATCHNO, [POWERCUT]
INTO
TEMPTABLE_IH
FROM
[dbo].[PRODUCTION_IH]
PIVOT
(SUM(IDLETIME)
FOR REASON IN ([POWERCUT])) AS P
How to use dynamic pivot with time datatype in SQL Server? I tried to move my data into a temp table and display the row values to as columns dynamically. The query works perfectly for integer datatype columns. But I got an error
Operand data type time is invalid for sum operator
I tried with cast function and Datediff but it is also throwing errors.
If your datatype is not numeric then you should use MAX or MIN instead of SUM
SELECT PARTNO,BATCHNO, ' + #IdleHoursColumns + '
INTO TEMPTABLE_IH
FROM [dbo].[PRODUCTION_IH]
PIVOT( MAX(IDLETIME))
FOR REASON IN (' + #IdleHoursColumns + ')) AS P'
Try the following:
SELECT T1.PARTNO, T1.BATCHNO, T2.[POWERCUT] FROM (
SELECT PARTNO,BATCHNO,[POWERCUT]
--INTO TEMPTABLE_IH
FROM [DBO].[PRODUCTION_IH]
PIVOT(MAX(IDLETIME)
FOR REASON IN ([POWERCUT])) AS P)T1
JOIN
(
SELECT PARTNO,BATCHNO, SUM(DATEDIFF(MINUTE, 0, IDLETIME)) / 60.0 AS [POWERCUT]
FROM [DBO].[PRODUCTION_IH]
GROUP BY PARTNO,BATCHNO
) T2
ON T1.PARTNO = T2.PARTNO AND T1.BATCHNO = T2.BATCHNO
It will give you total time in hours. You can modify it to include all "Reason" columns.
HTH
Thanks.

How to Add all values given in comma separated in sql

How to get the SUM of values of one column in table. Now I want get the SUM of 8 and 0 if I use query write below.
SELECT SUM(Items) FROM dbo.CustomSplit(#AssigneeProgress,',')
this gives an error
Operand data type varchar is invalid for sum operator.
My code is:
DECLARE #AssigneeProgress AS VARCHAR(100)
SELECT
#AssigneeProgress = STUFF((SELECT ',' + CAST(ISNULL((((TblAssignments.PlannedDuration * 100) / 300) * TblAssignments.Progress) / 100,0) AS VARCHAR(100))
FROM TblTasks,TblAssignments
WHERE TblTasks.TaskId = TblAssignments.AssignmentEntityId
AND TblAssignments.AssignmentEntity = 'Task'
AND AssignmentStatus NOT IN ('Deleted','Cancelled')
AND TblTasks.TaskId = 63 FOR XML PATH('')), 1, 1,'')
SELECT * FROM dbo.CustomSplit(#AssigneeProgress,',')
This query gives me the result in table like
Items
8
0
Does your method CustomSplit return a table having the column Items of type varchar?
If yes, you can convert the column:
SELECT SUM(cast(Items as int)) FROM dbo.CustomSplit(#AssigneeProgress,',')
Try to cast the Items that you get back from your split function to an INT type before summing:
SELECT
SUM(CAST(Items AS INT))
FROM
dbo.CustomSplit(#AssigneeProgress,',')
The CustomSplit function most likely returns varchar(x) items....

I can't get this error fixed : conversion failed when converting the nvarchar value

I am a newby at sql and can find the problem with my query, I am getting this error:
Msg 245, Level 16, State 1, Line 10
Conversion failed when converting the nvarchar value '00999-00-210312-11' to data type int.
Declare #Referenceid Int
SET #Referenceid = (
SELECT TOP 1 (CASE WHEN ad.ReferenceID LIKE '%NO ID%' THEN (RIGHT(MAX(ad.ReferenceID),2)+10)END)
FROM Campaign.dbo.Assets_DailyBulkImport AS ad
WHERE (ad.ReferenceID IS NOT NULL)
GROUP BY ad.ReferenceID
ORDER BY ad.ReferenceID DESC);
Select (c.Note + '' + '999' + N'-' + '00' + N'-' +
(SELECT REPLACE(CONVERT(VARCHAR(8), GETDATE(), 3), '/', '') AS MMDDYY) + N'-' + #Referenceid)
FROM Assets_DailyBulkImport AS a INNER JOIN
Controls_Profiles AS c ON a.Profile = c.Venture
where a.ReferenceID=#ReferenceID
it says that '00999-00-210312-11' is a string value you cannot convert that in integer value. as you can see - is part of data so it must be string i.e varchar its not integer value that the reason its giving error.
so to resolve you error you need to change this line
(SELECT REPLACE(CONVERT(VARCHAR(8), GETDATE(), 3), '/', '') AS MMDDYY) + N'-'
+ cast(#Referenceid as varchar(10))
in your code #Referenceid is integer value you cannot directly append it you first need to convert it like cast(#Referenceid as varchar(10))

Conversion error in SQL Server?

In my SQL query:
insert into Tbl_EmpMovement_T values (
'004101',
'2011-8-26',
CONVERT(datetime,convert(varchar(10),'2011-8-26',120)+' ' +
CONVERT(varchar(2),SUBSTRING('8:16',1,2))+':'+
convert(varchar(2),SUBSTRING('8:16',4,2))+':00.000',120),
CONVERT(datetime,convert(varchar(10),'2011-8-26',120)+' ' +
CONVERT(varchar(2),SUBSTRING('6:02',1,2))+':'+
convert(varchar(2),SUBSTRING('6:02',4,2))+':00.000',120)
)
I am getting this error
Conversion failed when converting date and/or time from character
string.
I would rather try this:
insert into Tbl_EmpMovement_T values (
'004101',
'2011-8-26',
CAST ('2011-8-26' + ' ' +
( SUBSTRING('16:02',0, PATINDEX('%:%', '16:02')+1)) +
( SUBSTRING('16:02',PATINDEX('%:%', '16:02')+1, 2)) AS datetime),
CAST ('2011-8-26' + ' ' +
( SUBSTRING('16:02',0, PATINDEX('%:%', '16:02')+1)) +
( SUBSTRING('16:02',PATINDEX('%:%', '16:02')+1, 2)) AS datetime)
)
This way, you wont have a problem when the time part moves between single and double digit hours.