Solved

SQL syntax to delelte data is not a date format

Posted on 2016-09-04
7
61 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
ID: 41783867
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
ID: 41783891
>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
ID: 41783895
Sorry. I meant update and make it null
0
[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 41783898
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
ID: 41784584
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
ID: 41784730
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
ID: 41784953
This worked!
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

895 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

12 Experts available now in Live!

Get 1:1 Help Now