?
Solved

I received an error using SQL Server 2008. The error is Msg 102, Level 15. Do you know how I can resolve this error?

Posted on 2016-08-21
6
Medium Priority
?
36 Views
Last Modified: 2016-08-21
I try and execute the following stored procedure and receive the following error using SQL Server 2008.
The table layout follows.
Do you know how I can resolve this error?

Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '.'.

SELECT DISTINCTROW ImportMonthlyChecksEntity.amt1,
Format$([ImportMonthlyChecksEntity].[dateFile],'mmmm yyyy') AS [dateFile By Month],
ImportMonthlyChecksEntity.branch, Sum(ImportMonthlyChecksEntity.amt2) AS [Sum Of amt2],
Count(*) AS [Count Of ImportMonthlyChecksEntity]
FROM ImportMonthlyChecksEntity
GROUP BY ImportMonthlyChecksEntity.amt1,
Format$([ImportMonthlyChecksEntity].[dateFile],'mmmm yyyy'),
ImportMonthlyChecksEntity.branch,
Year([ImportMonthlyChecksEntity].[dateFile])*12+DatePart('m',[ImportMonthlyChecksEntity].[dateFile])-1;


ImportMonthlyChecksEntity

bankname                            varchar(53)               allow nulls
banknumber                        varchar(19)               allow nulls
amt1                                      varchar(6)                 allow nulls
amt2                                      nvarchar(MAX)         allow nulls
datefile                                  datetime                   allow nulls  
dateimported                       datetime                   allow nulls  
importedby                           varchar(50)               allow nulls
branch                                   varchar(50)               allow nulls
0
Comment
Question by:zimmer9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 41764437
For starters, in these situations it would greatly help if you could place your code in a CODE block (comment toolbar), and when an error occurs double-click on the error, watch the cursor jump to the offending line, and tell us what line that is.

Looking..
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 41764439
By any chance did you copy this from Microsoft Access?   If so check out Migrating your Access Queries to SQL Server Transact-SQL, which is a handy conversion article I wrote.

Things that are errors..
  • DISTINCTROW is not a valid T-SQL function in 2008.  DISTINCT is.
  • FORMAT$ is not a valid T-SQL function in 2008.  
  • DATEPART('m' remove the single quote marks

Things that are not errors and highly recommended
  • Since this query only has one table name you can lose the tablename. reference in all of the column names.
  • Using DISTINCT and GROUP BY serve the same purpose and is redundant.  Recommend losing the DISTINCT.
  • [dateFile By Month] -->  Lose the spaces in column names, which force the developer to use square brackets [ ] to surround it, which is one thing to potentially forget and throw an error.

With all of the above..
SELECT DISTINCT 
	amt1, 
	DATENAME(month, datefile) + ' ' + YEAR(dateFile as char(4)) as dateFile_by_month, 
	branch, 
	Sum(amt2) AS count_amt2, 
	Count(*) AS count_all
FROM ImportMonthlyChecksEntity

Open in new window

0
 

Author Comment

by:zimmer9
ID: 41764440
SELECT DISTINCT
      amt1,
      DATENAME(month, datefile) + ' ' + YEAR(dateFile as char(4)) as dateFile_by_month,
      branch,
      Sum(amt2) AS count_amt2,
      Count(*) AS count_all
FROM ImportMonthlyChecksEntity

-------------------------------------------------
Yes I did copy this from Microsoft Access and I will review your article St. Jimbo.  :)

Msg 156, Level 15, State 10, Line 3
Incorrect syntax near the keyword 'AS'.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 66

Expert Comment

by:Jim Horn
ID: 41764444
Typo, my bad, did not include the CAST( before the year.
SELECT DISTINCT 
      amt1, 
      DATENAME(month, datefile) + ' ' + CAST(YEAR(dateFile) as char(4)) as dateFile_by_month, 
      branch, 
      Sum(amt2) AS count_amt2, 
      Count(*) AS count_all
FROM ImportMonthlyChecksEntity

Open in new window

0
 

Author Comment

by:zimmer9
ID: 41764446
Last follow up question Jim.

Do you know the best way to resolve the following error from my table structure:

Msg 8117, Level 16, State 1, Line 5
Operand data type nvarchar(max) is invalid for sum operator.
0
 
LVL 66

Accepted Solution

by:
Jim Horn earned 2000 total points
ID: 41764452
nvarchar is a Unicode character.  Think all of those Swedish letters that are not in the English A-Z alphabet.

Sum means you are trying to add a column of numbers.

Obviously you can't sum a bunch of characters.   SUM ( A + Z + WAHOO) = ??

If we're talking the Sum(amt2), and it is nvarchar, the obvious knee-jerk reaction is that if these are numbers (amt would imply amount) where you would want to do math operations, then the column should be some kind of numeric data type and not character data type.  Recommend changing the column data type.

If you can't do that then you could always SUM(CAST(amt2 as decimal(9,2))), but I reeeeeeeeeeeeeeeeeeeeeeeally recommend against that as any value that cannot be converted to a number will throw an error.
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question