Question

I have a situation where a decimal figure isn't truly a decimal, it is a sub-count of sorts. For example, when an prescription for medication is filled it is given an Rx number (lets say 345673). That number will stay with the prescription throughout any refills and the refills append a .1, .2 etc. So, over the life of that Rx number you could end up with 345673, 345673.1, 345673.2... ongoing. The problem is when you hit .10, .20, .30 etc. In decimal form those records are the same as .1, .2, .3.

Is there any way to track these numbers and support the trailing zeros without having use VARCHAR etc? This is the primary key column and I'm not crazy about using varchar on a Pk (is that old fashioned?)

Any and all suggestions/help is appreciated.

Edit to add: I should have explained why we can't use separate columns for this. This data originates elsewhere and is merged into our database from a bulk import operation. The merge is perpetual since much of the data is altered at the origin and combined on the next bulk import. The Rx number has to match exactly to perform the bulk import / merge.

Was it helpful?

Solution

I would recommend using a composite primary key. Add a second column, perhaps called refill, and use that for your incremental values. That way both columns would be integers and no need to use varchar for your primary key.

You may need to use a trigger to maintain the value as identity fields don't work with groupings.

OTHER TIPS

Convert the decimal to varchar using either CONVERT or CAST. Then you can use character/string functions such as LEFT(), SUBSTRING(), and REPLACE().

The other option is to parse out the decimal using math:

DECLARE @RxValue decimal(18,4) = 100.234
SELECT @RxValue [Rx Number]
    ,@RxValue - CONVERT(int,@RxValue) [Refill Count Decimal]
    ,LEN(CONVERT(float,(@RxValue - CONVERT(int,@RxValue))))-2 [After Decimal Length]
    ,POWER(10, LEN(@RxValue - CONVERT(int,@RxValue))-2) [Calc multiplier for Refill Count]
    ,CONVERT(int,@RxValue) [Rx ID]
    ,CONVERT(int,(@RxValue - CONVERT(int,@RxValue)) * POWER(10, LEN(CONVERT(float,(@RxValue - CONVERT(int,@RxValue))))-2)) [Refill Count]

The last two columns in the above select are your two desired columns. All that is above is my attempt to "show my work" so you can see what I was thinking. The "-2" is to to remove the "0." out of the LEN() function's result. The POWER() function takes the 10 and raises it to the power of the decimal result.

You should only need to replace all references to @RxValue with the column name of your "Rx number" to make it work. If you want to play around with it, you can change the values in @RxValue to whatever you want and make sure the result is what you expect. Be sure you change @RxValue's data type to match yours.

You can, of course keep your "Rx number" as the PK if that is the situation it is in. You'll just be adding a couple of derived columns: [Rx ID] and [Refill Count].

Please leave a comment if you have a question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top