August 17, 2016 at 10:57 am
Say I have a table Products with a field price and a field PriceInChar:
price
12345678.90
priceInChar
I want to select the values from products but the price must match this format equivalent and
must update the field PriceInChar with the equivalent value in char:
0->A
1->B
2->C
3->D
4->E
5->F
6->G
7->H
8->I
9->J
THE CORRESPONDING FIELD updated in priceInChar
price
12345678.90
priceInChar
BCDEFGHI.JA
Thank you
August 17, 2016 at 11:05 am
SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
CAST(price AS varchar(20)),
'0', 'A'),
'1', 'B'),
'2', 'C'),
--...
'8', 'I'),
'9', 'J') AS priceInChar
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
August 17, 2016 at 11:43 am
A couple other ways:
1. Use NGrams8K[/url] like so:
DECLARE @price decimal(12,2) = 12345678.90;
SELECT NewPrice =
(
SELECT CASE WHEN token LIKE '[0-9]' THEN CHAR(ASCII(token)+17) ELSE token END
FROM dbo.NGrams8k(@price, 1)
FOR XML PATH('')
);
2. Use Translate8K[/url] like so:
SELECT dbo.Translate8K(@price, '0123456789', 'ABCDEFGHIJ');
-- Itzik Ben-Gan 2001
August 17, 2016 at 2:15 pm
Just an opinion... you just about can't beat a Nested Replace, like what Scott wrote, for something like this. I say "just about" only because I don't know of something faster but I suppose there could be.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 17, 2016 at 2:21 pm
Jeff Moden (8/17/2016)
Just an opinion... you just about can't beat a Nested Replace, like what Scott wrote, for something like this. I say "just about" only because I don't know of something faster but I suppose there could be.
+1
I intended to mention that but carried away 😛
-- Itzik Ben-Gan 2001
August 17, 2016 at 2:35 pm
Alan.B (8/17/2016)
Jeff Moden (8/17/2016)
Just an opinion... you just about can't beat a Nested Replace, like what Scott wrote, for something like this. I say "just about" only because I don't know of something faster but I suppose there could be.+1
I intended to mention that but carried away 😛
The NGRAMS tool you wrote is very cool. Don't blame you a bit. 🙂
--Jeff Moden
Change is inevitable... Change for the better is not.
August 17, 2016 at 2:52 pm
if you don't mind me asking, I really do wonder, what possible reason could you have for wanting to recode your numbers as letters o_O? Some sort of archaic way of hiding prices in plain sight?
August 18, 2016 at 4:57 am
Thank you very much, it works with some arrangements, but it works perfectly.
August 18, 2016 at 4:58 am
Yes it's true:-)
August 18, 2016 at 11:32 am
The NGRAMS tool you wrote is very cool. Don't blame you a bit. 🙂
😀
-- Itzik Ben-Gan 2001
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply