Solved

Error with Datetime in Query

Posted on 2011-03-14
9
554 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
  • 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 7

Accepted Solution

by:
mkobrin earned 125 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction: Often, when running a query with joins, the results show up "duplicates", and often, those duplicates can be "eliminated" in the results using DISTINCT, for example. Using DISTINCT is simple: just add it after the SELECT keyword, an…
SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now