Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 varchar and datetime issue

Author  Topic 

NickC
Yak Posting Veteran

68 Posts

Posted - 2011-10-18 : 09:11:48
Hi

I'm trying to write a piece of code which will look at a varchar field and select the left 10 characters to produce a date and convert to date - that part is fine

however some dates have been misentered so I want something to exclude them and flag them up underneath as a seperate query?

is this possible

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-10-18 : 09:20:10
you could use isdate() to validate it

http://msdn.microsoft.com/en-us/library/ms187347.aspx


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

NickC
Yak Posting Veteran

68 Posts

Posted - 2011-10-18 : 09:26:33
I tried, but seems to only validate it after the conversation so is erroring before I ahve chance :(
Go to Top of Page

NickC
Yak Posting Veteran

68 Posts

Posted - 2011-10-18 : 09:31:18
Okay close to it, im doing isdate as jsut left(columnname,10) but its saying some arent dates cos its in dd/mm/yyyy format and its looking for mm/dd/yyyy instead?
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-10-18 : 09:33:06
quote:
Originally posted by NickC

I tried, but seems to only validate it after the conversation so is erroring before I ahve chance :(



case when isdate(left(col, 10)) = 1
then convert(datetime, left(col, 10))
else null
end



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

NickC
Yak Posting Veteran

68 Posts

Posted - 2011-10-18 : 09:36:24
Hero! I just got there

used
SET DATEFORMAT dmy;
GO

before writing it, and then used your bit! Thanks!!
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-10-18 : 09:39:57
yes. ISDATE() is depending on these settings. As stated in the link that i posted earlier
quote:

The return value of ISDATE depends on the settings set by SET DATEFORMAT, SET LANGUAGE and default language option




KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-10-21 : 03:41:35
Also use len() too. http://beyondrelational.com/blogs/madhivanan/archive/2007/09/24/handle-isdate-with-care.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -