How I can get the first 3 digits in 123456 Numbers in sql?

88,475

Solution 1

if CallingParty is of type int:

SELECT CAST(LEFT(CallingParty, 3) AS INT)
From CDR

Solution 2

SQL Server has a Left() function, but it works best on strings. (varchar/char in SQL)

Select left(cast(267672668788 as varchar), 3)

Solution 3

If the data length does not change then you can always divide by 10 * the digits you have

SELECT FLOOR(267672668788 / 1000000000)
=267

Solution 4

Use this query:

SELECT SUBSTRING(CAST(CallingParty AS VARCHAR(50)), 1, 3) FROM [CDR]
Share:
88,475
Admin
Author by

Admin

Updated on July 09, 2022

Comments

  • Admin
    Admin almost 2 years

    I have field called CallingParty in My CDR table it contains data like this:

    CallingParty
    ------------
    267672668788
    

    I want to select the first 3 number of each of those numbers like

    CallingParty
    ------------
    267