|
| 1 | +# MySQLCLRFunctions |
| 2 | +Free SQLCLR functions tested on SQL Server 2019 |
| 3 | + |
| 4 | +Simple stuff to avoid spending $500 for a two year license (https://sqlsharp.com/full/) |
| 5 | +It hasn't been updated in 2 years, so not very cool. |
| 6 | +I use UNSAFE assemblies, because me likey. |
| 7 | +Their names are a bit taxing. I call things StartsWith rather than SQL#.string_Is_A_BEGINNIN_WITH |
| 8 | +Less parameters. These are annoying to have to fill out. Rather, do it the old fashion way: Make a new function! |
| 9 | +CompareThese(a,b) |
| 10 | +BestOf3(a,b,3) |
| 11 | +Or make an agg. |
| 12 | +Pass everything NVARCHAR(MAX) for now. Speed isn't the main problem. The main problem is that SQL Server functions are severely lacking. |
| 13 | + |
| 14 | +If it's possible to think of a logical algorithm that can easily be described, then it's worth being a function. |
| 15 | + |
| 16 | +For instance, AnyOneOfTheseIsInThose, or something like that. If have a set. Are any of these items in this other list? |
| 17 | + |
| 18 | +To pass in lists, I just use delimited strings. |
| 19 | + |
| 20 | +Structure or class organization: |
| 21 | +<li> |
| 22 | + Adaptors - Converts something to something else as an electronic power adaptor would. It's not a transformation, and it's reversible. |
| 23 | +</li> |
| 24 | +<li> |
| 25 | + Compares - Comparisons. Since generics aren't supported, The name includes the type (like olden days). Often in SQL I need to get the greatest |
| 26 | + between two or a few things. Greatest between PURCHASE_DATE and QUOTE or some such. Usually between two or more events that could happen in |
| 27 | + either order. |
| 28 | +</li> |
| 29 | +<li> |
| 30 | + Environmental - I'm Windows centric. Sorry. So I try to "beep" sometimes in code, and I'm pretty sure the SQLCLR context is boxed out of |
| 31 | + beeping, so it doesn't work. But it will, someday. I want things like "You're data is ready, Captain." Might help the alertness quotient. |
| 32 | +</li> |
| 33 | +<li> |
| 34 | + FileNameExtract - Dumb name. But using .NET FileInfo is a major load for processing say millions of documents off a shared drive. |
| 35 | + I should be able to replace FileInfo with some straight-forward parsing. |
| 36 | +</li> |
| 37 | +<li> |
| 38 | + Humanization - One of the most common functions around, but never really fully cooked, is it. So this is yet-another attempt, and I may |
| 39 | + look around out there for better ones to borrow. |
| 40 | + DBAs/Developers never pause to think: Who is my audience? Do they need to see rows and rows with 30 date-time columns? If they're all |
| 41 | + sub-second from now, then say so. We need a "yada yada yada" function. If one sticks out and says "Last year", it should stick out! |
| 42 | + If there's a 9198, maybe it's really 1998? I've had SQL Server blow on DATETIME with an Oracle DATE value of 0200, a typo which Oracle tolerates |
| 43 | + in a DATE column, but SQL Server did not. DATE and DATETIME2 are better. |
| 44 | +</li> |
| 45 | +<li> |
| 46 | + NetworkCollect - Not sure what I was thinking. Collect? |
| 47 | +</li> |
| 48 | +<li> |
| 49 | + NetworkTest - I use the "Test" suffix a lot. "Ping" is important, but SQL Server feels like that's a job for System Engineers. Well, |
| 50 | + if I'm scanning my SQL Servers or a list of possible Servers, then if I try to connect to each one, there will be a lot of time outs, |
| 51 | + and the default time out is about 30 seconds, or 20. I turn that down, but if the host doesn't exist at all in Active Directory, |
| 52 | + then the timeout is defined by the OS, and SQL Server goes blithely on with waiting. |
| 53 | +</li> |
| 54 | +<li> |
| 55 | + StringExtract - This is a bit Polish-Hungarian for sorting. These are all the functions that extract something from a string or a string of |
| 56 | + substrings. Here's an example of constant use in C#, which is not really something you get in SSMS or SQLCMD. It's nice! |
| 57 | + I add functions that seem like an oversight. "LeftOf" some charactor or string. Why do I have to do |
| 58 | + CASE WHEN CHARINDEX(',', s) > 0 THEN SUBSTRING(s, CHARINDEX(',', s)-1 ELSE NULL END?????? |
| 59 | + <br><h1>ARGH!</h1> |
| 60 | +</li> |
| 61 | +<li> |
| 62 | + StringMeasure - Separate from extraction, we are instead taking measure of something, so new information is being gained, even though it |
| 63 | + is deductive information. Extracts are to avoid providing new information, but rather only provide a substring that exists within the source. |
| 64 | + <br> HowMany. Simple enough! How many commas? We are pulling in a CSV, and missing commas or extras will break it. |
| 65 | +</li> |
| 66 | +<li> |
| 67 | + StringTest - Returns 1 or 0! Very bitty. These are probably deterministic. I hope. |
| 68 | + A lot of "Is it" and "Does it" questions. |
| 69 | +</li> |
| 70 | +<li> |
| 71 | + StringTransform - Actual change being made, new information by structural abruption. |
| 72 | +</li> |
| 73 | + |
| 74 | +My favorites have to be Matches, Ping, StartsWith, EndsWith, AnyOfTheseeAreAnyOfThose, HowMany, LeftOf, LeftOfNth, LeftOfNth, IndexOfLast |
| 75 | +Also GetFirstName, Pieces, RTrimChar. |
| 76 | + |
| 77 | +I have a bunch somewhere, and I need to add more. |
| 78 | + |
| 79 | +Some ideas: |
| 80 | +- Better captures |
| 81 | +- Escape for multi-level dynamic SQL generation |
| 82 | +- A UNIX cut command for fixed-width |
| 83 | +- CSV, XML in/out |
| 84 | +- HTML table generation for emails. |
| 85 | +- Intelligent Substring Title casing, like "ROTC" is not "Rotc". |
| 86 | +- Smarter name normalization, accent removals for us Americanos. |
| 87 | +- SQLFormat? The way I like? |
| 88 | +- Some default formatters, like LogFileTimeStamp = "YYYYmmDDhh24missffffff" or some such. So I don't have to remember. |
| 89 | + |
| 90 | +- Possibly push SQL work down? Like a TRUNCATE TABLE that pre-strips FKs, truncates, reloads, and tries to add the FKs back. |
| 91 | +- Unix to/from for Active Directory columns |
| 92 | +- Natural language extraction of patterned speech. |
| 93 | + |
| 94 | + |
0 commit comments