Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 832
Cabaas Cabdi Avatar asked May 31 '12 06:05

Cabaas Cabdi


People also ask

How do I find the first digit of a number in SQL?

SQL Server LEFT() Function The LEFT() function extracts a number of characters from a string (starting from left).

How do you SELECT digits in SQL?

SELECT DISTINCT SUBSTR(DIGITS(INTCOL),1,4) FROM TABLEX; Example 2: Assume that COLUMNX has the data type DECIMAL(6,2), and that one of its values is -6.28. For this value, the following statement returns the value '000628'.


2 Answers

if CallingParty is of type int:

SELECT CAST(LEFT(CallingParty, 3) AS INT)
From CDR
like image 169
Habib Avatar answered Nov 11 '22 15:11

Habib


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

Select left(cast(267672668788 as varchar), 3)
like image 40
dbrosier Avatar answered Nov 11 '22 16:11

dbrosier