Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Conversion failed when converting from a character string to uniqueidentifier. Expand / Collapse
Author
Message
Posted Friday, September 28, 2012 6:37 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, September 05, 2013 2:25 AM
Points: 28, 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)

Post #1365837
Posted Friday, September 28, 2012 7:01 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Monday, April 14, 2014 1:34 PM
Points: 15,442, Visits: 9,588
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
Post #1365847
Posted Friday, September 28, 2012 7:38 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 9:33 AM
Points: 6,754, Visits: 12,854
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
Post #1365869
Posted Friday, September 28, 2012 7:59 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, September 05, 2013 2:25 AM
Points: 28, 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
Post #1365885
Posted Friday, September 28, 2012 8:20 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:24 PM
Points: 11,990, Visits: 11,007
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 Moden's 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)
Post #1365895
Posted Friday, September 28, 2012 8:31 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, September 05, 2013 2:25 AM
Points: 28, 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

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
Post #1365903
Posted Monday, November 12, 2012 10:43 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, December 12, 2013 12:20 PM
Points: 27, Visits: 64
@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, ','))




Post #1383777
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse