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 |
|
briankind
Starting Member
17 Posts |
Posted - 2009-01-21 : 17:24:12
|
| Update Update/insert table A based on table A and B 3 tables A and B and Ctable A has the Date_category field in text/stringDate_categorytable B has the Coll_rec_date in smalldatetimetable C has the Date_category_OLD in text/stringI want to populate table A based on information on table,B and Cthe first thing isif Coll_rec_date date range of table B is from July 1 2008 to june 30 2009 then insert 09if Coll_rec_date date range of table B is from July 1 2007 to june 30 2008insert 08if Coll_rec_date date range of table B is from July 1 2006 to june 30 2007insert 07if Coll_rec_date date range of table B is from July 1 2005 to june 30 2006insert 06and if there are any nulls in Coll_rec_date date then populate it with with the value of table C has the Date_category_OLD ( if it is not null). can you help me with the code thanks |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
thiyagu_rind
Starting Member
46 Posts |
Posted - 2009-01-22 : 00:06:34
|
| Hi Dear,Try this below Query:QueryINSERT INTO A(Date_category)SELECT CASE WHEN isnull(B.Coll_rec_date,C.Coll_rec_date) BETWEEN 'July 1 2008' AND 'june 30 2009' THEN '09' WHEN isnull(B.Coll_rec_date,C.Coll_rec_date) BETWEEN 'July 1 2007' AND 'june 30 2008' THEN '08' WHEN isnull(B.Coll_rec_date,C.Coll_rec_date) BETWEEN 'July 1 2006' AND 'june 30 2007' THEN '07' WHEN isnull(B.Coll_rec_date,C.Coll_rec_date) BETWEEN 'July 1 2005' AND 'june 30 2006' THEN '06' ELSE '' ENDFROM B INNER JOIN C ON B.ID = C.IDHere in the above query i used an ID field which helps you to make join between B and C. I hope the ID column will be there. If not use row_number function to generate and use it. Here i dont have SQL 2005 to use that..I hope it will help you a bit.RegardsThiyagarajanwww.sqlhunt.blogspot.com |
 |
|
|
briankind
Starting Member
17 Posts |
Posted - 2009-01-22 : 11:00:43
|
| What happen to Table C has the Date_category_OLDif there are nulls in table A Coll_rec_date date then us ethe value of Table C has the Date_category_OLD to populate A table ----A has the Date_category field in text/string----Date_categoryThanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-22 : 11:12:19
|
| why are you using text fields for storing dates? always try to use proper datatype for fields |
 |
|
|
briankind
Starting Member
17 Posts |
Posted - 2009-01-23 : 10:28:13
|
| I am not storing date values in a texti am trying to store text values in table A based on the range date value from table B another field. -----------------------------------------------table A has the Date_category field in text/stringDate_categorytable B has the Coll_rec_date in smalldatetimetable C has the Date_category_OLD in text/string |
 |
|
|
|
|
|