| Author |
Topic |
|
Yisman
Starting Member
8 Posts |
Posted - 2008-10-23 : 13:34:05
|
Hi Gurus!My database has addresses from several different countries. Different countries have different formats of how they set up address labels. Some countries you have:123 main streetMyCity, MyState 98765in other places addresses are written out as:Main Street 12398765 MyCityand many other variations (apt/flat/floor/po box...)Hence, I have a table Countries which has a field AddressFormat where i put a sql clause for each country. here are 2 samples: CountryName / AddressFormat Austria / Street + IsNull(HouseNum,'') + CHAR(10) + ZipCodeUSA / IsNull(HouseNum,'') + Street + CHAR(10) + CityName My question is, is there any way to return a view or sproc which shows a formatted address for each row according to the field list in "AddressFormat" of particular country?As far as I saw, Dynamic SQL via EXEC cannot change the columns on a per-row basis.I know I could work around with clientside String.Replace functions, but I am looking for a more elegent solution.I hope my question is clear, otherwise I'd be glad to rephrase.Thank you all for your time and patienceYisman |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-10-23 : 14:29:25
|
Let me make this clear:You have a table of addresses:HOUSENUM STREET CITYNAME STATE ZIP--------- ------- --------- ------ ------123 MAIN ST New York NY 10000 You have another table of countries:Country Name Address Format----------------- ----------------------------------------------------USA Street + IsNull(HouseNum,'') + CHAR(10) + ZipCodeAUSTRIA IsNull(HouseNum,'') + Street + CHAR(10) + CityName How you want to show this Address in this format:US 123 MAIN ST 10000AUSTRIA MAIN ST 123 New York Assume AddressFormat column contains the SQL to display for each country... |
 |
|
|
Yisman
Starting Member
8 Posts |
Posted - 2008-10-23 : 17:28:36
|
| Hi Hanbingl!Thank you for joining!You are 100% correct. I couldn't (and have'nt ;-)) have explained it any better then that.just for completeness ill add that i have another 2 tables that links every city to a state (-"Cities") and one that links every state to a country(-"States"). but it is really beside the point.awaiting your suggestion!Yisman |
 |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-10-23 : 18:47:54
|
| [code]declare @sql varchar(8000)select @sql='create table #tmpadr(ID int,HOUSENUM varchar(10),STREET varchar(20),CITYNAME varchar(20),STATE varchar(2),ZIPCODE varchar(10),Country varchar(10),Format varchar(8000),NEWADDR VARCHAR(8000))insert into #tmpadr (ID, HOUSENUM, STREET, CITYNAME, STATE, ZIPCODE, Country, Format)select a.ID, HOUSENUM, STREET, CITYNAME, STATE, ZIPCODE, b.countryname, b.addressfmt from address a, country b;declare @sql varchar(8000),@format varchar(8000)set nocount ondeclare cur cursor forselect distinct Format from #tmpadrOPEN curFETCH NEXT FROM cur INTO @formatWHILE @@FETCH_STATUS=0BEGINset @sql=''update #tmpadr set newaddr = ''+@format+'' where Format=''''''+replace(@format,'''''''','''''''''''')+''''''''execute(@sql)FETCH NEXT FROM cur INTO @formatENDCLOSE curDEALLOCATE curselect country, NEWADDR from #tmpadrdrop table #tmpadr'set nocount onexecute(@sql)[/code] |
 |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-10-23 : 18:55:34
|
| Trust me, you want to leave the formatting to the Front end |
 |
|
|
Yisman
Starting Member
8 Posts |
Posted - 2008-10-24 : 02:53:34
|
| Hi Hanbingl!Thanks for your posts.If the only way to to it in SQL is via a cursor loop then, yes, I will be leaving it to the presentation logic.Thanks for clearing it up for me.If anyone knows of a simpler way to accomplish this in MSSQL, I'd very appreciate their ideas.Thank you all |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-10-24 : 03:56:36
|
quote: Originally posted by Yisman Hi Hanbingl!Thanks for your posts.If the only way to to it in SQL is via a cursor loop then, yes, I will be leaving it to the presentation logic.Thanks for clearing it up for me.If anyone knows of a simpler way to accomplish this in MSSQL, I'd very appreciate their ideas.Thank you all
It can't be done with cursor or while loopMadhivananFailing to plan is Planning to fail |
 |
|
|
Yisman
Starting Member
8 Posts |
Posted - 2008-10-24 : 05:18:27
|
| hi Madhivananthanks for joiningcan't or can?that it can be done with iteration, we see from Hanbingl's post. do you have a different, non-looping, way?Thanks |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-10-24 : 05:40:44
|
quote: Originally posted by Yisman hi Madhivananthanks for joiningcan't or can?that it can be done with iteration, we see from Hanbingl's post. do you have a different, non-looping, way?Thanks
It can be done only with loopMadhivananFailing to plan is Planning to fail |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2008-10-24 : 06:17:47
|
quote: It can be done only with loopMadhivananFailing to plan is Planning to fail
It can. But please, for the love of Codd, don't. Do it in your application instead after querying the Locale.-------------Charlie |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-10-24 : 06:42:48
|
quote: Originally posted by Transact Charlie
quote: It can be done only with loopMadhivananFailing to plan is Planning to fail
It can. But please, for the love of Codd, don't. Do it in your application instead after querying the Locale.-------------Charlie
I always suggest that I also suggest to do these in front end application1 Date formation2 Generate Serial No3 Generate Running Total4 Suppress Duplicates5 Currency formation6 Concatenate valuesIf you joined the forums before 2007, you might have seen all my "Do it in front end" replies MadhivananFailing to plan is Planning to fail |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2008-10-24 : 06:49:18
|
quote: I always suggest that I also suggest to do these in front end application1 Date formation2 Generate Serial No3 Generate Running Total4 Suppress Duplicates5 Currency formation6 Concatenate valuesIf you joined the forums before 2007, you might have seen all my "Do it in front end" replies Madhivanan
I think I joined midway through 2007. Can't remember. The shortest and most frequent comments I remember are the ones that said:"How to ask a question": and then a link to a very concise page.I agree with all your points except maybe number 4) Suppress Duplicates. Surely better to modify query so that it doesn't spit out dups in the first place. (NB: I don't mean just add SELECT DISTINCT!)-------------Charlie |
 |
|
|
Yisman
Starting Member
8 Posts |
Posted - 2008-10-24 : 07:15:36
|
| not all your "rules" are clear, id be happy for more insight1&5 are obvoius. formatting is always frontend2: do u mean item number for each product? i ALWAYS use an identity column for that, with gr8 success, i also leave an optional field 4 users 2 fill in with their own homegrown ids or SNs3: totals are split. some totals are simple SUMs. but there are totals that factor many parameters, and for consistency, its best put into the view. i.e. qty*unitprice-discount-CustDiscountPercent*tax etc. keeping it in the BE makes sure u never skip a field or miscalc4: u mean unique db values. thats what indeciis r 4, no? front end cannot be counted on6: as with totals, 4 consistency i usually do it in a view. imagine rewriting alll over an expression like "isnull(prefix+' ','')+ isnull(firstname,'')+ isnull(lastname,'')" NEVER!that is THE reason i wanted 2 do the address formatting in the BE, Consistency. But not for any price...all the best |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2008-10-24 : 08:00:46
|
| Hi Yisman,I think Madhi's points are easily broken down into:1) Don't mess with the data-types2) Don't ask the server to do things it's not good/efficient atSo for his points.....1 Date formation (don't screw with the data. it's passed to your application in a nice consistent type (DATETIME) and then you can format it there)2 Generate Serial No (Actually I'm not sure what he means by this. Because he says Generate I'm assuming that no unique Serial number exists for whatever he brings back)3 Generate Running Total (best way to do this is with a nice little loop. Therefore don't do it in db. do it in your iterative app instead)4 Suppress Duplicates (You are artificially hiding the data from the application here. If your data actually contains dups (rather than arising from misbehaving query) then you should report them).5 Currency formation (See point 1)6 Concatenate values (See point 3)Madhivanan, I'd be interested if you could explain what you mean in point 2. I think I've got a handle on your other points.-------------Charlie |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-10-24 : 08:16:18
|
| <<Madhivanan, I'd be interested if you could explain what you mean in point 2. I think I've got a handle on your other points.>>Assume that your procedure returns N number of rows based on the values of input parametersWhat, if you want to show serial number as the first column?Though can be done using row_number() in SQL Server 2005 onwards, it is ineffecient in earlier versionsMadhivananFailing to plan is Planning to fail |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-10-24 : 08:22:02
|
Also note that by Suppress Duplicates I actually meantSample datacol1 col2-------------test1 100test1 9822test1 81123test2 100test2 1330test3 123test3 56Expected resultcol1 col2-------------test1 100 9822 81123test2 100 1330test3 123 56 MadhivananFailing to plan is Planning to fail |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2008-10-24 : 08:23:28
|
| Ah I see. Thanks for explaining.I love ROW_NUMBER(). So useful for so many situations.NB: - And I see exactly what you mean about the dups now also.-------------Charlie |
 |
|
|
|