SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Date in varchar10 in YYYY-MM-DD format want to del
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

cplusplus
Aged Yak Warrior

534 Posts

Posted - 03/01/2013 :  14:37:08  Show Profile  Reply with Quote
I want to delete all feb 2013 months data, Date value is in varchar(10) in YYYY-MM-DD format.

Delete from tbl_PatientVisits where post_date between convert('2013-02-01', 'yyyy-mm-dd') And convert('2013-02-28', 'yyyy-mm-dd')

Thank you very much for teh helpful info.

James K
Flowing Fount of Yak Knowledge

3562 Posts

Posted - 03/01/2013 :  14:58:54  Show Profile  Reply with Quote
Do it like shown below:
DELETE 
FROM   tbl_PatientVisits
WHERE  post_date BETWEEN CONVERT(DATETIME, '2013-02-01', 120) 
       AND CONVERT(DATETIME, '2013-02-28', 120) 
Even better, in case someone provides the date that includes a time portion (and if the variable/column had enough width to support it), would be to have it like this:
DELETE 
FROM   tbl_PatientVisits
WHERE  post_date >= CONVERT(DATETIME, '2013-02-01', 120) 
       AND post_date < DATEADD(dd,1,CONVERT(DATETIME, '2013-02-28', 120) )

Edited by - James K on 03/01/2013 14:59:18
Go to Top of Page

cplusplus
Aged Yak Warrior

534 Posts

Posted - 03/01/2013 :  15:14:30  Show Profile  Reply with Quote
Thank you james......
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3562 Posts

Posted - 03/01/2013 :  16:37:58  Show Profile  Reply with Quote
You are very welcome - glad to help.
Go to Top of Page

ScottPletcher
Constraint Violating Yak Guru

USA
341 Posts

Posted - 03/01/2013 :  18:04:54  Show Profile  Reply with Quote
Even better, don't convert to a date and force SQL to also convert the column table, which prevents any existing indexes on post_date from being used in a SEEK. And use < rather than <= to avoid any potential issues with time in the future:

Delete from tbl_PatientVisits 
where post_date >= '2013-02-01' and post_date < '2013-03-01'

Edited by - ScottPletcher on 03/01/2013 18:09:22
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 03/02/2013 :  05:22:34  Show Profile  Reply with Quote
see

http://visakhm.blogspot.in/2012/12/different-ways-to-implement-date-range.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.14 seconds. Powered By: Snitz Forums 2000