![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
|
From Data Order by IDNO |
#2
| |||
| |||
|
|
Hello, I am trying to get data from a table that looks like this: Database = Data Table = PersonalData IDNO FName LName Expiration 12345 John Doe 10/24/2006 23547 John Doe 10/25/2007 There are about 4000 records. The IDNO is unique for each, but many of the FName and LNames are the same. It is a long story, but there are many, many repeat names for different people in our database. Due to the way our database was set up (don't ask) a normal SELECT will pull the same row many times. Till now, I have been copying the data to an Excel spreadsheet and using Filter, but I would like to do this in SQL if possible. I wrote a query that basically looks like this: Select Distinct IDNO, FName, LName, Expiration From Data Order by IDNO I get the error message "The text, ntext, or image data type cannot be selected as DISTINCT." Is there any other way to pull the record only once? I hope this is enough data. If not I will supply more. I am a long time IT guy, but only using SQL for the past month or so. Also, due to the nature of our database I cannot create tables or copy to new columns. Thanks for any help, Rodjk #613 |
#3
| |||
| |||
|
|
Hi Posting DDL would help and may have shown you what was wrong http://www.aspfaq.com/etiquette.asp?id=5006. At a guess FName and/or LName is defined as being text or ntext, which is overkill for what they will hold. Try: SELECT DISTINCT IDNO, CAST(FName AS varchar(50)) AS FName, CAST(LName AS varchar(50)) AS LName, Expiration FROM Data ORDER BY IDNO John |

#4
| |||
| |||
|
|
John Bell wrote: Hi Posting DDL would help and may have shown you what was wrong http://www.aspfaq.com/etiquette.asp?id=5006. At a guess FName and/or LName is defined as being text or ntext, which is overkill for what they will hold. Try: SELECT DISTINCT IDNO, CAST(FName AS varchar(50)) AS FName, CAST(LName AS varchar(50)) AS LName, Expiration FROM Data ORDER BY IDNO John Hello John, Sorry about the DDL. I will have to try that next time. But your solution worked! Thanks! I have messed with that for 2 days. If you have time, could you explain what you did? I thought there was an issue with the IDNO, why does adjusting the names make it work? Thanks again! If you are ever in Kabul, I owe you...uh, a coke...as we cannot get beer. ![]() Rodjk #613 SNIP my original Post |
#5
| |||
| |||
|
|
Hi The error message tells you almost everything. "The text, ntext, or image data type cannot be selected as DISTINCT." Distinct applies to all columns not just the first, and the first one looking like a number and the last one looking like a date, it was probably one or both of the other two columns are text or ntext data types. Using the CAST function will convert the datatype of the expression (column) to the one you specified i.e. from text to varchar. |
|
It would be better if you address the choice of datatypes in your table definition rather than in the query. Check out the ALTER TABLE command in books online. |
|
John rjkardo (AT) gmail (DOT) com> wrote in message news:1134916769.920816.88340 (AT) g49g2000cwa (DOT) googlegroups.com... John Bell wrote: Hi Posting DDL would help and may have shown you what was wrong http://www.aspfaq.com/etiquette.asp?id=5006. At a guess FName and/or LName is defined as being text or ntext, which is overkill for what they will hold. Try: SELECT DISTINCT IDNO, CAST(FName AS varchar(50)) AS FName, CAST(LName AS varchar(50)) AS LName, Expiration FROM Data ORDER BY IDNO John Hello John, Sorry about the DDL. I will have to try that next time. But your solution worked! Thanks! I have messed with that for 2 days. If you have time, could you explain what you did? I thought there was an issue with the IDNO, why does adjusting the names make it work? Thanks again! If you are ever in Kabul, I owe you...uh, a coke...as we cannot get beer. ![]() Rodjk #613 SNIP my original Post |
#6
| |||
| |||
|
|
Hi The error message tells you almost everything. "The text, ntext, or image data type cannot be selected as DISTINCT." Distinct applies to all columns not just the first, and the first one looking like a number and the last one looking like a date, it was probably one or both of the other two columns are text or ntext data types. Using the CAST function will convert the datatype of the expression (column) to the one you specified i.e. from text to varchar. It would be better if you address the choice of datatypes in your table definition rather than in the query. Check out the ALTER TABLE command in books online. |
|
John rjkardo (AT) gmail (DOT) com> wrote in message news:1134916769.920816.88340 (AT) g49g2000cwa (DOT) googlegroups.com... John Bell wrote: Hi Posting DDL would help and may have shown you what was wrong http://www.aspfaq.com/etiquette.asp?id=5006. At a guess FName and/or LName is defined as being text or ntext, which is overkill for what they will hold. Try: SELECT DISTINCT IDNO, CAST(FName AS varchar(50)) AS FName, CAST(LName AS varchar(50)) AS LName, Expiration FROM Data ORDER BY IDNO John Hello John, Sorry about the DDL. I will have to try that next time. But your solution worked! Thanks! I have messed with that for 2 days. If you have time, could you explain what you did? I thought there was an issue with the IDNO, why does adjusting the names make it work? Thanks again! If you are ever in Kabul, I owe you...uh, a coke...as we cannot get beer. ![]() Rodjk #613 SNIP my original Post |
#7
| |||
| |||
|
|
John Bell wrote: Hi The error message tells you almost everything. "The text, ntext, or image data type cannot be selected as DISTINCT." Distinct applies to all columns not just the first, and the first one looking like a number and the last one looking like a date, it was probably one or both of the other two columns are text or ntext data types. Using the CAST function will convert the datatype of the expression (column) to the one you specified i.e. from text to varchar. It would be better if you address the choice of datatypes in your table definition rather than in the query. Check out the ALTER TABLE command in books online. Ok, I pulled a DDL. This is what it looks like: CREATE TABLE [PersonalData] ( [GUID] [uniqueidentifier] NOT NULL CONSTRAINT [DF__PersonalDa__GUID__7CC477D0] DEFAULT (newid()), [ENROLL_DATE] [datetime] NULL , [Title] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [LNAME] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [LNAME_SOUNDEX] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [FNAME] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [FNAME_SOUNDEX] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [MNAME] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [MNAME_SOUNDEX] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , There is LOTS more, so I did not put the whole thing. But these should (I hope) show enough about the FNAME and LNAME fields. It seems that character type is NVARCHAR. I found this: http://msdn.microsoft.com/library/de...ca-co_2f3o.asp I am reading it, but am not sure if I understand it yet. It is late at night here, I will check again in the morning. I just want to make sure I did not change anything... Sorry for being such a newb... Thanks again, Rodjk #613 Thanks again, Rodjk #613 John rjkardo (AT) gmail (DOT) com> wrote in message news:1134916769.920816.88340 (AT) g49g2000cwa (DOT) googlegroups.com... John Bell wrote: Hi Posting DDL would help and may have shown you what was wrong http://www.aspfaq.com/etiquette.asp?id=5006. At a guess FName and/or LName is defined as being text or ntext, which is overkill for what they will hold. Try: SELECT DISTINCT IDNO, CAST(FName AS varchar(50)) AS FName, CAST(LName AS varchar(50)) AS LName, Expiration FROM Data ORDER BY IDNO John Hello John, Sorry about the DDL. I will have to try that next time. But your solution worked! Thanks! I have messed with that for 2 days. If you have time, could you explain what you did? I thought there was an issue with the IDNO, why does adjusting the names make it work? Thanks again! If you are ever in Kabul, I owe you...uh, a coke...as we cannot get beer. ![]() Rodjk #613 SNIP my original Post |
#8
| |||
| |||
|
|
Hi There are no text, ntext or image columns in the DDL posted, but it does not show IDNO or Expiration. Which version of SQL Server (SELECT @@VERSION) are you using? John |
|
rjkardo (AT) gmail (DOT) com> wrote in message news:1134921106.330690.278440 (AT) g14g2000cwa (DOT) googlegroups.com... John Bell wrote: Hi The error message tells you almost everything. "The text, ntext, or image data type cannot be selected as DISTINCT." Distinct applies to all columns not just the first, and the first one looking like a number and the last one looking like a date, it was probably one or both of the other two columns are text or ntext data types. Using the CAST function will convert the datatype of the expression (column) to the one you specified i.e. from text to varchar. It would be better if you address the choice of datatypes in your table definition rather than in the query. Check out the ALTER TABLE command in books online. Ok, I pulled a DDL. This is what it looks like: CREATE TABLE [PersonalData] ( [GUID] [uniqueidentifier] NOT NULL CONSTRAINT [DF__PersonalDa__GUID__7CC477D0] DEFAULT (newid()), [ENROLL_DATE] [datetime] NULL , [Title] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [LNAME] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [LNAME_SOUNDEX] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [FNAME] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [FNAME_SOUNDEX] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [MNAME] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [MNAME_SOUNDEX] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , There is LOTS more, so I did not put the whole thing. But these should (I hope) show enough about the FNAME and LNAME fields. It seems that character type is NVARCHAR. I found this: http://msdn.microsoft.com/library/de...ca-co_2f3o.asp I am reading it, but am not sure if I understand it yet. It is late at night here, I will check again in the morning. I just want to make sure I did not change anything... Sorry for being such a newb... Thanks again, Rodjk #613 Thanks again, Rodjk #613 John rjkardo (AT) gmail (DOT) com> wrote in message news:1134916769.920816.88340 (AT) g49g2000cwa (DOT) googlegroups.com... John Bell wrote: Hi Posting DDL would help and may have shown you what was wrong http://www.aspfaq.com/etiquette.asp?id=5006. At a guess FName and/or LName is defined as being text or ntext, which is overkill for what they will hold. Try: SELECT DISTINCT IDNO, CAST(FName AS varchar(50)) AS FName, CAST(LName AS varchar(50)) AS LName, Expiration FROM Data ORDER BY IDNO John Hello John, Sorry about the DDL. I will have to try that next time. But your solution worked! Thanks! I have messed with that for 2 days. If you have time, could you explain what you did? I thought there was an issue with the IDNO, why does adjusting the names make it work? Thanks again! If you are ever in Kabul, I owe you...uh, a coke...as we cannot get beer. ![]() Rodjk #613 SNIP my original Post |
#9
| |||
| |||
|
|
John Bell wrote: Hi There are no text, ntext or image columns in the DDL posted, but it does not show IDNO or Expiration. Which version of SQL Server (SELECT @@VERSION) are you using? John Hello, Ok, it says "Microsoft SQL Server 2000 - 8.00.760 I did not include the whole thing, but all of the other fields are either NVARCHAR or DateTime. Rodjk #613 rjkardo (AT) gmail (DOT) com> wrote in message news:1134921106.330690.278440 (AT) g14g2000cwa (DOT) googlegroups.com... John Bell wrote: Hi The error message tells you almost everything. "The text, ntext, or image data type cannot be selected as DISTINCT." Distinct applies to all columns not just the first, and the first one looking like a number and the last one looking like a date, it was probably one or both of the other two columns are text or ntext data types. Using the CAST function will convert the datatype of the expression (column) to the one you specified i.e. from text to varchar. It would be better if you address the choice of datatypes in your table definition rather than in the query. Check out the ALTER TABLE command in books online. Ok, I pulled a DDL. This is what it looks like: CREATE TABLE [PersonalData] ( [GUID] [uniqueidentifier] NOT NULL CONSTRAINT [DF__PersonalDa__GUID__7CC477D0] DEFAULT (newid()), [ENROLL_DATE] [datetime] NULL , [Title] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [LNAME] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [LNAME_SOUNDEX] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [FNAME] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [FNAME_SOUNDEX] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [MNAME] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [MNAME_SOUNDEX] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , There is LOTS more, so I did not put the whole thing. But these should (I hope) show enough about the FNAME and LNAME fields. It seems that character type is NVARCHAR. I found this: http://msdn.microsoft.com/library/de...ca-co_2f3o.asp I am reading it, but am not sure if I understand it yet. It is late at night here, I will check again in the morning. I just want to make sure I did not change anything... Sorry for being such a newb... Thanks again, Rodjk #613 Thanks again, Rodjk #613 John rjkardo (AT) gmail (DOT) com> wrote in message news:1134916769.920816.88340 (AT) g49g2000cwa (DOT) googlegroups.com... John Bell wrote: Hi Posting DDL would help and may have shown you what was wrong http://www.aspfaq.com/etiquette.asp?id=5006. At a guess FName and/or LName is defined as being text or ntext, which is overkill for what they will hold. Try: SELECT DISTINCT IDNO, CAST(FName AS varchar(50)) AS FName, CAST(LName AS varchar(50)) AS LName, Expiration FROM Data ORDER BY IDNO John Hello John, Sorry about the DDL. I will have to try that next time. But your solution worked! Thanks! I have messed with that for 2 days. If you have time, could you explain what you did? I thought there was an issue with the IDNO, why does adjusting the names make it work? Thanks again! If you are ever in Kabul, I owe you...uh, a coke...as we cannot get beer. ![]() Rodjk #613 SNIP my original Post |
#10
| |||
| |||
|
|
Hi That makes the error message confusing! What happens if you force NVARCHAR? SELECT DISTINCT IDNO, CAST(FName AS nvarchar(50)) AS FName, CAST(LName AS nvarchar(50)) AS LName, Expiration FROM Data ORDER BY IDNO John |
|
rjkardo (AT) gmail (DOT) com> wrote in message news:1134959153.559411.299600 (AT) g49g2000cwa (DOT) googlegroups.com... John Bell wrote: Hi There are no text, ntext or image columns in the DDL posted, but it does not show IDNO or Expiration. Which version of SQL Server (SELECT @@VERSION) are you using? John Hello, Ok, it says "Microsoft SQL Server 2000 - 8.00.760 I did not include the whole thing, but all of the other fields are either NVARCHAR or DateTime. Rodjk #613 rjkardo (AT) gmail (DOT) com> wrote in message news:1134921106.330690.278440 (AT) g14g2000cwa (DOT) googlegroups.com... John Bell wrote: Hi The error message tells you almost everything. "The text, ntext, or image data type cannot be selected as DISTINCT." Distinct applies to all columns not just the first, and the first one looking like a number and the last one looking like a date, it was probably one or both of the other two columns are text or ntext data types. Using the CAST function will convert the datatype of the expression (column) to the one you specified i.e. from text to varchar. It would be better if you address the choice of datatypes in your table definition rather than in the query. Check out the ALTER TABLE command in books online. Ok, I pulled a DDL. This is what it looks like: CREATE TABLE [PersonalData] ( [GUID] [uniqueidentifier] NOT NULL CONSTRAINT [DF__PersonalDa__GUID__7CC477D0] DEFAULT (newid()), [ENROLL_DATE] [datetime] NULL , [Title] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [LNAME] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [LNAME_SOUNDEX] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [FNAME] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [FNAME_SOUNDEX] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [MNAME] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [MNAME_SOUNDEX] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , There is LOTS more, so I did not put the whole thing. But these should (I hope) show enough about the FNAME and LNAME fields. It seems that character type is NVARCHAR. I found this: http://msdn.microsoft.com/library/de...ca-co_2f3o.asp I am reading it, but am not sure if I understand it yet. It is late at night here, I will check again in the morning. I just want to make sure I did not change anything... Sorry for being such a newb... Thanks again, Rodjk #613 Thanks again, Rodjk #613 John rjkardo (AT) gmail (DOT) com> wrote in message news:1134916769.920816.88340 (AT) g49g2000cwa (DOT) googlegroups.com... John Bell wrote: Hi Posting DDL would help and may have shown you what was wrong http://www.aspfaq.com/etiquette.asp?id=5006. At a guess FName and/or LName is defined as being text or ntext, which is overkill for what they will hold. Try: SELECT DISTINCT IDNO, CAST(FName AS varchar(50)) AS FName, CAST(LName AS varchar(50)) AS LName, Expiration FROM Data ORDER BY IDNO John Hello John, Sorry about the DDL. I will have to try that next time. But your solution worked! Thanks! I have messed with that for 2 days. If you have time, could you explain what you did? I thought there was an issue with the IDNO, why does adjusting the names make it work? Thanks again! If you are ever in Kabul, I owe you...uh, a coke...as we cannot get beer. ![]() Rodjk #613 SNIP my original Post |
![]() |
| Thread Tools | |
| Display Modes | |
| |