Correcting truncated zip codes in Magento through SQL query - sql

Zip codes with leading zeroes were truncated without the zeroes after a migration from X-Cart to Magento. I'm trying to run a SQL query to add the zeroes back but don't know which table/column to run it against in the database. We use the zip code to determine which shipping services will be available to the customer during checkout. Does anyone know where the shipping address (both default and additional) zip codes are located in the database?

The zip / postal code in magento is an attribute of the customer_address entity.
Look in the table mage_eav_attribute to find what the is attribute id and where it is stored, e.g. execute this query to find out where magento is storing the attribute:
select attribute_id
, backend_type
, attribute_code
from mage_eav_attribute
where attribute_code like '%post%' or attribute_code like '%zip%'
For my implementation, the attribute_id = 29, attribute_code = 'postcode' and backend_type = 'varchar'. The column backend_type will tell you what kind of attribute this is on the backend.
varchar tells me that magento stores this attribute in a table called mage_customer_address_entity_varchar. For you this might be different because it seems to treat it as an integer and truncate leading zeros.
Then I can execute the following sql to get the postcodes. Of course, you'd have to modify the attribute table mage_customer_address_entity_varchar to where your implementation of magento is storing this attribute.
select e.entity_id, a.value atype.attribute_code
from mage_customer_address_entity e
join mage_customer_address_entity_varchar a ON a.entity_id = e.entity_id
join mage_eav_attribute atype ON atype.attribute_id = a.attribute_id
where atype.attribute_code = 'postcode'
Finally, you need this for shipping addresses (and not billing addresses?). Default shipping addresses are attributes of customer, so you'll have to modify my above approach to find which customer attribute table contains that information.
I hope this helps you figure out your solution
Cheers

could it be possible for you to just ignore theses leading 0? since for the logic of the system it doesn't matter and if you have to print it, just get the length of the string and add the leading 0 as a string if you want/need to.
If that doesn't work for you, you will have to look into source code of the module you are using to see wich table the module uses.

Related

Convert table design without impacting code

Background
We have a very old database design. The worst example is the config table. It's one row with a separate column for each value. It should be a key value pair setup (obviously). It's used everywhere in both our old legacy code, and now in our new web based code. Horrifyingly there are now over 300 columns and worse more are added every now and again.
Goal
I'd love to chuck the table in the bin and replace it with a correct key value pair. However coding time is short so I'm trying to come up with a clever way of displaying it as the old one row for the old legacy code retrieving it from a key value pair.
Attempted Solution
Ordinarily I'd use a view - but given that the best code to display it (pinched from here: https://stackoverflow.com/a/15745076/12059261) is dynamic SQL, I've come unstuck as I cannot use dynamic SQL in a view (my searching tells me that views do not allow dynamic SQL - I'm assuming this is correct?)
Table example
CurrentConfig
Key Name Address etc.
1 My Company My Address etc.
WantedConfig
ID Key Value
1 Name MyCompany
2 Address My Address
3 etc. etc.
I want to change the table from its current form to the new form, but still be able to display it as the current form to legacy code.
If the issue is displaying the new config to legacy code, then use conditional aggregation:
create view oldconfig as
select max(case when key = 'Name' then value end) as name,
max(case when key = 'Address' then value end) as address,
. . .
from newconfig;
Don't worry about dynamic SQL. There are a fixed set of columns used in the legacy code and that is what needs to go in the view. If you add new keys, they will not go in the view, but presumably the new keys are not used by the old code anyway.
You can use apply :
select tt.id, tt.Key, tt.Val
from table t cross apply
( values (1, 'Name', Name),
(2, 'Address', Address),
(3, 'etc.', etc)
) tt(id, Key, Val)

Search through Users with dynamic attributes with SQL

.Hi i'm working with Asp and SQL-Server and i have no problem with writing dynamic query
I'm trying to Write a search page for searching people.
I have 3 related tables:
See my table diagram in : http://tinypic.com/r/21159go/5
What i'm trying to do is to design a search page that a person can search users with a dynamic number of attributes.
Example:
think that a username called "User1" has 3 attributes named "Attr1", "Attr2" and "Attr3" related to him in "UserAttributes" table and "User2" has 3 attributes named "Attr1", "Attr2" and "Attr4".
Attribute names and other bunch of items unrelated to search function saved in "Attributes" Table. This is because i want to relate an attribute between multiple users. and their values are stored in "UserAttributes" table.
Well someone wants to search upon "Attr1" and "Attr2" and wants to return all users that have "Attr1" and "Attr2" with specific value.
I need a query to know how to implement this. I can write a dynamic query with asp.net so if someone please give me a query for this one example i have brought, i would be thankful
P.S. This is not my real database. my real database is much more complex and has more fields and tables but i just cut it and brought only necessary items. and because attributes are very dynamic they can't be embedded in table columns.
Thanks in advance
Based on your DB diagram your code would be something like this
update:
SELECT u.*
FROM users AS u
LEFT OUTER JOIN UserAttributes AS ua1
ON u.USER_ID = ua1.USER_ID
LEFT OUTER JOIN UserAttributes AS ua2
ON u.USER_ID = ua2.USER_ID
WHERE (
ua1.attribute_id = 'att1'
AND ua.attribute_value = 'MyValue' )
AND (
ua2.attribute_id = 'att2'
AND ua.attribute_value = 'MyValue2' )
In where clause you would specify the attirbute_Id and what value you are expecting out of it. Than just decide if you want to restrict users to have all values match or just one of them, in that case modify AND between statements to be OR
if you just want to do this quick and dirty you can create your own class library that can create adhoc sql that will pass to the database.
if you want more organized matter, create SP that will bring back users and accepts any left of id and value. Do a lot of that by passing list separated by comma, colon or semicolon. Than split it up in SP and filter results based on those values
there are many other alternatives like EntityFramework, LINQ-to-SQL and other options, just need to figure out what works best for you, how much time you want to spend on it and how easy will it be to support later.

SQL Server: Remove substrings from field data by iterating through a table of city names

I have two databases, Database A and Database B.
Database A contains some data which needs to be placed in a table in Database B. However, before that can happen, some of that data must be “cleaned up” in the following way:
The table in Database A which contains the data to be placed in Database B has a field called “Desc.” Every now and then the users of the system put city names in with the data they enter into the “Desc” field. For example: a user may type in “Move furniture to new cubicle. New York. Add electric.”
Before that data can be imported into Database B the word “New York” needs to be removed from that data so that it only reads “Move furniture to new cubicle. Add electric.” However—and this is important—the original data in Database A must remain untouched. In other words, Database A’s data will still read “Move furniture to new cubicle. New York. Add electric,” while the data in Database B will read “Move furniture to new cubicle. Add electric.”
Database B contains a table which has a list of the city names which need to be removed from the “Desc” field data from Database A before being placed in Database B.
How do I construct a stored procedure or function which will grab the data from Database A, then iterate through the Cities table in Database B and if it finds a city name in the “Desc” field will remove it while keeping the rest of the information in that field thus creating a recordset which I can then use to populate the appropriate table in Database B?
I have tried several things but still haven’t cracked it. Yet I’m sure this is probably fairly easy. Any help is greatly appreciated!
Thanks.
EDIT:
The latest thing I have tried to solve this problem is this:
DECLARE #cityName VarChar(50)
While (Select COUNT(*) From ABCScanSQL.dbo.tblDiscardCitiesList) > 0
Begin
Select #cityName = ABCScanSQL.dbo.tblDiscardCitiesList.CityName FROM ABCScanSQL.dbo.tblDiscardCitiesList
SELECT JOB_NO, LTRIM(RTRIM(SUBSTRING(JOB_NO, (LEN(job_no) -2), 5))) AS LOCATION
,JOB_DESC, [Date_End] , REPLACE(Job_Desc,#cityName,' ') AS NoCity
FROM fmcs_tables.dbo.Jobt WHERE Job_No like '%loc%'
End
"Job_Desc" is the field which needs to have the city names removed.
This is a data quality issue. You can always make a copy of the [description] in Database A and call it [cleaned_desc].
One simple solution is to write a function that does the following.
1 - Read data from [tbl_remove_these_words]. These are the phrases you want removed.
2 - Compare the input - #var_description, to the rows in the table.
3 - Upon a match, replace with a empty string.
This solution depends upon a cleansing table that you maintain and update.
Run a update query that uses the input from [description] with a call to [fn_remove_these_words] and sets [cleaned_desc] to the output.
Another solution is to look at products like Melisa Data (DQ) product for SSIS or data quality services in the SQL server stack to give you a application frame work to solve the problem.

OpenERP customers separation

Please I would like to know ho to separate customers in category, since we have two type of customers, the first group should only appear in crm->customers only and vice versa to avoid having a huge list of customers when searching.
My first attempt is to add a tag to different customers to separate them, for example the crm customers have the tag name Mass mailing is it correct to achieve this with tags ?? and my second question how to set default search keyword for example if I click on sales -> customers how to set the default value of search box to for example crmOnly tag thanks.
you can use "domain" where ever you want to have a such a separation.

Subfield values in .NET Local Report

I'm attempting to use business objects as a datasource for an ASP.NET Microsoft rdlc Report and I am having problems displaying the values of subobjects in the report.
For example, if have the following two classes 'Customer' and 'Address'.
Customer
FirstName
LastName
Address
etc.
Address
HomeAddress1
etc.
Customer contains an instance of Address. If I set a list of Customers as the report datasource, I can refer to the values of each customer in the report as follows:
=Fields!FirstName.Value
However, I can't work out how to refer to the values of the 'Address' sub-object. I had assumed one of the following would work:
=Fields!Address.HomeAddress1.Value
or
=Fields!Address!HomeAddress1.Value
But neither of these do. Can anyone advise?
I had the exact same problem, and I'm sorry to say that I haven't found a way to reference sub-properties (I do hope that someone else has one though!)
What I ended up doing is creating a wrapper class that has all the properties I need directly in it.
The following syntax seems to work.
=Fields!Address.Value.HomeAddress1