| Author |
Topic |
|
kamii47
Constraint Violating Yak Guru
353 Posts |
Posted - 2010-08-27 : 06:39:56
|
| I have varachar(50) field which have date in yyyymmdd formatNow i have to filter the data which are greater then server dateI have following querySelect * from MDB_CONTENT_INFOWhere convert(datetime, CON_SCHEDULED_INDUCTION_DATE,112) >= GetDate() Problem ist aht i may have some invalid data other then valid one.I am getting errorThe conversion of a varchar data type to a datetime data type resulted in an out-of-range value.orConversion failed when converting date and/or time from character string.I wanted to exclude those records but currently getting error for the queryas |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2010-08-27 : 06:50:59
|
| look for ISDATE function on bol |
 |
|
|
kamii47
Constraint Violating Yak Guru
353 Posts |
Posted - 2010-08-27 : 07:53:47
|
| Select * from MDB_CONTENT_INFOWhere ISDATE(convert(datetime, 'fdhgjdfgh',112)) = 1and convert(datetime, 'fdhgjdfgh',112) >= GetDate() but not workingKamran ShahidSr. Software Engineer(MCSD.Net,MCPD.net) |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2010-08-27 : 08:00:59
|
| [code]where ISDATE('fdhgjdfgh') = 1[/code] |
 |
|
|
kamii47
Constraint Violating Yak Guru
353 Posts |
Posted - 2010-08-27 : 12:57:09
|
| After changing the query to Select * from MDB_CONTENT_INFOWhere ISDATE(CON_SCHEDULED_INDUCTION_DATE) = 1and convert(datetime, CON_SCHEDULED_INDUCTION_DATE,112) >= GetDate()I have still same resultKamran ShahidSr. Software Engineer(MCSD.Net,MCPD.net) |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2010-08-27 : 13:18:06
|
| You can't do that in that way. There is no way to know which order SQL will evaluate the predicates.I'm trying to remember how to do this in a reasonable fashion. I'd suggest getting your data fixed up before you load it into your database. But, try selecting the data into a temp table WHERE ISDATE() = 1. Then from there you should be able to filter by GETDATE().So others might have some advice on how to do this better/esier/more effeciently. |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2010-08-27 : 13:25:22
|
Maybe this will work? WHERE convert ( datetime, CASE WHEN ISDATE(CON_SCHEDULED_INDUCTION_DATE) = 1 THEN CON_SCHEDULED_INDUCTION_DATE ELSE NULL END, 112 ) >= GetDate() |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-08-31 : 07:45:31
|
| orSelect * from MDB_CONTENT_INFOWhere ISDATE(CON_SCHEDULED_INDUCTION_DATE) = 1and len(CON_SCHEDULED_INDUCTION_DATE)=8and convert(datetime, CON_SCHEDULED_INDUCTION_DATE,112) >= GetDate()MadhivananFailing to plan is Planning to fail |
 |
|
|
sam4all
Starting Member
6 Posts |
Posted - 2010-08-31 : 15:17:09
|
kamii47,Madhivanan,LampreyNone of the solution will workBeen there done this and it does not work....As of now the only solution is to use temp table or update records with wrong dates to something elsekamii47I have been having the same probelm and no owe has given me a solution yetyou can follow this in the thread belowhttp://www.sqlteam.com/forums/topic.asp?TOPIC_ID=149310quote: Originally posted by madhivanan orSelect * from MDB_CONTENT_INFOWhere ISDATE(CON_SCHEDULED_INDUCTION_DATE) = 1and len(CON_SCHEDULED_INDUCTION_DATE)=8and convert(datetime, CON_SCHEDULED_INDUCTION_DATE,112) >= GetDate()MadhivananFailing to plan is Planning to fail
|
 |
|
|
atrofman
Starting Member
1 Post |
Posted - 2011-06-24 : 22:07:55
|
| Here is what I did. 1. I have created a scalar function (for the particular Data Base where I want to use convert(datetime,...) in conjunction with isdate():_____________________________________________________________________________________IF OBJECT_ID (N'dbo.convertDT', N'FN') IS NOT NULL DROP FUNCTION dbo.convertDT;GOCREATE FUNCTION dbo.convertDT (@DATE varchar(20))RETURNS datetimeWITH EXECUTE AS CALLERASBEGIN DECLARE @Ddate datetime; SET @Ddate=(Case when isdate(@DATE)=1 then CONVERT(datetime,@DATE) else null end) RETURN(@Ddate);END;GO_____________________________________________________________________________________________2. Then, instead of using convert(datetime, YourString), I used dbo.convertDT(YourString)You can also use it in the code with select statements.Ark Trofman |
 |
|
|
|