Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Union error due to field types Expand / Collapse
Author
Message
Posted Friday, May 2, 2014 2:02 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, September 18, 2014 10:14 AM
Points: 36, Visits: 52

I am trying to run the query below. Between the %% will be certain text that we are looking for.

The problem is the union blows up because optionlongdesc is a ntext field. Any thoughts on how to make this work?

Thanks


select OptionCode, OptionDesc, 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, 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
Post #1567170
Posted Friday, May 2, 2014 2:13 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, October 2, 2014 11:25 AM
Points: 238, Visits: 288
If you change your UNION to a UNION ALL, it will work. Note that it will not filter out duplicates like UNION will.
Post #1567174
Posted Friday, May 2, 2014 2:15 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, September 18, 2014 10:14 AM
Points: 36, Visits: 52
Thanks but I need the duplicates filtered or it will make a mess of the results for some items.

Post #1567176
Posted Friday, May 2, 2014 2:24 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:04 PM
Points: 13,085, Visits: 12,553
DaveK2014 (5/2/2014)

I am trying to run the query below. Between the %% will be certain text that we are looking for.

The problem is the union blows up because optionlongdesc is a ntext field. Any thoughts on how to make this work?

Thanks




Yes, change your table to use nvarchar(max) instead. the (n)text datatype has been deprecated. It is a pain to work with. You can't index it and you have to constantly convert it to a max datatype in order to use a where clause.

http://msdn.microsoft.com/en-us/library/ms187993.aspx

If you can't change the table you will have to do a convert in your query. You shouldn't have any issue changing the column to nvarchar(max) from ntext.


_______________________________________________________________

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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1567178
Posted Friday, May 2, 2014 2:25 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, September 18, 2014 10:14 AM
Points: 36, Visits: 52
Thanks sean

its a third party Database so I cant change it there.

How do I got about changing it in the query?

Thanks
Post #1567179
Posted Friday, May 2, 2014 2:44 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, September 30, 2014 4:29 PM
Points: 2,844, Visits: 1,155
SELECT  OptionCode, OptionDesc, 
OptionLongDesc = CAST(OptionLongDesc AS NVARCHAR(MAX)) , UnitPrice
FROM tbMasterOption
WHERE ...




Post #1567182
Posted Friday, May 2, 2014 2:58 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, September 18, 2014 10:14 AM
Points: 36, Visits: 52
Still get the same error

Msg 421, Level 16, State 1, Line 1
The ntext data type cannot be selected as DISTINCT because it is not comparable.
Post #1567184
Posted Monday, May 5, 2014 7:33 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, August 26, 2014 10:44 AM
Points: 48, Visits: 157
DaveK2014 (5/2/2014)
Still get the same error

Msg 421, Level 16, State 1, Line 1
The ntext data type cannot be selected as DISTINCT because it is not comparable.


Very quick and dirty test...I created 2 test tables, 3 columns each; one had int, varchar(3), and ntext. The other had int, varchar(3), and nvarchar(max).

This worked to union them:


select col1,col2,cast(col3 as nvarchar(max)) as col3 from dbo.test1
union
select col1,col2,col3 from dbo.test2

I hope this helps!
Post #1567502
Posted Monday, May 5, 2014 7:40 AM This worked for the OP Answer marked as solution


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:04 PM
Points: 13,085, Visits: 12,553
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 [RIGHT (OptionCode, 1)]. 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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1567508
Posted Monday, May 5, 2014 7:45 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, September 18, 2014 10:14 AM
Points: 36, Visits: 52
That was it I didn't have it on the first section.

I don't know of any other way to preform what I need to do other then those options.

I need the right as there are about 20,000 rows. The way it is setup is each item could have two prices based on what the customer selects. So if they pick level 1 they get price one, level 2 gets price 2. This is determined by the last letter of the option code. Any thoughts on that?

The wild card is to make it a bit easier for the user and not having to type out the everything.

It is a third party table so I don't to mess with anything and use full text index searching.
Post #1567510
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse