Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Source of this error - error converting data type nvarchar to datetime

Posted on 2015-02-10
24
Medium Priority
?
87 Views
Last Modified: 2015-02-10
I keep getting this error when I execute this stored proc:

How to fix?

error converting nvarchar to datetime
0
Comment
Question by:Tom Knowlton
[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
  • 13
  • 5
  • 4
  • +1
24 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 1200 total points
ID: 40600954
1  Find this proc in your SSMS, right-click: Script Stored Procedure As, Alter to > New Query Editor Window
2  Remove the ALTER PROC line, re-Declare all of the variables, setting them to what you have in the image
3  Execute, verify what line throws the error by double-clicking on the error message.
4  Then ultimately you'll have to identify the values that throw the error by doing some kind of SELECT ... WHERE ISDATE(column name) = 0
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 40600967
I've done 1 thru 3.


It highlights the SELECT statement (line 9)


Here is what I currently have:

DECLARE
	@LocNums nvarchar(2000) = null,
	@DateBegin datetime = null,
	@DateEnd datetime = null,
	@SortBy nvarchar(50) = null



SELECT
	mainAppr.LastName
	,mainAppr.FirstName
	,mainAppr.UBCID UBCID	
	,lu.LocalNumber LocalNumber
	,lu.ID LocalUnionID
	,ap.Period
	,ap.PercentOfJourneyWages AdvancementPer	
	,aah.dayAdvancementDate AdvancementDate		
FROM UNAdmin.Apprentices mainAppr
	LEFT JOIN UNAdmin.ApprenticeAdvancementHistory aah ON aah.ID = mainAppr.CurrentAdvancementID   
	LEFT JOIN UNAdmin.ApprenticePeriods ap on aah.ApprenticePeriodID = ap.ID
	LEFT JOIN UNAdmin.LocalUnions lu ON lu.ID = mainAppr.LocalUnionID 
	LEFT JOIN UNAdmin.ApprenticeStatus apprStatus ON mainAppr.ApprenticeTypeID = apprStatus.ApprenticeTypeID   
WHERE  apprStatus.IsInProgram = 1 
AND (@LocNums is null OR lu.LocalNumber IN (select value from UNAdmin.unf_SplitToString(@LocNums, ',')))
AND (@DateBegin is null OR aah.dayAdvancementDate >= @DateBegin)
AND (@DateEnd is null OR aah.dayAdvancementDate <= @DateEnd)
ORDER BY 
	CASE 
		WHEN @SortBy = 'Local Number' THEN lu.LocalNumber
        WHEN @SortBy = 'Last Name' THEN mainAppr.LastName
		WHEN @SortBy = 'Advancement Date' THEN aah.dayAdvancementDate
		WHEN @SortBy is null THEN lu.LocalNumber
    END		

Open in new window

0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 40600969
Is this valid TSQL:

(@DateBegin is null OR aah.dayAdvancementDate >= @DateBegin)
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 66

Expert Comment

by:Jim Horn
ID: 40600971
>AND (@DateBegin is null OR aah.dayAdvancementDate >= @DateBegin)
>AND (@DateEnd is null OR aah.dayAdvancementDate <= @DateEnd)
Eyeball aah.dayAdvancementDate and tell us if it's nvarchar or date.
0
 
LVL 52

Expert Comment

by:Vitor Montalvão
ID: 40600975
If the default value is NULL for the dates then don't pass any value. You are passing '''''
Just remove the DateBegin and the DateEnd from the SP call. Or replace the '''' for NULL
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 40600977
it is a datetime, not null
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40600979
btw what's the purpose of @DateBegin=N'''''' in the function call?  
Looks like you're passing a unicode (nvarchar) sting with a couple of ''s in it, and looking at the SP @dateBegin and @dateEnd are datetime's, so that alone could throw the error.

Since the SP defines @dateEnd = NULL, then you should either not set the varible when making the SP call, or set it to NULL instead of N''''''
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 40600981
is a date time
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 40600992
btw what's the purpose of @DateBegin=N'''''' in the function call?  

It was me passing in '' as a datetime value (just messing around trying to figure out the error)

I try passing in all nulls and get this:

all nulls
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40600996
>It was me passing in '' as a datetime value
There's your problem.  '' is an empty string, and NOT a NULL.  
To pass a NULL, explicitly state NULL.
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 40601002
Look what happens when I don't even pass in date values:


begin and end date not passed in
0
 
LVL 52

Expert Comment

by:Vitor Montalvão
ID: 40601012
Can you try without that ORDER BY clause?
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 40601037
Can you try without that ORDER BY clause?

It runs then.  : P
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 40601053
But I still don't know what to fix.

I can't simply remove my order by clause.  I need it.
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 40601054
What changes do I need to make to be able to use my ORDER BY clause?

It does run ... but if I uncomment the order by clause .. it does not run gives the same error.

DECLARE
	@LocNums nvarchar(2000) = null,
	@DateBegin datetime = null,
	@DateEnd datetime = null,
	@SortBy nvarchar(50) = null



SELECT
	mainAppr.LastName
	,mainAppr.FirstName
	,mainAppr.UBCID UBCID	
	,lu.LocalNumber LocalNumber
	,lu.ID LocalUnionID
	,ap.Period
	,ap.PercentOfJourneyWages AdvancementPer	
	,aah.dayAdvancementDate AdvancementDate		
FROM UNAdmin.Apprentices mainAppr
	LEFT JOIN UNAdmin.ApprenticeAdvancementHistory aah ON aah.ID = mainAppr.CurrentAdvancementID   
	LEFT JOIN UNAdmin.ApprenticePeriods ap on aah.ApprenticePeriodID = ap.ID
	LEFT JOIN UNAdmin.LocalUnions lu ON lu.ID = mainAppr.LocalUnionID 
	LEFT JOIN UNAdmin.ApprenticeStatus apprStatus ON mainAppr.ApprenticeTypeID = apprStatus.ApprenticeTypeID   
WHERE  apprStatus.IsInProgram = 1 
AND (@LocNums is null OR lu.LocalNumber IN (select value from UNAdmin.unf_SplitToString(@LocNums, ',')))
AND (@DateBegin is null OR aah.dayAdvancementDate >= @DateBegin)
AND (@DateEnd is null OR aah.dayAdvancementDate <= @DateEnd)
--ORDER BY 
--	CASE 
--		WHEN @SortBy = 'Local Number' THEN lu.LocalNumber
--        WHEN @SortBy = 'Last Name' THEN mainAppr.LastName
--		WHEN @SortBy = 'Advancement Date' THEN aah.dayAdvancementDate
--		WHEN @SortBy is null THEN lu.LocalNumber
--    END		

Open in new window

0
 
LVL 52

Expert Comment

by:Vitor Montalvão
ID: 40601066
For my understanding the order by columns need to be all from the same datatype since you are using a CASE it expected to return a value from the same type. You can try to convert all non-char fields to varchar.
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 40601068
Narrowed it down to this line:

            --WHEN @SortBy = 'Advancement Date' THEN aah.dayAdvancementDate


If I uncomment that line it stops working.

DECLARE
	@LocNums nvarchar(2000) = null,
	@DateBegin datetime = null,
	@DateEnd datetime = null,
	@SortBy nvarchar(50) = null



SELECT
	mainAppr.LastName
	,mainAppr.FirstName
	,mainAppr.UBCID UBCID	
	,lu.LocalNumber LocalNumber
	,lu.ID LocalUnionID
	,ap.Period
	,ap.PercentOfJourneyWages AdvancementPer	
	,aah.dayAdvancementDate AdvancementDate		
FROM UNAdmin.Apprentices mainAppr
	LEFT JOIN UNAdmin.ApprenticeAdvancementHistory aah ON aah.ID = mainAppr.CurrentAdvancementID   
	LEFT JOIN UNAdmin.ApprenticePeriods ap on aah.ApprenticePeriodID = ap.ID
	LEFT JOIN UNAdmin.LocalUnions lu ON lu.ID = mainAppr.LocalUnionID 
	LEFT JOIN UNAdmin.ApprenticeStatus apprStatus ON mainAppr.ApprenticeTypeID = apprStatus.ApprenticeTypeID   
WHERE  apprStatus.IsInProgram = 1 
AND (@LocNums is null OR lu.LocalNumber IN (select value from UNAdmin.unf_SplitToString(@LocNums, ',')))
AND (@DateBegin is null OR aah.dayAdvancementDate >= @DateBegin)
AND (@DateEnd is null OR aah.dayAdvancementDate <= @DateEnd)
ORDER BY 
	CASE 
		WHEN @SortBy = 'Local Number' THEN lu.LocalNumber
        WHEN @SortBy = 'Last Name' THEN mainAppr.LastName
		--WHEN @SortBy = 'Advancement Date' THEN aah.dayAdvancementDate
		WHEN @SortBy is null THEN lu.LocalNumber
    END		

Open in new window

0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 40601072
You can try to convert all non-char fields to varchar.


You are talking to a TSQL noob here.

Can you show me what you mean?
0
 
LVL 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 400 total points
ID: 40601076
ORDER BY
      CASE
            WHEN @SortBy = 'Local Number' THEN lu.LocalNumber
            WHEN @SortBy = 'Last Name' THEN mainAppr.LastName
            WHEN @SortBy = 'Advancement Date' THEN CONVERT(varchar(8),  aah.dayAdvancementDate, 112)
            WHEN @SortBy is null THEN lu.LocalNumber
    END
0
 
LVL 52

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 400 total points
ID: 40601086
As ScottPletcher presented above or you can take that sort independently:
DECLARE
	@LocNums nvarchar(2000) = null,
	@DateBegin datetime = null,
	@DateEnd datetime = null,
	@SortBy nvarchar(50) = null

IF @SortBy = 'Advancement Date'  THEN
  SELECT
	mainAppr.LastName
	,mainAppr.FirstName
	,mainAppr.UBCID UBCID	
	,lu.LocalNumber LocalNumber
	,lu.ID LocalUnionID
	,ap.Period
	,ap.PercentOfJourneyWages AdvancementPer	
	,aah.dayAdvancementDate AdvancementDate		
FROM UNAdmin.Apprentices mainAppr
	LEFT JOIN UNAdmin.ApprenticeAdvancementHistory aah ON aah.ID = mainAppr.CurrentAdvancementID   
	LEFT JOIN UNAdmin.ApprenticePeriods ap on aah.ApprenticePeriodID = ap.ID
	LEFT JOIN UNAdmin.LocalUnions lu ON lu.ID = mainAppr.LocalUnionID 
	LEFT JOIN UNAdmin.ApprenticeStatus apprStatus ON mainAppr.ApprenticeTypeID = apprStatus.ApprenticeTypeID   
WHERE  apprStatus.IsInProgram = 1 
AND (@LocNums is null OR lu.LocalNumber IN (select value from UNAdmin.unf_SplitToString(@LocNums, ',')))
AND (@DateBegin is null OR aah.dayAdvancementDate >= @DateBegin)
AND (@DateEnd is null OR aah.dayAdvancementDate <= @DateEnd)
ORDER BY aah.dayAdvancementDate

ELSE
  SELECT
	mainAppr.LastName
	,mainAppr.FirstName
	,mainAppr.UBCID UBCID	
	,lu.LocalNumber LocalNumber
	,lu.ID LocalUnionID
	,ap.Period
	,ap.PercentOfJourneyWages AdvancementPer	
	,aah.dayAdvancementDate AdvancementDate		
FROM UNAdmin.Apprentices mainAppr
	LEFT JOIN UNAdmin.ApprenticeAdvancementHistory aah ON aah.ID = mainAppr.CurrentAdvancementID   
	LEFT JOIN UNAdmin.ApprenticePeriods ap on aah.ApprenticePeriodID = ap.ID
	LEFT JOIN UNAdmin.LocalUnions lu ON lu.ID = mainAppr.LocalUnionID 
	LEFT JOIN UNAdmin.ApprenticeStatus apprStatus ON mainAppr.ApprenticeTypeID = apprStatus.ApprenticeTypeID   
WHERE  apprStatus.IsInProgram = 1 
AND (@LocNums is null OR lu.LocalNumber IN (select value from UNAdmin.unf_SplitToString(@LocNums, ',')))
AND (@DateBegin is null OR aah.dayAdvancementDate >= @DateBegin)
AND (@DateEnd is null OR aah.dayAdvancementDate <= @DateEnd)
ORDER BY 
	CASE 
		WHEN @SortBy = 'Local Number' THEN lu.LocalNumber
        WHEN @SortBy = 'Last Name' THEN mainAppr.LastName
		WHEN @SortBy is null THEN lu.LocalNumber

    END		

Open in new window

0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 40601097
This seems to work (attempted in the interim between responses):


DECLARE
	@LocNums nvarchar(2000) = null,
	@DateBegin datetime = null,
	@DateEnd datetime = null,
	@SortBy nvarchar(50) = null

SELECT
	mainAppr.LastName
	,mainAppr.FirstName
	,mainAppr.UBCID UBCID	
	,lu.LocalNumber LocalNumber
	,lu.ID LocalUnionID
	,ap.Period
	,ap.PercentOfJourneyWages AdvancementPer	
	,aah.dayAdvancementDate AdvancementDate		
FROM UNAdmin.Apprentices mainAppr
	LEFT JOIN UNAdmin.ApprenticeAdvancementHistory aah ON aah.ID = mainAppr.CurrentAdvancementID   
	LEFT JOIN UNAdmin.ApprenticePeriods ap on aah.ApprenticePeriodID = ap.ID
	LEFT JOIN UNAdmin.LocalUnions lu ON lu.ID = mainAppr.LocalUnionID 
	LEFT JOIN UNAdmin.ApprenticeStatus apprStatus ON mainAppr.ApprenticeTypeID = apprStatus.ApprenticeTypeID   
WHERE  apprStatus.IsInProgram = 1 
AND (@LocNums is null OR lu.LocalNumber IN (select value from UNAdmin.unf_SplitToString(@LocNums, ',')))
AND (@DateBegin is null OR aah.dayAdvancementDate >= @DateBegin)
AND (@DateEnd is null OR aah.dayAdvancementDate <= @DateEnd)
ORDER BY 
	CASE 
		WHEN @SortBy = 'Local Number' THEN lu.LocalNumber
        WHEN @SortBy = 'Last Name' THEN mainAppr.LastName
		WHEN @SortBy = 'Advancement Date' THEN CAST(aah.dayAdvancementDate AS nvarchar)
		WHEN @SortBy is null THEN lu.LocalNumber
    END		

Open in new window

0
 
LVL 52

Expert Comment

by:Vitor Montalvão
ID: 40601102
Good.
You are talking to a TSQL noob here.
You aren't that noob after all ;)
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 40601108
You aren't that noob after all ;)

How dare you!  : P


Thanks everyone for your help!
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 40601146
>> WHEN @SortBy = 'Advancement Date' THEN CAST(aah.dayAdvancementDate AS nvarchar) <<

FYI, be careful when sorting dates in character format.  The default format is typically:
mmm dd yyyy timeAM|PM
which of course does not sort correctly.

I used YYYYMMDD, but that only handles days, of course.  If you need time as well, you'll need to change that to:
WHEN @SortBy = 'Advancement Date' THEN CONVERT(varchar(30),  aah.dayAdvancementDate, 121)




select CAST(getdate() as nvarchar)
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

609 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