Does ChatGPT Know SQL?
The short answer is “Yes, but not entirely”. While ChatGPT may not be proficient in all aspects of SQL (actually, many aspects of SQL), it can generate executable code and perform certain tasks. However, I found it unreliable when I asked about various database topics, such as normalization forms and implicit conversion. However, it was able to properly explain a recursive statement that created a Mandelbrot set and a recursive query that solved Sudoku without informing it of the SQL’s purpose. I’m sure AI will grow exponentially here in the next few months and years, so I won’t lament everything that it couldn’t get correct.
However, one example that ChatGPT could do correctly was when I asked it to convert a string input into a separated list. One notable example of ChatGPT’s usefulness was its ability to quickly generate code to convert the string input “%34.45%aa6%4%xyz%1.23%” into a separated list of integers and real numbers: “34.45, 6, 4, 1.23”.
While this task may have taken a human developer longer to complete, ChatGPT solved it quickly and efficiently. ChatGPT’s knowledge of SQL is useful for certain tasks, but it is important to keep its limitations in mind.
Here is the code that it generated.
--CREATE FUNCTION extract_numbers(@input VARCHAR(MAX)) --RETURNS VARCHAR(MAX) --AS BEGIN DECLARE @output VARCHAR(MAX) = ''; DECLARE @input VARCHAR(MAX) = '%34.45%aa6%4%xyz%1.23%'; DECLARE @char CHAR(1); DECLARE @temp VARCHAR(MAX) = ''; DECLARE @i INT = 1; WHILE @i <= LEN(@input) BEGIN SET @char = SUBSTRING(@input, @i, 1); IF (@char BETWEEN '0' AND '9') OR (@char = '.') BEGIN SET @temp = @temp + @char; END ELSE BEGIN IF LEN(@temp) > 0 BEGIN IF LEN(@output) > 0 BEGIN SET @output = @output + ', '; END SET @output = @output + @temp; SET @temp = ''; END END SET @i = @i + 1; END IF LEN(@temp) > 0 BEGIN IF LEN(@output) > 0 BEGIN SET @output = @output + ','; END SET @output = @output + @temp; END SELECT @output; END