Page 1 of 1

SQL Problem - Incorrect syntax near....

Posted: Tue Oct 05, 2010 1:08 am
by jpraveen
Hi all

The below query when executed in the database, i am getting the error:

Incorrect syntax near 'Sun'
select Distinct
ProviderNumber,substring(printerexception,1,50) as PrinterException
from
providerpmg,AZ_locations

where
(PrinterException like 'Mon. - Fri.: 8:00 a.m. - 8:00 p.m.; Sat. - Sun.: 8:00 a.m. - 4:00 p.m.'
or PrinterException like 'Sun.-Sat. 9:00 a.m.. - 10:00 p.m. or
PrinterException like 'Urgent Care (Sun. - Sat.: 12:00p.m. - 10:00 p.m.) ph # 623-977-7211)' )
the error is in 'Urgent Care (Sun. - Sat.: 12:00p.m. - 10:00 p.m.) ph # 623-977-7211)'

the data type of PrinterException is Text

so can anyone suggest how to rectify the error?

Posted: Tue Oct 05, 2010 1:29 am
by wernerg_at
Hi,

I think a closing quote is missing after 10:00 p.m.

Try changing

or PrinterException like 'Sun.-Sat. 9:00 a.m.. - 10:00 p.m. or

To

or PrinterException like 'Sun.-Sat. 9:00 a.m.. - 10:00 p.m.' or

BR

Werner

Posted: Tue Oct 05, 2010 1:36 am
by jpraveen
HI Werner

i had tried with all the options like removing Closed bracket ')'

but in the database the value is
Urgent Care (Sun. - Sat.: 12:00p.m. - 10:00 p.m.) ph # 623-977-7211)
and the field is text

and if i remove substring
it is showing error like

Msg 8163, Level 16, State 3, Line 1
The text, ntext, or image data type cannot be selected as DISTINCT.
but i want only distinct records

Posted: Tue Oct 05, 2010 1:46 am
by wernerg_at
Msg 8163, Level 16, State 3, Line 1
The text, ntext, or image data type cannot be selected as DISTINCT.
but i want only distinct records
Considering this a SQL Server DBMS you need to cast your ntext column to nvarchar:

Select distinct with ntext fields
Pasted from <http://social.msdn.microsoft.com/forums ... 930e288ad5>

Posted: Tue Oct 05, 2010 2:01 am
by ArndW
Could the error be something as banal as the single "#" character being misinterpreted? Does the error go away when you remove the "#" (even if no rows are returned)?

Posted: Tue Oct 05, 2010 2:43 am
by jpraveen
hi

though i change into

Cast(PrinterException as NVarchar(50))

i am getting the same error
Urgent Care (Sun. - Sat.: 12:00p.m. - 10:00 p.m.) ph # 623-977-7211)
the above quote is a text value and field name is PrinterException

it doesn't matter i remove # or ph # 623-977-7211

Posted: Tue Oct 05, 2010 2:55 am
by wernerg_at
Did you cast the fields in the where clause as well. if not please try it.

Note:
(NTEXT/TEXT wont allow any string manipulation (including comparision))

Posted: Tue Oct 05, 2010 3:03 am
by wernerg_at
FYI:
Since ntext, text, and image data types marked as deprecated you should try to use different types or consider changing them at the next opportunity

For further details refer to the SQL Server 2008 R2 Documentation

ntext, text, and image (Transact-SQL)
Pasted from <http://msdn.microsoft.com/en-US/library/ms187993.aspx>

...
Important

ntext, text, and image data types will be removed in a future version of MicrosoftSQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them
...

Re: Incorrect syntax near....

Posted: Thu Oct 07, 2010 12:47 am
by HariK
select Distinct
ProviderNumber,substring(printerexception,1,50) as PrinterException
from
providerpmg,AZ_locations

where
(PrinterException = 'Mon. - Fri.: 8:00 a.m. - 8:00 p.m.; Sat. - Sun.: 8:00 a.m. - 4:00 p.m.'
or PrinterException = 'Sun.-Sat. 9:00 a.m.. - 10:00 p.m.' or
PrinterException = 'Urgent Care (Sun. - Sat.: 12:00p.m. - 10:00 p.m.) ph # 623-977-7211)' )
Could you try this. LIKE will work but is not required in your case as you are not using any wild characters.

Re: Incorrect syntax near....

Posted: Thu Oct 07, 2010 1:06 am
by wernerg_at
HariK wrote:
select Distinct
ProviderNumber,substring(printerexception,1,50) as PrinterException
from
providerpmg,AZ_locations

where
(PrinterException = 'Mon. - Fri.: 8:00 a.m. - 8:00 p.m.; Sat. - Sun.: 8:00 a.m. - 4:00 p.m.'
or PrinterException = 'Sun.-Sat. 9:00 a.m.. - 10:00 p.m.' or
PrinterException = 'Urgent Care (Sun. - Sat.: 12:00p.m. - 10:00 p.m.) ph # 623-977-7211)' )
Could you try this. LIKE will work but is not required in your case as you are not using any wild characters.
As already mentioned ntext/text fields neither support distinct nor string comparison (not even a '=' comparison).

I still believe that casting all the text/ntext fields (within select and where clauses) should solve this issue.

Posted: Thu Oct 07, 2010 10:12 pm
by truenorth
I agree with this diagnosis:
wernerg_at wrote:Hi,

I think a closing quote is missing after 10:00 p.m.

Try changing

or PrinterException like 'Sun.-Sat. 9:00 a.m.. - 10:00 p.m. or

To

or PrinterException like 'Sun.-Sat. 9:00 a.m.. - 10:00 p.m.' or

BR

Werner
Have you looked into it?

Posted: Thu Oct 07, 2010 10:19 pm
by ray.wurlod
I wonder if it's the "#" character ("not equal to")?