Yamix Posted February 28, 2020 at 05:29 PM Report Share #617450 Posted February 28, 2020 at 05:29 PM (edited) Boa tarde, Vocês conhecem alguma maneira de filtrar números entre um intervalo em uma coluna do tipo alfanumérico (varchar)? SELECT Campo FROM Tabela WHERE isNumeric(Campo) = 0 OR (Campo >= 1 AND Caixa <= 2) Tentei fazer desta forma, apelando para o uso do curto circuito. Tentei usando a cláusula case..when..then, mas também não funciona como o esperado, então deixei assim mesmo por fins de facilidade de leitura. + Resumo do problema: * Se o campo caixa for string, ignora e deixa passar; * Se o campo for número, faz o filtro daqueles números que esteja entre o intervalo; Edited February 28, 2020 at 09:01 PM by Yamix Link to comment Share on other sites More sharing options...
Solution Rechousa Posted February 28, 2020 at 11:44 PM Solution Report Share #617454 Posted February 28, 2020 at 11:44 PM Boa noite, Nas versões mais recentes de SQL Server podes utilizar a função TRY_PARSE. Código exemplo: USE [tempdb]; GO IF OBJECT_ID (N'dbo.Tabela', N'U') IS NOT NULL BEGIN DROP TABLE [dbo].[Tabela]; END CREATE TABLE [dbo].[Tabela] ( Id INT IDENTITY (1, 1), Campo VARCHAR(20) ); INSERT INTO [dbo].[Tabela] VALUES (NULL), ('1'), ('a'), (' '), ('' ), ('3'), ('12'), (' 2 '), ('.'), ('-'), (' 0 1 '), ('0'), ('-1'); SELECT Id, Campo FROM [dbo].[Tabela] WHERE ISNULL(TRY_PARSE(Campo AS INT), 1) BETWEEN 1 AND 2 SQLFiddle: http://sqlfiddle.com/#!18/66feb/3/0 1 Report Pedro Martins Sharing is Knowledge! http://www.linkedin.com/in/rechousa Link to comment Share on other sites More sharing options...
M6 Posted March 2, 2020 at 08:54 AM Report Share #617460 Posted March 2, 2020 at 08:54 AM Se tiveres garantia de que são sempre números, podes fazer um cast. 10 REM Generation 48K! 20 INPUT "URL:", A$ 30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50 40 PRINT "404 Not Found" 50 PRINT "./M6 @ Portugal a Programar." Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now