• Scott Coleman (5/2/2014)


    SELECT OptionCode, OptionDesc,

    OptionLongDesc = CAST(OptionLongDesc AS NVARCHAR(MAX)) , UnitPrice

    FROM tbMasterOption

    WHERE ...

    Did you use the same cast in BOTH queries?

    create table tbMasterOption

    (

    OptionCode nvarchar(10),

    OptionDesc nvarchar(50),

    OptionLongDesc ntext,

    UnitPrice numeric(9, 2)

    )

    select OptionCode, OptionDesc, CAST(OptionLongDesc AS NVARCHAR(MAX)) as OptionLongDesc, UnitPrice from tbMasterOption

    where (RIGHT (OptionCode, 1) = 'E' or RIGHT (Optioncode,1) = 'U') and

    OptionDesc like '%%' and

    Optiondesc like '%%' and

    OptionDesc like '%%'

    union

    select OptionCode, OptionDesc, CAST(OptionLongDesc AS NVARCHAR(MAX)) as OptionLongDesc, UnitPrice from tbMasterOption

    where (RIGHT (OptionCode, 1) = 'E' or RIGHT (Optioncode,1) = 'U') and

    OptionLongDesc like '%%' and

    OptionLongDesc like '%%' and

    OptionLongDesc like '%%'

    order by OptionCode

    BTW, this query has some serious performance issue potentials. The first one is using a function on a column in your where predicate

    . That is nonSARGable and will perform and index scan at best. The second issue is using leading wildcards for OptionDesc. The same issue of nonSARGability applies.

    _______________________________________________________________

    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/