Solved

SQL syntax to delelte data is not a date format

Posted on 2016-09-04
7
52 Views
Last Modified: 2016-09-05
I am importing data from a third party software
One of the fields they have is an nvarchar(200) but holds dates in format :  mm/dd/yyyy

Most of the entries in that field are in fact dates, but some aren't. I need to delete any entries in that field which are NOT a 'date' or in date format 'mm/dd/yyyy'

The table name is:  Dependents
The field name is:  DepLastUSEntry

Also some dates are in :  07/27/03

Can they be converted to :  07/27/2003  ?
And also:  8/25/2003  into 08/25/2003  
Maybe:  8/13.2002  into 08/13/2002

Anything different that cannot be converted into a mm/dd/yyyy should be deleted, and if the conversions above cannot be done then remove any date that is not mm/dd/yyyy




Thanks!
0
Comment
Question by:amucinobluedot
  • 3
  • 3
7 Comments
 
LVL 29

Expert Comment

by:Olaf Doschke
Comment Utility
Which SQL Server version are you using (SELECT @@Version)?

Starting wioth 2012 you may use:

SET LANGUAGE ENGLISH -- importatnt to process dates in format mm/dd/yyyy, might be optional if your server setting is in english/british/us format anyway.
DELETE FROM yourtable WHERE TRY_CONVERT(date, DepLastUSEntry) IS NULL

Open in new window


Just try some dates (especially with day>12) on try_convert to see whether it results in a date for correct nvarchar strings, so you don't delete everything.

Bye, Olaf.
0
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
>I need to delete any entries in that field which are NOT a 'date' or in date format 'mm/dd/yyyy'
DELETE means remove the entire row from the table.
UPDATE means change values in the row, say changing this column to an empty string or NULL '', and keep the row.

Tell us which you wish to do here.
0
 

Author Comment

by:amucinobluedot
Comment Utility
Sorry. I meant update and make it null
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 29

Expert Comment

by:Olaf Doschke
Comment Utility
Well, then you would UPDATE instead of delete, but you can't update an nvarchar column with a date, you can only set those NULL, which aren't a date in string form.

SET LANGUAGE ENGLISH -- important to process dates in format mm/dd/yyyy, might be optional if your server setting is in english/british/us format anyway.
UPDATE yourtable SET DepLastUSEntry = NULL 
WHERE TRY_CONVERT(date, DepLastUSEntry) IS NULL

Open in new window


Again, test before you do, with the wrong settings (eg TRY_CONVERT converting dd/mm/yyyy instead of mm/dd/yyyy, international formats differ a lot also in regard of the dash separator) you may lose part of the dates you rather wanted to keep.

If your overall goal is to alter the table to have a date or datetime column:

Set Language English;
Create Table #Test (DepLastUSEntry nvarchar(200));
Insert Into #Test Values (N'12/31/2010'), (N'31/12/2015');
Select * From #Test;

Update #Test set DepLastUSEntry = NULL WHERE TRY_CONVERT(date,DepLastUSEntry) IS NULL;

Alter Table #Test Alter Column DepLastUSEntry Date NULL;
Select * From #Test;
Drop Table #Test;

Open in new window


You would execute lines 1, 6, and 8 only, obviously.

Bye, Olaf.
0
 

Author Comment

by:amucinobluedot
Comment Utility
I tried this, but I am using SQL 2008R2

SET LANGUAGE ENGLISH -- important to process dates in format mm/dd/yyyy, might be optional if your server setting is in english/british/us format anyway.
UPDATE yourtable SET DepLastUSEntry = NULL 
WHERE TRY_CONVERT(date, DepLastUSEntry) IS NULL

Open in new window


Error

Msg 195, Level 15, State 10, Line 5
'TRY_CONVERT' is not a recognized built-in function name.

Open in new window

0
 
LVL 29

Accepted Solution

by:
Olaf Doschke earned 500 total points
Comment Utility
That's why I asked for the version. Then try it with ISDATE():

First see, if these dates are correct:
SELECT DepLastUSEntry FROM yourtable WHERE ISDATE(DepLastUSEntry)=1

Open in new window


If so, this update will change all other invalid ones to NULL:
UPDATE yourtable SET DepLastUSEntry = NULL 
WHERE ISDATE(DepLastUSEntry)=0

Open in new window

Bye, Olaf.
0
 

Author Closing Comment

by:amucinobluedot
Comment Utility
This worked!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

743 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

15 Experts available now in Live!

Get 1:1 Help Now