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

x
?
Solved

Error with Datetime in Query

Posted on 2011-03-14
9
Medium Priority
?
561 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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

618 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