/ ** == ** 1. Custom function for group BY error ** == ** / - a simple function crete function f_str (@str sysname, @ i int) Returns char (1) as begin Return (Substring (@ Str, @ i, 1)) EndGo
- The following is a test
- Define Test Data Declare @t Table (Name Sysname) Insert Into @t Values ('ABCD')
- There is a group by the case, the result is incorrect a = dbo.f_str (name, 1), b = dbo.f_str (name, 2) from @tgroup by dbo.f_str (name, 1), dbo.f_str (Name ,2)
- There is no group by the situation, the result is correct Select a = dbo.f_str (name, 1), b = dbo.f_str (name, 2) from @TGO
- Delete Custom Function DROP FUNCTION F_STR
/ * - Test results A b ---- ---- a a
(The number of rows affects is 1 line)
A b ---- ---- a b
(The number of rows affects is 1 line) - * /