Conversion failed when converting from a character string to uniqueidentifier.

  • 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) +','

    FROMLibertyGasGroup_MSCRM.dbo.FilteredITT_instruction I

    WHEREi.itt_jobcodeid in (select JobIDs from @Tmp)

    AND

    (i.itt_outcome = 2)

    GROUP BYi.itt_appointmentdateandtime

    selectdistincti.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

    fromLibertyGasGroup_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)

  • 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

  • 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

  • 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) +','

    FROMLibertyGasGroup_MSCRM.dbo.FilteredITT_instruction I

    WHEREi.itt_jobcodeid in (select JobIDs from @Tmp)

    AND

    (i.itt_outcome = 2)

    GROUP BYi.itt_appointmentdateandtime

    selectdistincti.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

    fromLibertyGasGroup_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

  • 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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • 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

  • @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, ','))

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply