Domanda

I have a numeric field like 1,3065 and I need that to became like this: 000000000000000130.

I mean 16 integers and 2 decimals, without the comma, adding 0es to the left and, if needed, to the right.

Is there any way to do that with a query?

È stato utile?

Soluzione

I think this will work in Sybase:

select right(replicate('0', 16) + cast(cast(field*100 as int) as varchar(255)), 16)

Altri suggerimenti

Try this way:

select REPLICATE('0',16-len(cast(cast((1.3065*100) as int) as varchar(16))))+cast(cast((1.3065*100) as int) as varchar(16))

Result of above select:

0000000000000130

If you want approximation you should use FLOOR or CEILING function as below

declare @multiplier int

select @multiplier = 1000

select REPLICATE('0',16-len(cast((floor(1.3065*@multiplier)) as varchar(16))))+cast(floor(1.3065*@multiplier) as varchar(16))

Result of above select:

0000000000001306

You can change an approximation by changing @multiplier. For example if you want result from 1st query you should change @multiplier to 100.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top