-1

i have a table which contains columns

[id],[StartsWith],[Length] ,[MinExt] ,[MaxExt],[isDID] ,[DeleteDigits] ,[AppendDigits]

208 ,     61     ,    5    ,  61000  ,  61999 ,   0    ,       2       ,   22058

209 ,     63     ,    5    ,  63000  ,  63999 ,   0    ,       2       ,  26518

now let suppose if user sends a number say 61205 i have to check that in which row that number(i.e 61205) exist by checking between MinExt and MaxExt

after that i have to truncate x digits from 61205 mentioned in corresponding deleteDigits from the start of the number (in this case 2 digits will be truncated) after truncate i have to append 205 in last of [appenddigits] column. which will make a complete number like 22058205.

i have to do this through select statement as it will be an inner query. or if anyone can suggest something else i will be very thankful.

waqar ahmed somra
  • 141
  • 1
  • 5
  • 16

2 Answers2

1

Try this:

DECLARE @VAL NVARCHAR(100) = '61205'

SELECT AppendDigits + SUBSTRING(@VAL, 3, LEN(@VAL) - 2)
FROM   YOUR_TABLE
WHERE  CAST(@VAL AS int) BETWEEN MinExt AND MaxExt
McNets
  • 10,352
  • 3
  • 32
  • 61
0

If the lookup table mentioned in your question has few rows, you could use a multi-level Case Statement to do this. Although it would look cumbersome, it will do the job perfectly without dependence on other code.

If this table is likely to have many rows, you may want to use a SQL Server User-Defined function.

Whirl Mind
  • 884
  • 1
  • 9
  • 18
  • lookup table has many rows more than 4000. and for userdefined function means i have to call function for every number(if user is sending more than one number)? – waqar ahmed somra Apr 12 '17 at 04:52
  • Thanks for your advice i created a stored procedure and then implemented the lopic. thanks again – waqar ahmed somra Apr 12 '17 at 06:16
  • Ok. Just saying for future needs of similar nature, the use of a user-defined function (in-line functions) will help you to still use it in select query or an inner query. Have a good day. – Whirl Mind Apr 14 '17 at 17:03