SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Conversion failed when converting from a character string to uniqueidentifier.


Conversion failed when converting from a character string to uniqueidentifier.

Author
Message
Qutip
Qutip
SSC Veteran
SSC Veteran (206 reputation)SSC Veteran (206 reputation)SSC Veteran (206 reputation)SSC Veteran (206 reputation)SSC Veteran (206 reputation)SSC Veteran (206 reputation)SSC Veteran (206 reputation)SSC Veteran (206 reputation)

Group: General Forum Members
Points: 206 Visits: 265
Hello I keep getting the above error when running this script.

any ideas? It's driving me nuts! It's probably something simple but it's been a very long week!

It seems to be something to do with line 17, it's the bold text for ease
DECLARE @dates VARCHAR(100)   
declare @jobid VARCHAR(max) =
('101038,
101039,
101040,
101044')

DECLARE @Tmp TABLE(JobIDs VARCHAR(40))

INSERT INTO @Tmp (JobIds)
SELECT CAST(Items AS VARCHAR(40)) FROM InterfaceSystem.dbo.fn_SplitString(@jobid,',')





SELECT @dates = COALESCE (@dates ,'') + CONVERT(VARCHAR(100),i.itt_appointmentdateandtime, 103) +','
FROM LibertyGasGroup_MSCRM.dbo.FilteredITT_instruction I
WHERE i.itt_jobcodeid in (select JobIDs from @Tmp)
AND
(i.itt_outcome = 2)
GROUP BY i.itt_appointmentdateandtime

select distinct i.itt_outcome
, j.itt_id
, substring(@dates, 1, charindex(',', @dates)-1) as date1
, substring(@dates, 12, charindex(',', @dates)-1) as date2
, substring(@dates, 23, charindex(',', @dates)-1) as date3
, substring(@dates, 34, charindex(',', @dates)-1) as date4
from LibertyGasGroup_MSCRM.dbo.FilteredITT_instruction i
inner join LibertyGasGroup_MSCRM.dbo.FilteredITT_job j on j.ITT_jobId = i.itt_jobcodeid

where (i.itt_outcome = 2)
and i.itt_jobcodeid in (select JobIDs from @Tmp)


GSquared
GSquared
SSC Guru
SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)

Group: General Forum Members
Points: 60063 Visits: 9730
Not really enough here to help you out. At the very least, I'd need to see the definition of your split string function, and probably the table definitions.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)

Group: General Forum Members
Points: 43011 Visits: 20015
What's the datatype of column [itt_jobcodeid], table [LibertyGasGroup_MSCRM].dbo.[FilteredITT_instruction]?

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Qutip
Qutip
SSC Veteran
SSC Veteran (206 reputation)SSC Veteran (206 reputation)SSC Veteran (206 reputation)SSC Veteran (206 reputation)SSC Veteran (206 reputation)SSC Veteran (206 reputation)SSC Veteran (206 reputation)SSC Veteran (206 reputation)

Group: General Forum Members
Points: 206 Visits: 265
Hi,

the datatype of the all of the id columns are uniqueidentifiers within the database. However these values are being passed to the SP from SSRS as a multi value parameter value. Hence the split function.

I've tried casting the id's as uniqueidentifiers as seen below but with no luck.

DECLARE @dates VARCHAR(100)   
declare @jobid uniqueidentifier =
('d63b6001-59fd-e111-94eb-001517a81d9d,
a149467b-b9f5-e111-94eb-001517a81d9d,
441ca81a-bdf5-e111-94eb-001517a81d9d,
d28923b2-bcf5-e111-94eb-001517a81d9d')

DECLARE @Tmp TABLE(JobIDs VARCHAR(40))

INSERT INTO @Tmp (JobIds)
SELECT CAST(Items AS uniqueidentifier) FROM InterfaceSystem.dbo.fn_SplitString(@jobid,',')





SELECT @dates = COALESCE (@dates ,'') + CONVERT(VARCHAR(100),i.itt_appointmentdateandtime, 103) +','
FROM LibertyGasGroup_MSCRM.dbo.FilteredITT_instruction I
WHERE i.itt_jobcodeid in (select JobIDs from @Tmp)
AND
(i.itt_outcome = 2)
GROUP BY i.itt_appointmentdateandtime

select distinct i.itt_outcome
, j.itt_id
, substring(@dates, 1, charindex(',', @dates)-1) as date1
, substring(@dates, 12, charindex(',', @dates)-1) as date2
, substring(@dates, 23, charindex(',', @dates)-1) as date3
, substring(@dates, 34, charindex(',', @dates)-1) as date4
from LibertyGasGroup_MSCRM.dbo.FilteredITT_instruction i
inner join LibertyGasGroup_MSCRM.dbo.FilteredITT_job j on j.ITT_jobId = i.itt_jobcodeid

where (i.itt_outcome = 2)
and i.itt_jobcodeid in (select JobIDs from @Tmp)



I was playing round with the shorter values to see if that would make any difference.

this is typically how the values would be passed back from SSRS

'd63b6001-59fd-e111-94eb-001517a81d9d,
a149467b-b9f5-e111-94eb-001517a81d9d,
441ca81a-bdf5-e111-94eb-001517a81d9d,
d28923b2-bcf5-e111-94eb-001517a81d9d'

unless of course there is a better way to pass them through..?


here's the split function



ALTER function [dbo].[fn_SplitString]
(
@String nvarchar(MAX),
@Delimiter char(1)
) Returns @Results Table (Items nvarchar(4000))
As
Begin
Declare @Index int
Declare @Slice nvarchar(4000)

Select @Index = 1

If @String Is NULL Return
While @Index != 0
Begin
Select @Index = CharIndex(@Delimiter, @String)

If (@Index != 0)
Select @Slice = left(@String, @Index - 1)
else
Select @Slice = @String
Insert into @Results(Items) Values (@Slice)
Select @String = right(@String, Len(@String) - @Index)

If Len(@String) = 0 break
End
Return
End


thanks in advance
Sean Lange
Sean Lange
SSC Guru
SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)

Group: General Forum Members
Points: 65088 Visits: 17979
The problem is because you have a list of numbers and you are trying to compare that to a uniqueidentifier.

You insert this into a table.
('101038, 101039, 101040, 101044')


INSERT INTO @Tmp (JobIds)
SELECT CAST(Items AS VARCHAR(40)) FROM InterfaceSystem.dbo.fn_SplitString(@jobid,',')



But then you try to find them in your table. Assuming itt_jobcodeid is a uniqueidentifier you are comparing apples to oranges here. It will attempt to cast your values to a uniqueidentifier and those are not valid values.


and i.itt_jobcodeid in (select JobIDs from @Tmp)



As a side note, I would highly recommend reading the article linked in my signature about splitting strings. It will blow the doors off the while loop splitter.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Qutip
Qutip
SSC Veteran
SSC Veteran (206 reputation)SSC Veteran (206 reputation)SSC Veteran (206 reputation)SSC Veteran (206 reputation)SSC Veteran (206 reputation)SSC Veteran (206 reputation)SSC Veteran (206 reputation)SSC Veteran (206 reputation)

Group: General Forum Members
Points: 206 Visits: 265
Thanks Sean,

I've tried it a few ways casting as unique identifiers and varchars and none of them seem to be working. I was only playing round with the shorter values and forgot to edit them out of my post w00t

I think I may be barking up the wrong tree with this solution anyway but I'll need to split the values out anyway so I'll read the split article shortly.


edit: Thanks, you were all right. Apologies, it's a friday afternoon.
Regards
sivapragasam2000
sivapragasam2000
Valued Member
Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)

Group: General Forum Members
Points: 74 Visits: 83
@Qutip
I also face the same issue with GUIDs.
Use Jeff's Splitter. It is awesome.
I converted the GUID column to varchar and it worked. Try it out

cast(<GUIDColumn> as varchar(max)) IN (SELECT Item FROM dbo.udf_Split(@ParamVariable, ','))
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search