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 |
poolmwv
Starting Member
12 Posts |
Posted - 2008-01-23 : 15:44:23
|
I'm sorry to be so dumb that I can't extrapolate from the conditional insert/update, but I can't find anything quite what I'm looking for.We recently decided to change our naming standard, so I wrote two scripts that would quickly change the name in the database:If the Serial Number was the name, I ran this:UPDATE ASSETSET NAME=ASSET.SERIAL_NUMBER + "." + ASSET_TYPES.TYPEFROM ASSET JOIN ASSET_TYPES ON ASSET.ID_TYPE = ASSET_TYPES.IDWHERE ASSET.SERIAL_NUMBER=ASSET.NAMEThis would result in something like 1234.PCIf the Termid was the name, I ran this:UPDATE ASSETSET NAME=ASSET.SERIAL_NUMBER + "." + ASSET_TYPES.TYPE + "." + ASSET.NAMEFROM ASSET JOIN ASSET_TYPES ON ASSET.ID_TYPE = ASSET_TYPES.IDWHERE ASSET.SERIAL_NUMBER<>ASSET.NAMEThis would result in something like 5678.PRINTER.ABCDNow I would like to change the job that runs daily to do this. This is an abbreviated version:INSERT INTO ASSET(ASSET.MODEL, ASSET.MFG, ASSET.SN, ASSET.TYPE, ASSET.NAME)SELECT AU.Model, AU.MFG, AU.Serial_Number, AU.Type, AU.NameFROM Asset_Update as AUI could just put AU.Serial_Number + "." + AU.Type + "." + AU.Termid instead of AU.Name, but that would leave a period at the end and they don't want that. So if AU has a termid, I would like ASSET.NAME to be:AU.Serial_Number + "." + AU.Type + "." + AU.TermidIf AU has a null termid, I would like ASSET.NAME to be:AU.Serial_Number + "." + AU.TypeThank you for your assistance.I'm not an idiot, but I play one on the net. |
|
jdaman
Constraint Violating Yak Guru
354 Posts |
Posted - 2008-01-23 : 16:05:43
|
AU.Serial_Number + '.' + AU.Type + COALESCE('.'+AU.Termid, '') |
 |
|
poolmwv
Starting Member
12 Posts |
Posted - 2008-01-23 : 16:11:51
|
quote: Originally posted by jdaman AU.Serial_Number + '.' + AU.Type + COALESCE('.'+AU.Termid, '')
Aaargh! I should have thought of that! I had just been using the COALESCE for something else but I was trying to make it more complicated. Thank you!I'm not an idiot, but I play one on the net. |
 |
|
jdaman
Constraint Violating Yak Guru
354 Posts |
Posted - 2008-01-23 : 16:14:56
|
quote: Originally posted by poolmwv
quote: Originally posted by jdaman AU.Serial_Number + '.' + AU.Type + COALESCE('.'+AU.Termid, '')
Aaargh! I should have thought of that! I had just been using the COALESCE for something else but I was trying to make it more complicated. Thank you!I'm not an idiot, but I play one on the net.
It usually is more simple than we first think. Glad I could help. |
 |
|
|
|
|
|
|