Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. CREATE FUNCTION getTotalWorkingSeconds(@machineList varchar(255))
  2. RETURNS int
  3. AS
  4. BEGIN
  5. declare @res int
  6. Select @res = Sum(DURATION) From PROCESSDATA where MACHINEID in (@machineList)
  7. return @res
  8. END
  9.  
  10. SELECT dbo.getTotalWorkingSeconds('3,2')
  11.  
  12. result; Conversion failed when converting the varchar value '3,2' to data type int.
  13.  
  14. SELECT dbo.getTotalWorkingSeconds(''''+'3,2'+'''')
  15.  
  16. result; Conversion failed when converting the varchar value ''3,2'' to data type int.
  17.  
  18. CREATE FUNCTION Split
  19. (
  20. @delimited nvarchar(max),
  21. @delimiter nvarchar(100)
  22. ) RETURNS @t TABLE
  23. (
  24. -- Id column can be commented out, not required for sql splitting string
  25. id int identity(1,1), -- I use this column for numbering splitted parts
  26. val nvarchar(max)
  27. )
  28. AS
  29. BEGIN
  30. declare @xml xml
  31. set @xml = N'<root><r>' + replace(@delimited,@delimiter,'</r><r>') + '</r></root>'
  32.  
  33. insert into @t(val)
  34. select
  35. r.value('.','varchar(max)') as item
  36. from @xml.nodes('//root/r') as records(r)
  37.  
  38. RETURN
  39. END
  40. GO
  41.  
  42. CREATE FUNCTION getTotalWorkingSeconds(@machineList varchar(255))
  43. RETURNS int
  44. AS
  45. BEGIN
  46. declare @t table (val nvarchar(100))
  47. insert into @t select * from dbo.split(@machineList,',')
  48.  
  49. declare @res int
  50. Select @res = Sum(DURATION) From PROCESSDATA where MACHINEID in (select val from @t)
  51. return @res
  52. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement