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
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Today @ 10:47 AM
Points: 29, Visits: 124
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 @ 1:24 PM
Points: 12,906, Visits: 31,984
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
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Today @ 10:47 AM
Points: 29, Visits: 124
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


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: 2 days ago @ 5:29 PM
Points: 1,787, Visits: 5,693

;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


  • MMGrid Addin
  • MMNose Addin


  • Forum Etiquette: How to post Reporting Services problems
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • How to Post Performance Problems - by Gail Shaw

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


    SSChampion

    SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

    Group: General Forum Members
    Last Login: Today @ 1:24 PM
    Points: 12,906, Visits: 31,984
    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
    SSC Rookie

    SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

    Group: General Forum Members
    Last Login: Today @ 10:47 AM
    Points: 29, Visits: 124
    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


    Hall of Fame

    Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

    Group: General Forum Members
    Last Login: 2 days ago @ 4:14 AM
    Points: 3,618, Visits: 5,254
    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?



    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    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?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
    Since random numbers are too important to be left to chance, let's generate some!
    Learn to understand recursive CTEs by example.
    Splitting strings based on patterns can be fast!
    Post #1373002
    Posted Tuesday, October 16, 2012 8:50 AM
    SSC Rookie

    SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

    Group: General Forum Members
    Last Login: Today @ 10:47 AM
    Points: 29, Visits: 124
    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