November 7, 2008 at 10:09 am
Hi all,
Relative newbie here in the world of SQL.
How can I do a select on a text as well as an integer column and narrow it down to specific lengths for the return?
i.e. I want to see everything with more than or less than 12 characters?
Thanks much!
Wally
[font="Verdana"]-- Wally
-- Reluctant DBA[/font]
November 7, 2008 at 10:56 am
When you say text, do you really mean a text datatype or is it a varchar/char data type?
November 7, 2008 at 11:01 am
Sorry,
varchar(50)
[font="Verdana"]-- Wally
-- Reluctant DBA[/font]
November 7, 2008 at 11:11 am
Wally
Are you sure the data type for the column is TEXT, or is it VARCHAR, or CHAR? I presume that you are not checking for more or less than x number of digits in the column whose data type is INT, or am I wrong?
If the column has a data type of TEXT or NTEXT I suggest you read up on the DATALENGTH function in Books on Line (BOL) Then you could compose your select statement somewhat like
SELECT X, SomeStuff FROM dbo.tableAA
WHERE DATALENGTH(SomeStuff) > 12
or for VARCHAR you might use something like
SELECT SomeStuff, LEN(Somestuff) AS 'Len someStuff' FROM Dbo.TableAA WHERE LEN(Somestuff) > 12
November 7, 2008 at 11:16 am
In that case, use LEN:
DECLARE @Table TABLE (Col1 int, Col2 varchar(50))
INSERT INTO @Table
SELECT 1, '123234345' UNION ALL
SELECT 2, '123456789124578'
SELECT *
FROM @Table
WHERE LEN(Col2) < 12
November 7, 2008 at 11:29 am
John Rowan (11/7/2008)
In that case, use LEN:
DECLARE @Table TABLE (Col1 int, Col2 varchar(50))
INSERT INTO @Table
SELECT 1, '123234345' UNION ALL
SELECT 2, '123456789124578'
SELECT *
FROM @Table
WHERE LEN(Col2) < 12
Perfect!
Thanks!
Wally
[font="Verdana"]-- Wally
-- Reluctant DBA[/font]
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply