How do I extract the last 4 digits of numerical value? - sql

How can I extract the last for digits from a number in Microsoft SQL Server? For example '6789' from a number like '123456789'.
I just want to keep the last 4 numbers.

If you are starting with a number, you are asking for the modulo operator. This generally has one of two syntaxes. SQL Server uses:
select col % 10000
An alternative syntax in other databases is:
select mod(col, 10000)

You can use right function, example for you:
select right('123456789',4)

One more approach using SUBSTRING.
DECLARE #table table(a varchar(30))
insert into #table
values ('12345678')
SELECT SUBSTRING(a,len(a) - 3, 4) from #table
5678

Related

How to round multiple numeric columns to 2 digits in sql?

Say if I need to round multiple numbers to 2 digits now, but I don't want to repeat using round(..., 2) or format(...).
Is there any method to set up the float numbers with 2 digits globally?
select cast(float_column as decimal(10,2))
from your_table
Declare the column or variable of type numeric(18, 2)
You can also use with the CONVERT function
select CONVERT(numeric(18, 2) , 5.54722)

CAST - Make string length to be 2 characters long

I need to combine two fields but force the characters of the second string to be 2 characters.
I'm combining a year field and month field and want the result to be YYYY_MM. Forcing any single months (e.g. 1,2,3,4) into a two digit format e.g. (01).
Below is my formula for combining the fields, but I need help making the month two digits.
Thanks, L
WITH so_header(soh_build_year,soh_build_week) AS (
SELECT 2020, 3
UNION ALL SELECT 2020,13
)
SELECT
CAST(SO_HEADER.SOH_Build_Year AS VARCHAR)
+'_'
+CAST(SO_HEADER.SOH_Build_Week AS VARCHAR) as [Build YYYY_WW]
FROM so_header;
Try this out (Syntax: SQL Server)
SELECT
CAST(2019 AS VARCHAR)
+'_'
+CAST(format (1, '0#') AS VARCHAR) as [Build YYYY_WW]
Replace your values with your variables
Try this:
WITH so_header(soh_build_year,soh_build_week) AS (
SELECT 2020, 3
UNION ALL SELECT 2020,13
)
SELECT
CAST(SO_HEADER.SOH_Build_Year AS VARCHAR)
+ '_'
+ SUBSTR(
CAST(100+SO_HEADER.SOH_Build_Week AS VARCHAR)
, 2
, 2
) as Build_YYYY_WW
FROM so_header;
-- out Build_YYYY_WW
-- out ---------------
-- out 2020_03
-- out 2020_13
If you are using SQL Server never use varchar (or related types) with no length. The default varies by context and may not be large enough for what you want.
If you are trying to convert a date to YYYY_MM format, you can use format():
select format(getdate(), 'yyyy_MM')
I recommend using dates, if they are available. If you are not using SQL Server, most other databases have similar functionality.
If not, you an simply use:
select concat(so_header.SOH_Build_Year, '_'
right(concat('00', so_header.soh_build_week), 2)
)
concat() does not require explicitly converting the values to strings.

control in display Decimal separators in sql server

I have problem in display the decimal numbers, it has many number.
My sql statement :
Select sum(HS$totalMoney)
the result :
12132.123444343
I want to display as 12132.12 without the another number
Thanks.
If your logic is for money you should first round the values not truncate
select CONVERT(decimal(18,2),round(12132.123444343 ,2)) gives 12132.12
select CONVERT(decimal(18,2),round(12132.125944343 ,2)) gives 12132.13
Try this one -
SELECT CAST(12132.123444343 AS DECIMAL(10,2))
if you are using mysql, use code blew
SELECT TRUNCATE(sum(HS$totalMoney), 2);
this query slove your problem
SELECT CAST(12132.123444343 AS DECIMAL(10,2))
or you can use
select CONVERT(decimal(18,2),round(12132.1255555 ,2))
SELECT CONVERT(decimal(21, 2), sum(HS$totalMoney))
-- This one will round in SQL Server but truncate in ASE 15 (which was available to me at the time)
SELECT CONVERT(decimal(21, 2), round(sum(HS$totalMoney), 2, 1))
-- This one uses a variant of ROUND supported by SQL Server, but not ASE 15 (and will truncate the third and subsequent decimal places).
The round function has a function parameter to truncate instead of round:
select round(12132.123444343 , 2, 1)
From here:
http://msdn.microsoft.com/en-us/library/ms175003.aspx

Problem with max() in sql server

I have alphanumeric values like. XYZ1,XYZ2......XYZ11, XYZ12 and so on, now I want to select only the Max numeric value, i.e. 12 here.
I tried-
select max(REPLACE(ID,'XYZ','')) from myTable;
but this is returning 9. why?
Try converting to INT before max
select max(cast(REPLACE(ID,'XYZ','') as int)) from myTable;
It's still treating your value as a string instead of a number. Try:
select max(CAST(REPLACE(ID,'XYZ','') AS INT) from myTable;
Because you're still comparing strings. The fact that they contain only numeric digits doesn't mean that they're not strings. You need to convert them:
SELECT MAX(CAST(REPLACE(id, 'XYZ', '') AS INT)) FROM My_Table
Another method is
select max(REPLACE(ID,'XYZ','')*1) from myTable

How to give select digit after decimal point?

In database there is column amount which datatype is money. I want to select that row only with two digit after decimal. for this how to write the query?
My query is like this:
SELECT AMOUNT FROM DETAIL_PAGE.
I want to modify this query so that it selects two digits after decimal point.
SELECT AMOUNT - FLOOR(AMOUNT) FROM DETAIL_PAGE
That will get you just the decimal though. I think you want
SELECT FORMAT(AMOUNT, 2) FROM DETAIL_PAGE
Or without commas:
SELECT REPLACE(FORMAT(AMOUNT, 2), ',', '') FROM DETAIL_PAGE
Not sure if this is SQL standard and works elsewhere, but in Oracle you can say
select round(amount,2) from detail_page
-- round(12.345, 2) would return 12.35
or
select trunc(amount,2) from detail_page
-- trunc(12.345, 2) would return 12.34