Click here to Skip to main content
15,917,964 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have column SL nvarchar(5) from tablename

SL SL
1/2 1/2
3 --> 03
23 23
5 05

select
JavaScript
(replace(left(REPLACE(STR(sl,3),' ','0'),1),'0','')+
			right(REPLACE(STR(sl,3),' ','0'),2)) as slmax

from tablename

What I have tried:

but didn't work ,i can't solve 1/2 with SL(nvarchar). helps me
Posted
Updated 8-Aug-16 22:05pm
Comments
Kornfeld Eliyahu Peter 9-Aug-16 3:51am    
Not clear what you are expecting...
First of all it seems that you are working with strings (as 1/2 is number only for you, but for SQL it is a string)...
You should edit the question and show clearly what output you expect on what input...
Member 12676820 9-Aug-16 4:26am    
(replace(left(REPLACE(STR(sl,3),' ','0'),1),'0','')+right(REPLACE(STR(sl,3),' ','0'),2)) as slmax -> it's ok type number, but with "1/2" nvarchar-> error
when i use type SL(nvarchar) but result can't converting data type nvarchar to float.

1 solution

Don't do this in SQL: do it in your presentation software where it's a lot simpler as you will have much better string handling and data conversion facilities.
In C# for example, it's pretty trivial: Try to convert the string to an integer: if it fails, leave it alone. If it succeeds, convert that to a string with at least two digits:
C#
int x;
string result = int.TryParse(input, out x) ? x.ToString("D2") : input;

This is a presentation function, not a data storage function, and should be handled as such.
Just because it could be done in SQL doesn't mean it should!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900