Click here to Skip to main content
15,914,162 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In following code I am getting Error in SQL Server 2012
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near 'NULL'.
SQL
SELECT TBLTRANSUSERS.FirstName + ' ' + TBLTRANSUSERS.LastName + '(' + TBLTRANSUSERS.UserCode + ')' as 'User Name',TBLTRANSJOBS.JobName as 'Job Name', TBLTRANSDOCUMENTS.DocName as 'Loan Name',Convert(Varchar(25),AssignedDate,131) as 'Assigned Date',
(SELECT RIGHT('0' + CAST(Datediff(second,AssignedDate,GetDate()) / 3600 AS VARCHAR),2) + ':' + RIGHT('0' + CAST((Datediff(second,AssignedDate,GetDate()) / 60) % 60 AS VARCHAR),2) + ':' + RIGHT('0' + CAST(Datediff(second,AssignedDate,GetDate()) % 60 AS VARCHAR),2) as 'Elapsed Time'
FROM ((TBLTRANSDOCUMENTS inner join TBLTRANSJOBS ON TBLTRANSJOBS.JobID = TBLTRANSDOCUMENTS.JobID) inner join TBLTRANSUSERS on TBLTRANSDOCUMENTS.ExtractionAssigned=TBLTRANSUSERS.UserID) WHERE KeyedDate is NULL and 
ExtractionAssigned is not NULL


What I have tried:

No Solutions are coming in the above problem.
Posted
Updated 25-Oct-18 1:47am
v2

We can't solve that for you - we don't have any access to your data and that's probably going to be very relevant to the problem.

So start by cutting it down - rip it to the minimum, and see if it works and provides the right data.
Perhaps just this:
SQL
SELECT FirstName, LastName, UserCode, TBLTRANSJOBS.JobName, TBLTRANSDOCUMENTS.DocName, AssignedDate
FROM TBLTRANSDOCUMENTS 
inner join TBLTRANSJOBS ON TBLTRANSJOBS.JobID = TBLTRANSDOCUMENTS.JobID
Then slowly add bits back until the problem reoccurs.
When it does, look at what you added and see how that would affect the data.

But we can't do any of that for you!
 
Share this answer
 
Formatting your code to relevant block is a good idea - it helps to find unclosed/wrongly nested code blocks...
In your case there is a missing ) (closing bracket) at the end, after IS NOT NULL...
 
Share this answer
 
Further to the valid comments in solutions 1 and 2 here is some more advice...

1. Leave the formatting of output to the UI layer and not the data layer ... so instead of
SQL
TBLTRANSUSERS.FirstName + ' ' + TBLTRANSUSERS.LastName + '(' + TBLTRANSUSERS.UserCode + ')' as 'User Name'
just return all the parts required and let the business or UI layer format it accordingly. I.e.
SQL
TBLTRANSUSERS.FirstName, TBLTRANSUSERS.LastName, TBLTRANSUSERS.UserCode
The immediate benefit of this is that your code can be reused for cases where you don't want to concatenate the fields. It will also run more efficiently.

2. Especially do not convert dates to strings in your SQL. Very inefficient and it also means the calling code has to do a data conversion back to date.
SQL
Convert(Varchar(25),AssignedDate,131) as 'Assigned Date',
should just be
SQL
AssignedDate as 'Assigned Date',
3. Try to get out of the habit of using spaces in field names ('Assigned Date', 'User Name'). It's a real PITA having to use square brackets to refer the column name and can lead to hard-to-find errors being introduced into your code.

4. Be consistent! You have used Table Names to unambiguously identify many of your columns except for AssignedDate ... which table is that coming from? The same is true of the columns in the WHERE clause. You might also want to consider using short ALIAS names for tables - it will save some typing and can also make things a little clearer

5. This
SQL
(
	SELECT RIGHT('0' + CAST(Datediff(second,AssignedDate,GetDate()) / 3600 AS VARCHAR),2) + ':' + 
		RIGHT('0' + CAST((Datediff(second,AssignedDate,GetDate()) / 60) % 60 AS VARCHAR),2) + ':' + 
		RIGHT('0' + CAST(Datediff(second,AssignedDate,GetDate()) % 60 AS VARCHAR),2
) as 'Elapsed Time'
is truly awful but in any event belongs in the UI layer. At most return the time difference in seconds if you really must.

Try applying these changes and compare the return from the SQL Profiler (Run SQL Server Profiler | Microsoft Docs[^] plus there are articles here on CodeProject)
 
Share this answer
 
SQL
SELECT TBLTRANSUSERS.FirstName + ' ' + TBLTRANSUSERS.LastName + '(' + TBLTRANSUSERS.UserCode + ')' as 'User Name',TBLTRANSJOBS.JobName as 'Job Name', TBLTRANSDOCUMENTS.DocName as 'Loan Name',AssignedDate as 'Assigned Date',
                                            (SELECT RIGHT('00' + CAST(Datediff(second,D.AssignedDate,GetDate()) / 3600 AS VARCHAR),3) + ':' + RIGHT('0' + CAST((Datediff(second,D.AssignedDate,GetDate()) / 60) % 60 AS VARCHAR),2) + ':' + RIGHT('0' + CAST(Datediff(second,D.AssignedDate,GetDate()) % 60 AS VARCHAR),2) FROM TBLTRANSDOCUMENTS D Where D.DocID=TBLTRANSDOCUMENTS.DocID) as 'Elapsed Time'
                                            FROM ((TBLTRANSDOCUMENTS  inner join TBLTRANSJOBS ON TBLTRANSJOBS.JobID = TBLTRANSDOCUMENTS.JobID) inner join TBLTRANSUSERS on TBLTRANSDOCUMENTS.ExtractionAssigned=TBLTRANSUSERS.UserID) WHERE KeyedDate is NULL and 
                                            ExtractionAssigned is not NULL order by TBLTRANSUSERS.Firstname
 
Share this answer
 
Comments
CHill60 25-Oct-18 8:59am    
No need for the embedded sub-queries

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900