First Two Words

  • Steve Collins wrote:

    If ChatGPT set that up and it was quick and easy that's amazing.  Before it writes queries it would be nice if it did boring and boilerplate work

    There is a new skill you can acquire called "Prompt Engineering" which apparently (according to some YouTube videos) attracts very high salaries - lol (I don't think so).  It's basically how you ask ChatGPT questions in a way to get better answers. I just added "Please create a test table (#temp) with column Col1 and some test data and the query I need to solve it" to the end of the OP's question and pasted it into ChatGPT:

    Screenshot 2023-03-10 194810

    Screenshot 2023-03-10 194853

    The query it produced was actually the simplest one yet:

    SELECT 
    LEFT(Col1, CHARINDEX(' ', Col1 + ' ', CHARINDEX(' ', Col1) + 1) - 1) AS FirstTwoWords
    FROM
    #temp
  • Awesome test rig, Jonathan!

    Here's another method that I think you'll appreciate performance wise. (didn't see the post above when I posted).

        SET STATISTICS TIME,IO OFF;
    DROP TABLE IF EXISTS #4;
    PRINT 'Start ***************************************************** "Lost Wax Method"';
    SET STATISTICS TIME,IO ON;
    SELECT first2_in_order = RTRIM(LEFT(t.Col1,ISNULL(NULLIF(CHARINDEX(' ',t.Col1,CHARINDEX(' ',t.Col1)+1),0),8000)))
    INTO #4
    FROM #temp t;
    SET STATISTICS TIME,IO OFF;

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • The only problem that the ChatGPT version has is when the first "word" is 8000 characters long or the combination of the first and second words is 8000 characters long.  Then you'll get the dreaded error of ...

    Msg 537, Level 16, State 2, Line 86

    Invalid length parameter passed to the LEFT or SUBSTRING function.

    While that's "Not likely", I hate finding out when it does occur by accident.

     

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jonathan AC Roberts wrote:

    It's basically how you ask ChatGPT questions in a way to get better answers.

    That means that all of those people are doomed.  😀  Stop and think about the questions they ask here and on other forums.  No effort on the part of many.  They're doomed. 😀

     

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • If their plan fails, they can always go back to tipping Dogecoin or peddling courses on how to get rich quick by buying cheap stuff on AliBaba and reselling it on Fulfilled by Amazon

  • Jonathan AC Roberts wrote:

    If their plan fails, they can always go back to tipping Dogecoin or peddling courses on how to get rich quick by buying cheap stuff on AliBaba and reselling it on Fulfilled by Amazon

    rofl

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 6 posts - 16 through 20 (of 20 total)

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