## Numerical conversion

 Author Message Bollivier Forum Newbie Group: General Forum Members Points: 9 Visits: 7 I need to take numerical data from a CSV file and format it as follows:Value in File - Converted Value3 003025.75 0258 12 0120125.8 1258The converted value must contain 4 numbers, rounded to the nearest 10th, numbers only, no decimal points. I am using SQL Tools on SQL Server 2012 to import the data into a table in the SQL Server, then using t-sql in a stored procedure to convert the data and export the file as a CSV for use in another system. I am having a very difficult time getting the leading or ending zeros as needed, along with rounding to the nearest 10th. Thank you in advance for any help you can provide. HappyGeek SSCertifiable Group: General Forum Members Points: 6915 Visits: 4503 Not sure you are going to achieve that without changing the data type to a character string, having said that something along the lines of the following may help:`DECLARE @Num decimal (6,2) = 3.55SELECT LEFT('0000', 4 - LEN(CAST((@NUM * 10) as INT))) + CAST ((CAST(ROUND((@NUM * 10), 0) as INT)) as varchar(4)) ConvVal` ... Thom A SSC-Forever Group: General Forum Members Points: 46143 Visits: 15669 Slightly different approach. This also won't fall over if a number is greater than 999:`WITH VTE AS ( SELECT * FROM (VALUES(3),(25.75),(12),(125.8),(1000),(1857.5)) V(I))SELECT I, RIGHT('0000' + CONVERT(varchar(8),CONVERT(int,ROUND(I*10,0))),4) AS LeadingZerosFROM VTE;` Thom~Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does :-PPlease always remember to encapsulate your code in IFCode Markup. For example [code=sql] [/code].Click here to read Jeffs Guide on how to post SQL questions, and get swift and helpful answers from the community Luis Cazares SSC Guru Group: General Forum Members Points: 96260 Visits: 21206 A shorter version:`WITH VTE AS ( SELECT * FROM (VALUES(3),(25.75),(12),(125.8),(1000),(1857.5)) V(I))SELECT I, RIGHT( 10000 + CAST(ROUND(I*10, 0) AS int), 4) AS LeadingZerosFROM VTE;` Luis C.General Disclaimer:Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?How to post data/code on a forum to get the best help: Option 1 / Option 2 Jack Corbett SSC Guru Group: General Forum Members Points: 104429 Visits: 15047 This is an even simpler option:`WITH VTE AS ( SELECT * FROM ( VALUES (3), (25.75), (12), (125.8), (1000), (1857.5) ) V (I) )SELECT VTE.I, FORMAT(I*10, '0000') AS LeadingZeros FROM VTE;` Jack Corbett Consultant Straight Path Solutions Dont let the good be the enemy of the best. -- Paul FlemingAt best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at workCheck out these links on how to get faster and more accurate answers: Forum Etiquette: How to post data/code on a forum to get the best helpNeed an Answer? Actually, No ... You Need a QuestionHow to Post Performance ProblemsCrosstabs and Pivots or How to turn rows into columns Part 1Crosstabs and Pivots or How to turn rows into columns Part 2 Luis Cazares SSC Guru Group: General Forum Members Points: 96260 Visits: 21206 +x Jack Corbett - Friday, December 29, 2017 8:47 AMThis is an even simpler option:`WITH VTE AS ( SELECT * FROM ( VALUES (3), (25.75), (12), (125.8), (1000), (1857.5) ) V (I) )SELECT VTE.I, FORMAT(I*10, '0000') AS LeadingZeros FROM VTE;`I usually avoid FORMAT due to its fame of being slow. Luis C.General Disclaimer:Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?How to post data/code on a forum to get the best help: Option 1 / Option 2 Bollivier Forum Newbie Group: General Forum Members Points: 9 Visits: 7 +xHappyGeek - Friday, December 29, 2017 1:18 AMNot sure you are going to achieve that without changing the data type to a character string, having said that something along the lines of the following may help:`DECLARE @Num decimal (6,2) = 3.55SELECT LEFT('0000', 4 - LEN(CAST((@NUM * 10) as INT))) + CAST ((CAST(ROUND((@NUM * 10), 0) as INT)) as varchar(4)) ConvVal`Thank you, HappyGeek! this worked perfectly. Have a Happy New Year. Sue_H SSC-Dedicated Group: General Forum Members Points: 33546 Visits: 9539 +xLuis Cazares - Friday, December 29, 2017 8:56 AM+x Jack Corbett - Friday, December 29, 2017 8:47 AMThis is an even simpler option:`WITH VTE AS ( SELECT * FROM ( VALUES (3), (25.75), (12), (125.8), (1000), (1857.5) ) V (I) )SELECT VTE.I, FORMAT(I*10, '0000') AS LeadingZeros FROM VTE;`I usually avoid FORMAT due to its fame of being slow.I was just comparing the two for that very reason....format version is much slower on execution times as I thought it would be. Quite noticeable with a cold cache even with a small set of data. Keep thinking maybe there is some scenario where it is more useful, isn't so slow but haven't found one yet. Sue Thom A SSC-Forever Group: General Forum Members Points: 46143 Visits: 15669 +xSue_H - Friday, December 29, 2017 4:30 PM+xLuis Cazares - Friday, December 29, 2017 8:56 AM+x Jack Corbett - Friday, December 29, 2017 8:47 AMThis is an even simpler option:`WITH VTE AS ( SELECT * FROM ( VALUES (3), (25.75), (12), (125.8), (1000), (1857.5) ) V (I) )SELECT VTE.I, FORMAT(I*10, '0000') AS LeadingZeros FROM VTE;`I usually avoid FORMAT due to its fame of being slow.I was just comparing the two for that very reason....format version is much slower on execution times as I thought it would be. Quite noticeable with a cold cache even with a small set of data. Keep thinking maybe there is some scenario where it is more useful, isn't so slow but haven't found one yet. SueAgreed, I remember about 4 years ago when I got access to FORMAT on 2012, and I was really excited. Started using it, and noticed a significant performance decrease. Couldn't see at all as to why; then someone (here) posted about FORMAT and it's awful performance; i changed the FORMAT back to CONVERT and they query runs beautifully again.A real shame, as FORMAT is so much easier on the eyes; something that starts with REPLACE(LEFT(CONVERT(varchar(15), and ends with something like ,121),7),'-','') just isn't as easily understood by some. Thom~Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does :-PPlease always remember to encapsulate your code in IFCode Markup. For example [code=sql] [/code].Click here to read Jeffs Guide on how to post SQL questions, and get swift and helpful answers from the community