Home » General

The text, ntext, and image data types are invalid in this subquery or aggregate expression

19 June 2009 2,439 views 2 Comments

I have a client I was building an e-commerce website for. I was developing the website on MS SQL 2005 Database, but their deployment server have MS SQL 2000. From experience I knew there are some stuffs that would not work in MS SQL 2000, so I avoid them. This one missed my eyes. I get this error message on the search results page:

The text, ntext, and image data types are invalid in this subquery or aggregate expression.

After checking I found I was trying to get a TEXT column in a subquery, and that was causing the problem. An example query is:

Select customerName,customerNote=(select top 1 note from customerNotes Where customerId=C.customerId) from Customers C

Note that the column NOTE is a text column that was causing the problem.

Solution:

The text column need to be retrieved as a varchar column. Rewriting the query this way solves the problemma:

Select customerName,customerNote=(select top 1 Cast(note As varchar(500)) from customerNotes Where customerId=C.customerId) from Customers C

You can also use Convert instead of Cast like this:

Select customerName,customerNote=(select top 1 Convert(varchar(500),note) from customerNotes Where customerId=C.customerId) from Customers C

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

2 Comments »

  • charan said:

    create procedure createnewleft1copy(@BranchId nvarchar(50))
    as
    update tblImages set imgleft1old=(select Cast(imgleft1 as image) from tblImages ) where BranchId=@BranchId

    GO
    iam using sql server 2000

    it is giving error

    Server: Msg 279, Level 16, State 3, Procedure createnewleft1copy, Line 3
    The text, ntext, and image data types are invalid in this subquery or aggregate expression.

    can you corect where iam going wrong

  • manik (author) said:

    @charan
    Here is the quote from MS:
    Automatic data type conversion is not supported for the text and image data types. You can explicitly convert text data to character data, and image data to binary or varbinary, but the maximum length is 8000.

    Also your error message says the problem.

    You can try to save the old value on a local variable, then use it to make the update.

    Hope that helps.

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.