I have a table that have 3 columns
id, company and adress
i found a bug today that saved the adress in the company-column and company in the adress-column SOMETIMES, i have corrected the bug and now im trying to put the data in the right places
every adress has a number in it so my guess is that the easiest way is to switch adress and company columns if there is a number in the company-column (if there should be a number in the real company name this wont matter that much :p).
How should i write this in TSQL?
I'm not sure this is right thing to do here but as I can't think of any other alternative this should do it.
Update dbo.MyTable
Set Company = Address,
Address = Company
Where Company like '%[0-9]%'
You can try this: i put a simple protection to avoid the swap if the company adress already contains a number
insert into COMPANY (NAME, ADDRESS)
VALUES ('2 bld d''Italie' , 'CA') ,
('Take 2' , 'anselmo street 234') ,
('Microsoft' , '1 Microsoft Way Redmond'),
('lake street 14' , 'Norton'),
('lake street 17' , 'trendMicro');
SELECT * FROM COMPANY
UPDATE COMPANY set NAME = ADDRESS, ADDRESS = NAME
WHERE NAME like '%[0-9]%' and (ADDRESS not like '%[0-9]%')
SELECT * FROM COMPANY
You could notice that the take 2 line won't be swapped
Related
After reading about normalization I am unsure of how to interpreter the 1 NF requirements
According to wikipedia, something is in first normal form, if the "domain of each attribute contains only atomic indivisible values"
My question is: Who decides what is indivisible or not?
You may divide a date datatype into year, month, day, second, nanoseconds. You may aswell divide an adress into the exact latitude coordinates. When can you really be sure that you have reached 1NF?
Would this table be considered 1NF?
fullName
fullAdresss
Joe Zowesson
87th Victoria Street London EC96 1MB, 14584
Mason Hamburg
47th Jeremy Street London EC26 1MB, 13584
Dedrik Terry
27th Burger Street London EC16 1MB, 17584
My interpretation here is that the value Joe Zowesson is indivisible in regards to the column fullName. And that both zip code, street number and street name is atomic in relation to the column name fullAddress.
I am almost certain that I am in the wrong, but I can not yet understand why.
The question is in regards to an upcoming exam, where I will need to "proove" which normal form something currently is in. Something that I find very hard depending on how you interpreter the word atomic.
You have misunderstood the concept of 1NF basically. By atomic value, it is meant that when you have a column for Name, you should not store any other values alongside it. In other words, the column intended for the Name should not store ID, Address or anything else together with Name, so that when you query the column Name you get only Name, and not name with Id or Address. And Name can be in any form you want whether it be First name + Last name or First name + Last name + Middle name + Previous name.
The decision of whether you need separate columns for the related data should be made during design. Let's suppose you have table Student:
StudentId
FullName
Address
Average grade
1
John Done
New York, US
3.4
2
Robert Bored
New York, US
0
3
Student LName
Dallas, US
1
4
Another LName
Munich, Germany
2
In this case, it means that you do not write queries and don't need data based on First name, Last name separately, but you need all at once for example:
SELECT FullName
FROM Student
WHERE StudentId = 1;
John Done
And when you need First name, Last name separately, you decompose them into several columns, for example:
StudentId
FullName
LastName
Address
Average grade
1
John
Done
New York, US
3.4
2
Robert
Bored
New York, US
0
3
Student
LName
Dallas, US
1
4
Another
LName
Munich, Germany
2
And your queries might look like this:
SELECT LastName, AverageGrade
FROM Student
WHERE AverageGrade >= 1 AND FirstName != 'John';
The result will be:
| LastName | AverageGrade |
---------------------------
| LName | 1 |
| LName | 2 |
Or something like this maybe:
UPDATE Student
SET AverageGrade = 4
WHERE LastName = 'LName' AND FirstName != 'Student'
Basically, the decision depends on how you manipulate the data and in which form you need it.
To sum it up. Whether the relation is in 1NF or not depends on what values you're trying to store on this table, as I mentioned above, one column should store only one type of value, e.g ID, Address, Name, etc. And the decision of how your columns' values will look depends on the design and how you NEED TO STORE the data. If you do not need to query fistname, middlename, lastname, secondname separately, then what you can do is just save all of them in one column FullName and it will still be in 1NF. But if you need them separately, you can store them in separate columns, and again it will still be in 1NF, but it might violate other rules.
Here are some tutorials you might find useful: https://www.studytonight.com/dbms/first-normal-form.php
Let the application, and how it will be used, guide you as to what data should be split further into additional fields (or not).
For example;
If, in your application, you are constantly splitting first name from last name so that you can say "Hi Joe" on correspondence, you should split fullName into two fields. Conversely, If you had two fields firstName and lastName, and were always concatenating them so that you could correctly address an envelope, it would make more sense to have those two fields stored in a single column in your table.
In practice, it is not uncommon for a database to show some de-normalization with the above example given how common both scenarios are but the risk is that they get out of sync if someone updates first name (for example) but doesn't update fullName.
Consider things like how you will force your users to follow a certain pattern if you decide to go with a single column fullName. How would you prevent "Smith, Joe" if your application needed "Joe Smith"?
Dates are another good example and again, whether you split the parts into separate columns depends on how they will be used.
A datetime field which indicates when a row was inserted probably doesn't need to be split out, but if you had many queries which were only interested in the year (for example), it might make sense to split it out.
This only scratches the surface which is why this answer is more about how to think about the underlying problem. Yes normalizing your database is important for all kinds of reasons, but how far you go with it depends on how your data will be used at the end of the day.
I am trying to extract the city from an address where everything is in one column, between last and second to last comma.
For example Address column contains
LYNDEN COURT,CHARTWELL,HAMILTON,3210
CNR DIXON & BANNISTER STREETS,MASTERTON,MASTERTON,5810
3C,SHORT STREET,NEWMARKET,AUCKLAND,1023
As you can see, there is not always the same number of comma's. But I need to extract "Hamilton", "Masterton", and "Auckland" in these examples.
Want it as part of a select statement along with other fields in the same table.
Using SQL Server 2008 R2, but also using SSMS 2014 to access the server
I probably over complicated this but you can try
DECLARE #var VARCHAR(50) = 'CNR DIXON & BANNISTER STREETS,MASTERTON,MASTERTON,5810'
SELECT REVERSE(SUBSTRING(REVERSE(#var), CHARINDEX(',',REVERSE(#var)) +1, CHARINDEX(',', REVERSE(#var), CHARINDEX(',',REVERSE(#var)) + 1) - CHARINDEX(',',REVERSE(#var)) -1))
Is there a way in Crystal Reports where I can select the correct record based on multiple rows of a dataset?
I have a contacts table that links to an address table. In the address table there is a field that defines the address type. The address table may contain multiple addresses for the same contact that might be the type of business, home or mailing addresses.
When selecting my dataset I pull multiple records but my report only needs one. How can I select the appropriate address record depending on what records are there? For example if business address is there I want that, if not I want mailing, if not there I want home.
I need to filter these records within Crystal Reports unless I can modify the sql select.
The output of the sql looks like this:
ABC Company, Main Street, Location
ABC Company, First Street, Mailing
ABC Company, Second Street, Business
Your question should include some relevant data, tables schema and desired output. The following should get you started.
SELECT C.ContactId, C.ContactName
COALESCE(BA.Address, MA.Address, HA.Address) AS Address
FROM Contact C
LEFT JOIN Address BA ON BA.ContactId = BA.ContactId AND BA.AddressTypeId = 1 -- your business address type
LEFT JOIN Address MA ON MA.ContactId = MA.ContactId AND MA.AddressTypeId = 2 -- your mail address type
LEFT JOIN Address HA ON HA.ContactId = HA .ContactId AND HA .AddressTypeId = 3 -- your home address type
Could you please advise me what is the best way of parsing address from string? I have available a table of addresses exported in the form of OSM Points (city, street, house number, country code, post code, geometry column, ...), and text parameter entered by user, for example:
'Prague Letna 15'
This string I need to parse (city, street name, street number, ...) and based on these data I want select from the Points table the greatest similarity point. I will be grateful for any advice.
I treid this:
select *
from parse_address('Prague Letna 15')
but result is not good.
I have a field called Address1 in my table Table1.
Here is an example of data in that field -
8 Brick Lane and 11 Balkerne Drive
I want in a query to spit the street and the number but am struggling with how to achieve this.
Any help would be greatful.
Thanks
SELECT
LEFT(Address1, PATINDEX('%[a-z]%', Address1)- 1) as HouseNumber,
SUBSTRING(Address1, PATINDEX('%[a-z]%', Address1), LEN(Address1)) as Street
FROM Table1
Using PATINDEX to find when HouseName begin, in this way you can separate address.
This the expected result:
HouseNumber Street
8 Brick Lane
11 Balkerne Drive
I hope this help.
I am asssuming you want to split the number from the text, you can use
SELECT Left(Address1,CHARINDEX(' ',Address1,0)-1) as houseNumber,
Right(Address1,Len(Address1)-CHARINDEX(' ',Address1,0)) as houseStreet
From Table1