| 
                
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 |  
                                    | pcockStarting Member
 
 
                                        12 Posts | 
                                            
                                            |  Posted - 2009-06-24 : 01:36:39 
 |  
                                            | [code]select meterid, datetimeins, datetimerem,case when DATEPART(yyyy , datetimeins ) = 2009and DATEPART(mm , datetimeins ) = 1and DATEPART(dd , datetimeins ) = 1 then datetimeins else null end as newdatetimeinsfrom dbo.CashBoxDataImportorder by datetimeread[/code]gives me a list similar to the following.[code]meter_id, datetimeins, datetimerem, newdatetimeins1001	2009-02-11 07:53:00.000	2009-02-18 08:05:00.000	NULL1012	2009-02-11 07:55:00.000	2009-02-18 08:09:00.000	NULL1001	2009-02-18 08:10:00.000	2009-02-25 08:05:00.000	NULL1012	2009-02-18 08:09:00.000	2009-02-25 08:07:00.000	NULL1012	2009-02-25 08:07:00.000	2009-03-04 08:20:00.000	NULL1001	2009-01-01 00:00:00.000	2009-03-04 08:16:00.000	2009-01-01 00:00:00.0001012	2009-03-04 08:20:00.000	2009-03-11 07:39:00.000	NULL[/code]When there's a power failure the meter logs in the first day of the year on field datetimeins,(eg, datetimeins on the 5th row should have been >= 2009-02-25 08:05:00.000 and not 2009-01-01 00:00:00.000)What is the best way to fix this?  I have over 400 meters with this problem, so I can't really update the values manually. |  |  
                                    | khtanIn (Som, Ni, Yak)
 
 
                                    17689 Posts | 
                                        
                                          |  Posted - 2009-06-24 : 01:43:12 
 |  
                                          | quote:How do you get the should have been datetime "2009-02-25 08:05:00.000" ?eg, datetimeins on the 5th row should have been >= 2009-02-25 08:05:00.000 and not 2009-01-01 00:00:00.000
 
 KH[spoiler]Time is always against us[/spoiler]
 |  
                                          |  |  |  
                                    | pcockStarting Member
 
 
                                    12 Posts | 
                                        
                                          |  Posted - 2009-06-24 : 01:49:30 
 |  
                                          | the last time there was any activity on meter 1001 was 2009-02-25 08:05:00.000 (row 3) |  
                                          |  |  |  
                                    | khtanIn (Som, Ni, Yak)
 
 
                                    17689 Posts | 
                                        
                                          |  Posted - 2009-06-24 : 01:54:09 
 |  
                                          | [code]select meterid, datetimeins, datetimerem,       case when datetimeins  = '20090101'            then (select max(datetimerem) from dbo.CashBoxDataImport x                   where  x.meterid = d.meterid and x.datetimerem < d.datetimerem)            else null             end as newdatetimeinsfrom   dbo.CashBoxDataImport dorder by datetimeread[/code] KH[spoiler]Time is always against us[/spoiler]
 |  
                                          |  |  |  
                                    | khtanIn (Som, Ni, Yak)
 
 
                                    17689 Posts | 
                                        
                                          |  Posted - 2009-06-24 : 01:57:10 
 |  
                                          | quote:I assumed that the the meter log of 1st day of the year is without time, then you can just simply useWhen there's a power failure the meter logs in the first day of the year on field datetimeins
 
 case when datetimeins  = '20090101'or if you want to make it more dynamic, this will give you 1st day of current year dateadd(year, datediff(year, 0, getdate()), 0) KH[spoiler]Time is always against us[/spoiler]
 |  
                                          |  |  |  
                                    | pcockStarting Member
 
 
                                    12 Posts | 
                                        
                                          |  Posted - 2009-06-24 : 02:14:36 
 |  
                                          | quote:Thank you khtan :)Originally posted by khtan
 
 select meterid, datetimeins, datetimerem,       case when datetimeins  = '20090101'            then (select max(datetimerem) from dbo.CashBoxDataImport x                   where  x.meterid = d.meterid and x.datetimerem < d.datetimerem)            else null             end as newdatetimeinsfrom   dbo.CashBoxDataImport dorder by datetimeread KH[spoiler]Time is always against us[/spoiler]
 
 |  
                                          |  |  |  
                                |  |  |  |  |  |