|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Sunday, March 24, 2013 2:05 PM
Points: 23,
Visits: 99
|
|
I have a data clumn (A) stored as Text and contains all sorts of codes and also includes date stored MM/DD/YYYY. I need to select only date characters and exclude all others. Then I need to put this date in my where clause where A=Getdate() - one week
Thank you,
Helal
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 4:31 AM
Points: 11,648,
Visits: 27,762
|
|
where ISDATE(A) = 1 AND CONVERT(datetime,A) > DATEADD(dd,-7,Getdate()) --7 days? 8 days? what's minus one week for you? --AND CONVERT(datetime,A) > DATEADD(ww,-1,Getdate()) --1 week?
Lowell
--There is no spoon, and there's no default ORDER BY in sql server either. Actually, Common Sense is so rare, it should be considered a Superpower. --my son
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Sunday, March 24, 2013 2:05 PM
Points: 23,
Visits: 99
|
|
Thx Lowell, I received this error after applying the codes "Conversion failed when converting date and/or time from character string" after looking at the data in A, all codes are excluded excpet date characters and somer init ones such as 2088, 3011, etc...I think this init's stored as text causing the above error.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 5:50 PM
Points: 1,308,
Visits: 3,899
|
|
;with data as ( select 'klsjadlkj ads891 3y1kjnd al k 22/31/2012 lk;adk asl;k das#' as things union all select 'k 11/1/2012 dk asl;k das#' as things union all select 'al k 10/07/2012' as things union all select '12/31/2012' as things union all select 'klsjadlkj sadas ads891 3y1kjnd al k 12/31/2012 lk;adk asl;k das#' as things ), dates as ( select things,SUBSTRING(things,patindex('%[01][0-9]/[0-3][0-9]/[0-9][0-9][0-9][0-9]%',things),10) as found_date from data where things like '%[01][0-9]/[0-3][0-9]/[0-9][0-9][0-9][0-9]%' ) select things,found_date from dates where found_date = CONVERT(CHAR(10),dateadd(day,-8,getdate()),101)
MM
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 4:31 AM
Points: 11,648,
Visits: 27,762
|
|
it's a little tough with denormalized data; ideally you'll want to change the schema if you can to store dates in date columns, and notes in a seperate column; but we cannot always do that, i know.
do you know whether, if the field contains a date, they ALL follow a standard pattern like mm/dd/yyyy?
Lowell
--There is no spoon, and there's no default ORDER BY in sql server either. Actually, Common Sense is so rare, it should be considered a Superpower. --my son
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Sunday, March 24, 2013 2:05 PM
Points: 23,
Visits: 99
|
|
| I undrestand, but I am only the developer and don't have much control. And yes, all date are stored as MM/DD/YYYY.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 7:24 PM
Points: 2,346,
Visits: 3,192
|
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Sunday, March 24, 2013 2:05 PM
Points: 23,
Visits: 99
|
|
I have yet to try what MM posted. But I foudn this solution and it's working: ISDATE(A) = 1 and len(A)=10
Anyway, thank you for sendin gme the right direction.
Helal
|
|
|
|