SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Exclude all but date characters in SQL 2008


Exclude all but date characters in SQL 2008

Author
Message
helal.mobasher 13209
helal.mobasher 13209
SSC Veteran
SSC Veteran (292 reputation)SSC Veteran (292 reputation)SSC Veteran (292 reputation)SSC Veteran (292 reputation)SSC Veteran (292 reputation)SSC Veteran (292 reputation)SSC Veteran (292 reputation)SSC Veteran (292 reputation)

Group: General Forum Members
Points: 292 Visits: 219
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
Lowell
Lowell
SSC Guru
SSC Guru (74K reputation)SSC Guru (74K reputation)SSC Guru (74K reputation)SSC Guru (74K reputation)SSC Guru (74K reputation)SSC Guru (74K reputation)SSC Guru (74K reputation)SSC Guru (74K reputation)

Group: General Forum Members
Points: 74399 Visits: 40977
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!
helal.mobasher 13209
helal.mobasher 13209
SSC Veteran
SSC Veteran (292 reputation)SSC Veteran (292 reputation)SSC Veteran (292 reputation)SSC Veteran (292 reputation)SSC Veteran (292 reputation)SSC Veteran (292 reputation)SSC Veteran (292 reputation)SSC Veteran (292 reputation)

Group: General Forum Members
Points: 292 Visits: 219
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.
mister.magoo
mister.magoo
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10897 Visits: 7891


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

  • Lowell
    Lowell
    SSC Guru
    SSC Guru (74K reputation)SSC Guru (74K reputation)SSC Guru (74K reputation)SSC Guru (74K reputation)SSC Guru (74K reputation)SSC Guru (74K reputation)SSC Guru (74K reputation)SSC Guru (74K reputation)

    Group: General Forum Members
    Points: 74399 Visits: 40977
    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!
    helal.mobasher 13209
    helal.mobasher 13209
    SSC Veteran
    SSC Veteran (292 reputation)SSC Veteran (292 reputation)SSC Veteran (292 reputation)SSC Veteran (292 reputation)SSC Veteran (292 reputation)SSC Veteran (292 reputation)SSC Veteran (292 reputation)SSC Veteran (292 reputation)

    Group: General Forum Members
    Points: 292 Visits: 219
    I undrestand, but I am only the developer and don't have much control. And yes, all date are stored as MM/DD/YYYY.
    dwain.c
    dwain.c
    SSCoach
    SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

    Group: General Forum Members
    Points: 18329 Visits: 6431
    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!
    My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
    helal.mobasher 13209
    helal.mobasher 13209
    SSC Veteran
    SSC Veteran (292 reputation)SSC Veteran (292 reputation)SSC Veteran (292 reputation)SSC Veteran (292 reputation)SSC Veteran (292 reputation)SSC Veteran (292 reputation)SSC Veteran (292 reputation)SSC Veteran (292 reputation)

    Group: General Forum Members
    Points: 292 Visits: 219
    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
    Go


    Permissions

    You can't post new topics.
    You can't post topic replies.
    You can't post new polls.
    You can't post replies to polls.
    You can't edit your own topics.
    You can't delete your own topics.
    You can't edit other topics.
    You can't delete other topics.
    You can't edit your own posts.
    You can't edit other posts.
    You can't delete your own posts.
    You can't delete other posts.
    You can't post events.
    You can't edit your own events.
    You can't edit other events.
    You can't delete your own events.
    You can't delete other events.
    You can't send private messages.
    You can't send emails.
    You can read topics.
    You can't vote in polls.
    You can't upload attachments.
    You can download attachments.
    You can't post HTML code.
    You can't edit HTML code.
    You can't post IFCode.
    You can't post JavaScript.
    You can post emoticons.
    You can't post or upload images.

    Select a forum

































































































































































    SQLServerCentral


    Search