Exclude all but date characters in SQL 2008

  • 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

  • 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


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • 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.

  • ;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



    select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);

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

  • 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


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • I undrestand, but I am only the developer and don't have much control. And yes, all date are stored as MM/DD/YYYY.

  • 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![/I]

    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?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • I have yet to try what MM posted. But I foudn this solution and it's working:

    ISDATE(A) = 1and len(A)=10

    Anyway, thank you for sendin gme the right direction.

    Helal

  • Viewing 8 posts - 1 through 7 (of 7 total)

    You must be logged in to reply to this topic. Login to reply