There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Software Development
Tag Cloud
audio blue screen boot bsod computer cpu crash dell desktop driver drivers error excel external hard drive firefox format freezes freezing hard drive hardware hijackthis install internet internet explorer itunes keyboard laptop malware motherboard mouse network networking outlook 2007 power printer problem ram router screen slow sound trojan usb virus vista vista 32-bit windows windows xp winxp wireless
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
T-SQL Question


Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
billbos's Avatar
Junior Member with 6 posts.
 
Join Date: Dec 2004
Experience: Intermediate
21-Jan-2005, 05:56 PM #1
T-SQL Question
I'm converting 2 date fields that are stored as VARCHAR for comparison, but I receive this error when running error: 'Syntax error converting datetime from character string', because there are 'N/A' values stored in the field (even though I have this in the WHERE clause to ignore 'N/A'). Anyone have an idea of how to get around this? Here's my code:


SELECT TOP 10 *
FROM WorkDB..Sales
WHERE CAST(Billing_End_Date AS DATETIME) > CAST(Coverage_End_Date AS DATETIME)
AND CAST(Billing_End_Date AS DATETIME) < (SELECT GETDATE())
AND Billing_End_Date <> 'N/A'
andy_p's Avatar
Junior Member with 3 posts.
 
Join Date: Jan 2005
Experience: Einstein
22-Jan-2005, 02:44 PM #2
you can't cast Billing_End_Date into a DATETIME before you've checked to see if it's 'N/A'. Try this:

SELECT TOP 10 * FROM WorkDB..Sales
WHERE CASE Billing_End_Date <> 'N/A'
WHEN true THEN CAST(Billing_End_Date AS DATETIME) > CAST(Coverage_End_Date AS DATETIME) AND CAST(Billing_End_Date AS DATETIME) < (SELECT GETDATE())
ELSE false
END
andy_p's Avatar
Junior Member with 3 posts.
 
Join Date: Jan 2005
Experience: Einstein
22-Jan-2005, 02:51 PM #3
or better, this:
WHERE CASE
WHEN Billing_End_Date <> 'N/A' THEN CAST(Billing_End_Date AS DATETIME) > CAST(Coverage_End_Date AS DATETIME) AND ....

look at http://msdn.microsoft.com/library/de...ca-co_5t9v.asp
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 11:06 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.