?
Solved

Error with Datetime in Query

Posted on 2011-03-14
9
Medium Priority
?
560 Views
Last Modified: 2012-05-11
I have the following query.

select sys.netbios_name0,
  wi.ServiceTag0 [Service Tag],
  wi.provider0 [Provider],
  wi.shipdate0 [Ship Date],
  wi.Startdate0 [Warranty Start Date],
  wi.Enddate0 [Warranty End Date],
  GETDATE(),
 DateDiff("d",wi.Enddate0,GETDATE())[Warranty Days Remaining],
  wi.WarrantyExtension0 [Warranty Extension],
  wi.SystemType0 [System Type],
  wi.datescriptran0 [Timestamp data gathered],
  wi.dellIBU0,
  wi.Description0 as [Description]
from v_R_System as [SYS]
join v_gs_Warranty_Info0 as [WI] on sys.resourceid=wi.resourceid
order by sys.netbios_name0

The warranty end date is in the format MM/DD/YYYY except for a few fields where it could not collect the data, those fields show up as "--"
I'm sure I need a case statement in there where I bolded the line above to resolve the error, but I can't figure it out. Here's the error.

Msg 242, Level 16, State 3, Line 1
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

Thanks
0
Comment
Question by:NBPLSADMIN
[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
  • 5
  • 3
9 Comments
 
LVL 7

Expert Comment

by:mkobrin
ID: 35127879
select sys.netbios_name0,
  wi.ServiceTag0 [Service Tag],
  wi.provider0 [Provider],
  wi.shipdate0 [Ship Date],
  wi.Startdate0 [Warranty Start Date],
  wi.Enddate0 [Warranty End Date],
  GETDATE(),
case wi.Enddate0 when '--' then 0 else
 DateDiff("d",wi.Enddate0,GETDATE())[Warranty Days Remaining] end as date_diff,
  wi.WarrantyExtension0 [Warranty Extension],
  wi.SystemType0 [System Type],
  wi.datescriptran0 [Timestamp data gathered],
  wi.dellIBU0,
  wi.Description0 as [Description]
from v_R_System as [SYS]
join v_gs_Warranty_Info0 as [WI] on sys.resourceid=wi.resourceid
order by sys.netbios_name0

0
 

Author Comment

by:NBPLSADMIN
ID: 35127922
Now I'm getting this error:

Msg 102, Level 15, State 1, Line 9
Incorrect syntax near 'Warranty Days Remaining'.

FYI there is no column called [Warranty Days Remaining] , what I'm trying to do with this line

DateDiff("d",wi.Enddate0,GETDATE())[Warranty Days Remaining]

is to have the date returned to a column that will be called [Warranty Days Remaining] in the table being displayed. Maybe I have this format wrong as well. I was following the format of the other fields like this one.

wi.Startdate0 [Warranty Start Date]

0
 
LVL 7

Expert Comment

by:mkobrin
ID: 35127995
Sorry, I meant to take that out before posting.

Try this

select sys.netbios_name0,
  wi.ServiceTag0 [Service Tag],
  wi.provider0 [Provider],
  wi.shipdate0 [Ship Date],
  wi.Startdate0 [Warranty Start Date],
  wi.Enddate0 [Warranty End Date],
  GETDATE(),
case wi.Enddate0 when '--' then 0 else
 DateDiff("d",wi.Enddate0,GETDATE()) end as date_diff,
  wi.WarrantyExtension0 [Warranty Extension],
  wi.SystemType0 [System Type],
  wi.datescriptran0 [Timestamp data gathered],
  wi.dellIBU0,
  wi.Description0 as [Description]
from v_R_System as [SYS]
join v_gs_Warranty_Info0 as [WI] on sys.resourceid=wi.resourceid
order by sys.netbios_name0

0
Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 35128039
would this work:

case wi.Enddate0 when '--' then 0 else
 DateDiff("d",wi.Enddate0,GETDATE())
end as [Warranty Days Remaining]

AW
0
 
LVL 7

Accepted Solution

by:
mkobrin earned 500 total points
ID: 35128080
Sorry again, I never read your comment properly. This will work for you:


select sys.netbios_name0,
  wi.ServiceTag0 [Service Tag],
  wi.provider0 [Provider],
  wi.shipdate0 [Ship Date],
  wi.Startdate0 [Warranty Start Date],
  wi.Enddate0 [Warranty End Date],
  GETDATE(),
case wi.Enddate0 when '--' then 0 else
 DateDiff("d",wi.Enddate0,GETDATE()) end as [Warranty Days Remaining] ,
  wi.WarrantyExtension0 [Warranty Extension],
  wi.SystemType0 [System Type],
  wi.datescriptran0 [Timestamp data gathered],
  wi.dellIBU0,
  wi.Description0 as [Description]
from v_R_System as [SYS]
join v_gs_Warranty_Info0 as [WI] on sys.resourceid=wi.resourceid
order by sys.netbios_name0

0
 

Author Comment

by:NBPLSADMIN
ID: 35128849
Thanks that worked perfectly.

For the Warranty Days Remaining, I want it to show 0 if the value is less than 0. Obviously, if the warranty is expired, I have 0 days remaining, not -56 for example. I know this is on top of the question asked, but you seem on a roll.

Following your format I tried
case [Warranty Days Remaining] when <0 then 0 else [Warranty Days Remaining] end,
But then I get:
Msg 102, Level 15, State 1, Line 9
Incorrect syntax near '<'.


So with some more looking up on the case syntax I tried:
case when [Warranty Days Remaining]<0 then 0 else [Warranty Days Remaining] end,
which produces:
Msg 207, Level 16, State 1, Line 9
Invalid column name 'Warranty Days Remaining'.
Msg 207, Level 16, State 1, Line 9
Invalid column name 'Warranty Days Remaining'.


I thought I was pretty good with SQL but I guess I still have lots to learn.



0
 
LVL 7

Expert Comment

by:mkobrin
ID: 35129406
I'm not sure if this will work, but you can try it:

select sys.netbios_name0,
  wi.ServiceTag0 [Service Tag],
  wi.provider0 [Provider],
  wi.shipdate0 [Ship Date],
  wi.Startdate0 [Warranty Start Date],
  wi.Enddate0 [Warranty End Date],
  GETDATE(),
case wi.Enddate0 when '--' then 0 else
 case DateDiff("d",wi.Enddate0,GETDATE()) when < 0 then 0 else  DateDiff("d",wi.Enddate0,GETDATE())  end end as [Warranty Days Remaining] ,
  wi.WarrantyExtension0 [Warranty Extension],
  wi.SystemType0 [System Type],
  wi.datescriptran0 [Timestamp data gathered],
  wi.dellIBU0,
  wi.Description0 as [Description]
from v_R_System as [SYS]
join v_gs_Warranty_Info0 as [WI] on sys.resourceid=wi.resourceid
order by sys.netbios_name0

0
 

Author Comment

by:NBPLSADMIN
ID: 35129463
You just about had it.

Had to move the "when" statement

case DateDiff("d",wi.Enddate0,GETDATE()) when < 0 then 0 else  DateDiff("d",wi.Enddate0,GETDATE())  end end as [Warranty Days Remaining] ,

to

case when DateDiff("d",GETDATE(),wi.Enddate0)  < 0 then 0 else DateDiff("d",GETDATE(),wi.Enddate0)  end end as [Warranty Days Remaining] ,

Thanks again for your help.
0
 
LVL 7

Expert Comment

by:mkobrin
ID: 35129513
No problem, all the best with your learning of SQL.
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

752 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