Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Maachie
Yak Posting Veteran
69 Posts |
Posted - 2008-11-11 : 16:25:22
|
| I have the data in my column CHDEFAULTLOGIN like sozzz100107RILEYM4zzz100107Russolzzz100107siekrauszzz100107wangw2zzz100108cassam1zzz100108gascokzzz100108INTCOLMJzzz100108malirrI want to pull only data that are older than 30days past. For eg; today is 11/11/2008 I want anything older than 10/10/2008. The data has " either zzz or zz to disable with date like mmdtyear" I want to just pull the data older than 30days and delete them. How would i pull just those that i want.A sample query would help.I tried (CHDEFAULTLOGIN LIKE '%zzz10%')and (CHDEFAULTLOGIN LIKE '%2008%')Thanks,Maachie |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-11-11 : 16:35:37
|
| how about CHDEFAULTLOGIN LIKE '%zzz10__08%' |
 |
|
|
Kumar_Anil
Yak Posting Veteran
68 Posts |
Posted - 2008-11-11 : 20:58:59
|
| You need 2 different things here. You need a UDF that would seperate the numbers and then convert it into date. Here is the function for it. --Function that searches a string and seperates numbers out of itCreate function [dbo].[SeperateNumsInString](@InTheString varchar(5000))returns varchar(5000)asbeginwhile patindex('%[^0-9]%',@InTheString)>0Set @InTheString = rtrim(ltrim(replace(@InTheString,substring(@InTheString,patindex('%[^0-9]%',@InTheString),1),'')))return @InTheStringend/*The sql for it using your own test data except for the last row as you havent provided any data for less than 30 day date.*/declare @Login table (CHDEFAULTLOGIN varchar(100))insert into @Loginselect 'zzz100107RILEYM4' unionselect 'zzz100107Russol' unionselect 'zzz100107siekraus' unionselect 'zzz100107wangw2' unionselect 'zzz100108cassam1' unionselect 'zzz100108gascok' unionselect 'zzz100108INTCOLMJ' unionselect 'zzz100108malirr' unionselect 'zzz102108malirr' select CHDEFAULTLOGIN, Numbers = dbo.SeperateNumsInString(CHDEFAULTLOGIN),Date = convert(datetime,'20'+right(substring(dbo.SeperateNumsInString(CHDEFAULTLOGIN),1,6),2)+left(right('00'+substring(dbo.SeperateNumsInString(CHDEFAULTLOGIN),1,6),6),4)),Newdate = DateAdd(day, -30, convert(datetime,'20'+right(substring(dbo.SeperateNumsInString(CHDEFAULTLOGIN),1,6),2)+left(right('00'+substring(dbo.SeperateNumsInString(CHDEFAULTLOGIN),1,6),6),4))) from @Loginwhere convert(datetime,'20'+right(substring(dbo.SeperateNumsInString(CHDEFAULTLOGIN),1,6),2)+left(right('00'+substring(dbo.SeperateNumsInString(CHDEFAULTLOGIN),1,6),6),4)) > dateadd(day,-30,getdate()) |
 |
|
|
Maachie
Yak Posting Veteran
69 Posts |
Posted - 2008-11-13 : 08:01:17
|
| hanbinglThanks for your response. I want older than CHDEFAULTLOGIN LIKE '%zzz10__08%' fromzzz100107RILEYM4zzz100107Russolzzz100107siekrauszzz100107wangw2zzz100108cassam1zzz100108gascokzzz100108INTCOLMJzzz100108malirrwhich means i should be able to pull out for eg these:zzz100107RILEYM4zzz100107Russolzzz100107siekrauszzz100107wangw2How would i do that?Thanks,Maachie |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2008-11-13 : 08:25:17
|
| select * from <yourtable>where convert(datetime,'20'+ substring(replace(CHDEFAULTLOGIN ,'z',''),5,2) + left(replace(CHDEFAULTLOGIN ,'z',''),2)+substring(replace(CHDEFAULTLOGIN ,'z',''),3,2))<'20081001' |
 |
|
|
Maachie
Yak Posting Veteran
69 Posts |
Posted - 2008-11-13 : 08:47:39
|
| Hi Kumar_AnilI tried this UDF funtion. I did not understand what does this pc below required for?Newdate = DateAdd(day, -30, convert(datetime,'20'+right(substring(dbo.SeperateNumsInString(CHDEFAULTLOGIN),1,6),2)+left(right('00'+substring(dbo.SeperateNumsInString(CHDEFAULTLOGIN),1,6),6),4))) This is the result set i got from running the functionCHDEFAULTLOGIN Numbers Date Newdatezzz102108malirr 102108 10/21/08 09/21/08Also, i need all records prior to 30days.Thanks,Maachie |
 |
|
|
Maachie
Yak Posting Veteran
69 Posts |
Posted - 2008-11-13 : 08:49:43
|
| Hi sakets_2000Msg 241, Level 16, State 1, Line 1Conversion failed when converting datetime from character string. |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2008-11-13 : 08:59:42
|
| Then you might have data which doesn;t follow the format you mentioned.Run this and post the output,select * from test where isdate('20'+ substring(replace(CHDEFAULTLOGIN ,'z',''),5,2) + left(replace(CHDEFAULTLOGIN ,'z',''),2)+substring(replace(CHDEFAULTLOGIN ,'z',''),3,2))<>1 |
 |
|
|
Maachie
Yak Posting Veteran
69 Posts |
Posted - 2008-11-13 : 09:09:05
|
| Can you please explain what this pc of code does?isdate('20'+ substring(replace(CHDEFAULTLOGIN ,'z',''),5,2) + left(replace(CHDEFAULTLOGIN ,'z',''),2)+substring(replace(CHDEFAULTLOGIN ,'z',''),3,2))<>1Thanks,Maachie |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2008-11-13 : 09:22:34
|
| Some data there isn't in the format you mentioned.It could be because you might have something other than "z" stuffed in the beginning, or may be somedata which doesn't suit mmddyy format like you mentioned.The query I gave you will bring out cases like these. Isdate is a function which would return you 1 if it can be converted to datetime, 0 otherwise. |
 |
|
|
Maachie
Yak Posting Veteran
69 Posts |
Posted - 2008-11-13 : 09:29:40
|
| So in my data there are old data and i want everything prior to 30days data. what i mean by that is today is 11/13/08, i want everything prior to 10/13/08. Does your query bring that? I understand that the data also has z, zz, zzz before the mm/dt/year.Thanks,Maachie |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2008-11-13 : 10:19:23
|
| select * from <yourtable>where convert(datetime,'20'+ substring(replace(CHDEFAULTLOGIN ,'z',''),5,2) + left(replace(CHDEFAULTLOGIN ,'z',''),2)+substring(replace(CHDEFAULTLOGIN ,'z',''),3,2))<'20081001'The above should bring you that. But since you're getting an error. I asked you to run the below query to find out why it was erring.Can you run it and post some data that it fetches, please .select CHDEFAULTLOGIN from test where isdate('20'+ substring(replace(CHDEFAULTLOGIN ,'z',''),5,2) + left(replace(CHDEFAULTLOGIN ,'z',''),2)+substring(replace(CHDEFAULTLOGIN ,'z',''),3,2))<>1 |
 |
|
|
Maachie
Yak Posting Veteran
69 Posts |
Posted - 2008-11-13 : 11:17:13
|
| and CHDEFAULTLOGIN not LIKE '%zzz10__08%' and CHDEFAULTLOGIN not LIKE '%zzz11__08%' is bringing me excluding for the month Oct and Nov. I get about 6193 rowsBut what I wanted is everything prior to 30days. WHERE (CHDATA <> '') AND (CHSERIALNUM = '') AND (CHDEFAULTLOGIN LIKE 'zz%')and isdate('20'+ substring(replace(CHDEFAULTLOGIN ,'z',''),5,2) + left(replace(CHDEFAULTLOGIN ,'z',''),2)+substring(replace(CHDEFAULTLOGIN ,'z',''),3,2))<>1This brings only 131 rows.Thanks,Maachie |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2008-11-13 : 11:40:12
|
| can you post some data that you got out of that query ?> |
 |
|
|
Maachie
Yak Posting Veteran
69 Posts |
Posted - 2008-11-13 : 11:50:01
|
| From this, I get the data belowSELECT TOP (100) PERCENT ITOKENNUM, BENABLED, CHSERIALNUM, DATEPIN, DATEENABLED, DATETOKENASSIGNMENT, DATELASTLOGIN, IUSERNUM, CHLASTNAME, CHFIRSTNAME, CHDEFAULTLOGIN, CHKEY, CHDATA, iReplacingTokenNum, iTypeFROM dbo.ACE_USERSWHERE (CHDATA <> '') AND (CHSERIALNUM = '') AND (CHDEFAULTLOGIN LIKE 'zz%')and CHDEFAULTLOGIN not LIKE '%zzz10__08%' and CHDEFAULTLOGIN not LIKE '%zzz11__08%'ORDER BY CHDEFAULTLOGINCHLASTNAME CHFIRSTNAME CHDEFAULTLOGINZZZ ZZZZ ZZChen Zhanling zz01042008CHENZ3Honore Sandrine zz01102008honorsColak Kristijan zz01312008COLAKKShewchuk April zz02072008INTSHEWAGalligan Sheila zz030308GALLISDisco Greg zz041007DISCOGMatteson Timothy zz043008mattet1Eden Gregory zz050707EDENGMakeeva Irina zz070708MAKEEIBalasubramanian Muthuvadivel zz081707TCSBALAMSteele Greg zz090706STEELGWalker Leon D zz122006WALKEL1Wu Yuan zzz002222008WUY6Turner Allison zzz01/02/2008TURNEAball michael zzzzBALLM112-18-06-BulkBarreras Nathan zzzzbarren2-28-bulkBaugh Michael zzzzBAUGHM12-28-bulkBebie Glenn zzzzBEBIEG2-28-bulkBell Thomas zzzzBELLT2-28-bulkGALLAGHER JOSEPH zzzzBENGALLJ12-18-06-BulkMoylan David zzzzBENMOYLD12-18-06-BulkHebert Matthew zzzzwrongly craetedFrom this the data i get is:SELECT TOP (100) PERCENT ITOKENNUM, BENABLED, CHSERIALNUM, DATEPIN, DATEENABLED, DATETOKENASSIGNMENT, DATELASTLOGIN, IUSERNUM, CHLASTNAME, CHFIRSTNAME, CHDEFAULTLOGIN, CHKEY, CHDATA, iReplacingTokenNum, iTypeFROM dbo.ACE_USERSWHERE (CHDATA <> '') AND (CHSERIALNUM = '') AND (CHDEFAULTLOGIN LIKE 'zz%')and isdate('20'+ substring(replace(CHDEFAULTLOGIN ,'z',''),5,2) + left(replace(CHDEFAULTLOGIN ,'z',''),2)+substring(replace(CHDEFAULTLOGIN ,'z',''),3,2))<>1CHLASTNAME CHFIRSTNAME CHDEFAULTLOGINPothapu Jagadish zzzpothaj11092006_BULKGreenplate Carrie zzz61406GREENC5_bulkSharma Rohan zzzaccsharr11092006_BULKLiu Andy zzz12/30/2007liua1Ahmed Mohammed zzz02/01/08ahmedm4Kimball Glenda zzzkimbag11092006_BULKNayak Bharat zzzzALLNAYAB12-18-06-BulkNagaraja Kumar zzz0690108nagarkSuram Karunakar zzzzALLSURAK1-18-07-bulkGoswami Richa zzzallgoswr11092006_BULKNandini Uma zzznandiu11092006_BULKLammert Marco zzzLAMMEM11092006_BULKDydek Jerry zzzdydekj11092006_BULKCheng Allan zzz13007chenga1Allsup Kerry zzzallsuk11092006_BULKNagel Christopher zzz-11-21-2007-idmcleanupNAGELSLim Hong zzz04007limaPerry Mark zzzperrym11092006_BULKPicherit Sandrine zzzdnbpiches11092006_BULKDessoubret Gilles ZZZ270408dessog |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2008-11-13 : 12:21:51
|
| right, the first row there --"zzzpothaj11092006_BULK", Does this follow the rules you mentioned earlier ??Its got more than 'zzz' or 'zz' coming, Its got 'zzzpothaj','zzzaccsharr' etc. Other problems include , dates coming like 11-21-2007. From what you mentioned earlier, You'd see just 'zz' or 'zzz', and dates coming in mmddyy format. |
 |
|
|
Kumar_Anil
Yak Posting Veteran
68 Posts |
Posted - 2008-11-13 : 12:44:52
|
| Hey Maachie,What you got in here is a bit complicated problem & Im sure you know that. I ran into a similar situation few months ago & that is what I did. Here is the explanation of what Im doing here.The UDF pulls out only the numeric components out of the string that is stored in the column, CHDEFAULTLOGIN and then what I do after that is convert that into a DATE column. There is one more step involved in here & I format that date into a six digit number & then assign a Day value for the first 2 numbers, a Month value for the next 2 numbers and finally an Year value for the last two.Im not sure what is your confusion about ? Please let me know what you need or if you are not clear about anything that I posted.regards,Anil Kumar.----------------------------------------------------------------------Run it from here -----------------------------------------------------------------------------------------------------------------------Function that searches a string and seperates numbers out of itCreate function [dbo].[SeperateNumsInString](@InTheString varchar(5000))returns varchar(5000)asbeginwhile patindex('%[^0-9]%',@InTheString)>0Set @InTheString = rtrim(ltrim(replace(@InTheString,substring(@InTheString,patindex('%[^0-9]%',@InTheString),1),'')))return @InTheStringend/*The sql for it using your own test data except for the last row as you havent provided any data for less than 30 day date.*/declare @Login table (CHDEFAULTLOGIN varchar(100))insert into @Loginselect 'zzz100107RILEYM4' unionselect 'zzz100107Russol' unionselect 'zzz100107siekraus' unionselect 'zzz100107wangw2' unionselect 'zzz100108cassam1' unionselect 'zzz100108gascok' unionselect 'zzz100108INTCOLMJ' unionselect 'zzz100108malirr' unionselect 'zzz102108malirr' select CHDEFAULTLOGIN, Numbers = dbo.SeperateNumsInString(CHDEFAULTLOGIN),Date = convert(datetime,'20'+right(substring(dbo.SeperateNumsInString(CHDEFAULTLOGIN),1,6),2)+left(right('00'+substring(dbo.SeperateNumsInString(CHDEFAULTLOGIN),1,6),6),4)),Newdate = DateAdd(day, -30, convert(datetime,'20'+right(substring(dbo.SeperateNumsInString(CHDEFAULTLOGIN),1,6),2)+left(right('00'+substring(dbo.SeperateNumsInString(CHDEFAULTLOGIN),1,6),6),4))) from @Loginwhere convert(datetime,'20'+right(substring(dbo.SeperateNumsInString(CHDEFAULTLOGIN),1,6),2)+left(right('00'+substring(dbo.SeperateNumsInString(CHDEFAULTLOGIN),1,6),6),4))> dateadd(day,-30,getdate()) |
 |
|
|
Maachie
Yak Posting Veteran
69 Posts |
Posted - 2008-11-13 : 13:11:03
|
| Hi sakets_2000But why your query pulls less rows about 200 something and mine about 6000 something? |
 |
|
|
Maachie
Yak Posting Veteran
69 Posts |
Posted - 2008-11-13 : 13:12:58
|
| Hi Kumar_AnilCHDEFAULTLOGIN Numbers Date Newdatezz01042008CHENZ3 010420083 2020-01-04 00:00:00.000 2019-12-05 00:00:00.000zz01102008honors 01102008 2020-01-10 00:00:00.000 2019-12-11 00:00:00.000zz01312008COLAKK 01312008 2020-01-31 00:00:00.000 2020-01-01 00:00:00.000zz02072008INTSHEWA 02072008 2020-02-07 00:00:00.000 2020-01-08 00:00:00.000I don't get this? Why is the new date col showng 2020 for year? How does this going to get anything prior to 3days old data from the column? |
 |
|
|
Kumar_Anil
Yak Posting Veteran
68 Posts |
Posted - 2008-11-13 : 13:22:16
|
| Maachie,Looks like the sample test data that you gave before is different than what you wrote in here now. As I just explained, Im trying to assign the last 2 digits as Year & Im appending 20 in front of it & that is what is the following piece of code ..."convert(datetime,'20'+right(substring(dbo.SeperateNumsInString(CHDEFAULTLOGIN),1,6),2)+"But it looks like you now have different data & what my function is doing is taking the last 2 digits & then adding 20 infront of it & that is how you see 2020.You can just work on "select CHDEFAULTLOGIN, Numbers = dbo.SeperateNumsInString(CHDEFAULTLOGIN)," and that will give you only numbers that you need to pay attention to & you can then split them accordingly.regards,Anil Kumar. |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2008-11-13 : 13:51:20
|
quote: Originally posted by Maachie Hi sakets_2000But why your query pulls less rows about 200 something and mine about 6000 something?
The second query I gave you is to see which records don't fit the format you mentioned earlier. Your requirements and the format of data has changed betwteen the posts. If you can outline your problem clearly, It'll be great :) |
 |
|
|
Next Page
|
|
|
|
|