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)
 Date in varchar10 in YYYY-MM-DD format want to del

Author  Topic 

cplusplus
Aged Yak Warrior

567 Posts

Posted - 2013-03-01 : 14:37:08
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
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-03-01 : 14:58:54
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) )
Go to Top of Page

cplusplus
Aged Yak Warrior

567 Posts

Posted - 2013-03-01 : 15:14:30
Thank you james......
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-03-01 : 16:37:58
You are very welcome - glad to help.
Go to Top of Page

ScottPletcher
Aged Yak Warrior

550 Posts

Posted - 2013-03-01 : 18:04:54
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'
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-03-02 : 05:22:34
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
   

- Advertisement -