I was working on an SQL server tool and got stuck in a place where I had to split the the numbers into two parts, first should be before the decimal part and second should be after the decimal part. In my earlier articles, I already wrote how to get the numbers before the decimal. In this article, I will share, how to get the numbers after the decimals.
Let me create a sample, before proceeding with the solution.
Sample :
USE Tempdb GO --DROP TABLE tbl_sample --GO CREATE TABLE tbl_sample ( [Col_ID] INT, [Col_Decimal] decimal(18,4) ) GO INSERT INTO tbl_sample VALUES (1,12345.9876) INSERT INTO tbl_sample VALUES (2,-12345.9876) INSERT INTO tbl_sample VALUES (3,123.45) INSERT INTO tbl_sample VALUES (4,12.90) GO
Given below are the solutions.
Solution 1 : (Without any function)
USE Tempdb GO SELECT [Col_ID], [Col_Decimal] , ([Col_Decimal]%1) As [Col_After_decimal] FROM tbl_sample GO --OUTPUT
Solution 2 : (Using CAST & ABS function)
USE Tempdb GO SELECT [Col_ID], [Col_Decimal] , ABS([Col_Decimal]) - CAST(ABS([Col_Decimal]) AS INT) As [Col_After_decimal] FROM tbl_sample GO --OUPUT
hi Imran.
I have developed my project in 2008 sql server. but production server have 2005 sql server. so the procedure and some other functions not working in that database and also not allowed database restoring how to i solved this problem…
By
Arun
Hi Arun,
Basically, It is not a good practice to develop a project in an upgraded version of SQL Server and deployed in an earlier versions. And you also used the upgraded stuff in your functions and procedures due this it is not working. I have a recommendation that you need to upgrade your production server.
Alternatively, You need modify your scripts in each function and procedures to make it compatible with SQL Server 2005.
Thanks
Imran