Selecting from a text defined value into varchar

  • I am populating a temp table with data drawn from a text defined field in a data table.

    I can get the first 8000 characters using : set tm.cntct_notes = convert(varchar(8000), notes.note)

    How do I access the next and subsequent blocks of 8000 characters from this notes entry?

    Thanks,

    Peter

  • PeterR-1037872 (7/22/2012)


    I am populating a temp table with data drawn from a text defined field in a data table.

    I can get the first 8000 characters using : set tm.cntct_notes = convert(varchar(8000), notes.note)

    How do I access the next and subsequent blocks of 8000 characters from this notes entry?

    Thanks,

    Peter

    Hey Peter,

    User SUBSTRING function for both the cases.

    --rhythmk
    ------------------------------------------------------------------
    To post your question use below link

    https://www.sqlservercentral.com/articles/forum-etiquette-how-to-post-datacode-on-a-forum-to-get-the-best-help
    🙂

  • If you want/need a more detailed response you will need to provide a more detailed post. Take a look at the first link in my signature about best practices when posting questions. The time you spend will be rewarded with tested, accurate and fast code.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Try using VARCHAR(MAX) instead for VARCHAR(8000)

    For details check this link: http://qa.sqlservercentral.com/Forums/Topic647815-145-1.aspx

    - Lokesh

    ~ Lokesh Vij


    Guidelines for quicker answers on T-SQL question[/url]
    Guidelines for answers on Performance questions

    Link to my Blog Post --> www.SQLPathy.com[/url]

    Follow me @Twitter

  • lokeshvij (7/23/2012)


    Try using VARCHAR(MAX) instead for VARCHAR(8000)

    For details check this link: http://qa.sqlservercentral.com/Forums/Topic647815-145-1.aspx

    - Lokesh

    The OP may be trying to avoid off page storage of certain parts of data which VARCHAR(MAX) would not accomplish.

    now for some quick sample code to partially answer the OP's Question (since there is no sample data or ddl this is as good as i can get)

    DECLARE @LongString VARCHAR(MAX)

    SET @LongString = (SELECT REPLICATE(CAST('abcdefghijk' AS VARCHAR(MAX)),900))

    SELECT SUBSTRING (@LongString,(8000*N) + 1 ,8000)

    FROM Tally

    WHERE N*8000 < LEN(@LongString)

    If you dont know what a tally table is you can read about them here http://qa.sqlservercentral.com/articles/T-SQL/62867/. The above code uses a 0 based tally table.


    For faster help in answering any problems Please read How to post data/code on a forum to get the best help - Jeff Moden[/url] for the best way to ask your question.

    For performance Issues see how we like them posted here: How to Post Performance Problems - Gail Shaw[/url]

    Need to Split some strings? Jeff Moden's DelimitedSplit8K[/url]
    Jeff Moden's Cross tab and Pivots Part 1[/url]
    Jeff Moden's Cross tab and Pivots Part 2[/url]

  • Problem solved - Thanks for the advice.

    The substring solution worked, but a string length of 8000 would only return 4200, so I ended up using the following:-

    settm.cntct_notes1 = substring(notes.note, 1, 4200),

    tm.cntct_notes2 = substring(notes.note, 4201, 4200),

    tm.cntct_notes3 = substring(notes.note, 8401, 4200)

    Thanks again,

    Peter

  • PeterR-1037872 (7/23/2012)


    Problem solved - Thanks for the advice.

    The substring solution worked, but a string length of 8000 would only return 4200, so I ended up using the following:-

    settm.cntct_notes1 = substring(notes.note, 1, 4200),

    tm.cntct_notes2 = substring(notes.note, 4201, 4200),

    tm.cntct_notes3 = substring(notes.note, 8401, 4200)

    Thanks again,

    Peter

    What are you basing that on. What you see on the display? The display limits how many characters will be returned. Do a LEN() of your substrings and see.

    --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)
    Intro to Tally Tables and Functions

Viewing 7 posts - 1 through 6 (of 6 total)

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