Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Exclude all but date characters in SQL 2008 Expand / Collapse
Author
Message
Posted Monday, October 15, 2012 2:43 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

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
Post #1372943
Posted Monday, October 15, 2012 2:51 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

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
Post #1372947
Posted Monday, October 15, 2012 3:10 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

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.

Post #1372961
Posted Monday, October 15, 2012 3:25 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen 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




Post #1372966
Posted Monday, October 15, 2012 3:25 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

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
Post #1372967
Posted Monday, October 15, 2012 3:56 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

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.
Post #1372977
Posted Monday, October 15, 2012 6:36 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 7:24 PM
Points: 2,346, Visits: 3,192
helal.mobasher 13209 (10/15/2012)
I undrestand, but I am only the developer and don't have much control. And yes, all date are stored as MM/DD/YYYY.


Could there be more than one date per record? If yes, are you only interested in the first or do you want them all?



No loops! No CURSORs! No RBAR! Hoo-uh!

INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?

Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Are you too recursively challenged?
Splitting strings based on patterns can be fast!
Post #1373002
Posted Tuesday, October 16, 2012 8:50 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

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
Post #1373325
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse