Home > OS >  How to generate proper SQL request
How to generate proper SQL request

Time:01-20

I have a database where ID is int value 7 or 8 digits (2500859 or 15201234) long so i need to generate correct SQL request. I tried to

SELECT MAX(Id) FROM Table WHERE FORMAT(Id, '0#######') LIKE '25'

but it doesn't works. Could you please me generate correct request.

SELECT MAX(Id) FROM Table WHERE FORMAT(Id, '0#######') LIKE '25'

result is 0

CodePudding user response:

Thanks everyone. I've solve it by this: "SELECT MAX(CAST(RIGHT(FORMAT(Id, '0#######'),5) as INT)) FROM Column WHERE CAST(LEFT(FORMAT(Id, '0#######'),3) as INT) = Number"

This string returns int which I asked for.

CodePudding user response:

Postgresql:

select max(id) from kkkk where length(id::varchar) =7;
select max(id) from kkkk where length(id::varchar) =8;

    ------------ method 1
select * from kkkk where length(id::varchar) =7;
select * from kkkk where length(id::varchar) =8;

---------------method 2
select 
case when length(id::varchar) =7 then id end as "ids with 7 caracters",
case when length(id::varchar) =8 then id end as "ids with 8 caracters"
from kkkk

Demo

enter image description here

  • Related