Hi everyone thanks for taking some time to look into my question,
Background
I'm using the ROW_NUMER() function along with a PARTITION BY..ORDER BY statement to set a varchar value with an incrementing int value at the end of it for each of a value in one of my tables.
SELECT component_module_id as component_module_id,
component_property_id,
'cf' + CAST(ROW_NUMBER() OVER (PARTITION BY component_module_id ORDER BY component_module_id) as VARCHAR) AS cf
FROM component_property
Here the 'cf' value goes from cf1-cfX for each component_module_id
My Question
Whenever I attempt to use these cf values elsewhere, like saved in a temp table, other Ordering and Grouping statements change these values. It's like the statements to generate the 'cf' values are saved and not the 'cf' values themselves.
After inserting the query above into a temp table #t -
SELECT * FROM #t ORDER BY cf
I receive 'cf' values that start at cf1 and jump to cf10 and then cf100, with a range of cf values from cf1 to cf900... I should only be receiving values ranging from c1 to cf29.
My question here is - Why are the values in this column treated differently than any other normal value? Why is the ROW_NUMBER() OVER (PARTITION BY....)) calculation being passed to further queries down the line? (if that's actually what's happening). And finally, how I can treat these 'cf' values like normal VARCHAR values and not have them change on me whenever I try to group or order by them.
Thanks for any help!
Update
I took the suggestion from Larnu,
"Seems like you'd be better off just storing the int value, and using a (PERSISTED) computed column to concatenate your prefix and ROW_NUMBER value."
and my 'cf' values are now appearing correctly after being sorted. Thanks everyone marking as solved.