SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Select not empty on data type=text fields
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Razzle00
Starting Member

35 Posts

Posted - 03/09/2007 :  14:55:22  Show Profile  Reply with Quote
Hi,

How do you create a select statement based on if a field that is of data type 'Text' is empty or not. I have a table with a text field that holds large amounts of text. I need pull only the records where the text data type field is NOT empty.

Select * from Table1 where Mytextfld IS NOT EMPTY
Select * from Table1 where Mytextfld != ''
Select * from Table1 where LEN(Mytextfld)>'0'

Please help.

Thanks,

Razzle

snSQL
Flowing Fount of Yak Knowledge

USA
1837 Posts

Posted - 03/09/2007 :  15:04:16  Show Profile  Reply with Quote
Select * from Table1 where Mytextfld IS NOT NULL


Also, when you use numbers in SQL Server, don't enclose them in quotes, and use the datalength function for the text type, so one of your other queries should be

Select * from Table1 where datalength(Mytextfld) > 0
Go to Top of Page

Razzle00
Starting Member

35 Posts

Posted - 03/12/2007 :  11:17:49  Show Profile  Reply with Quote
Thanks snSQL,

datalength(Mytextfld) > 0, is working for my query. One other question though. If the I have field which data type is text, and it does not allow nulls and the value is an empty string like '', is there a function in SQL like Is Null that work for string empty? In VB.net I can check a string variable like this...

Dim MyString as String = ""
If MyString.equals(string.empty)
msgbox("String is Empty")
else
msgbox("String is NOT Empty")
End If

Razzle
Go to Top of Page

snSQL
Flowing Fount of Yak Knowledge

USA
1837 Posts

Posted - 03/12/2007 :  11:53:53  Show Profile  Reply with Quote
Well that would be datalength(MyString) = 0 right?
Go to Top of Page

Razzle00
Starting Member

35 Posts

Posted - 03/13/2007 :  13:44:05  Show Profile  Reply with Quote
LOL, I guess that would be the solution. So basically the datalength() function is the best way to handle empty/not empty strings in SQL server. I just thought since you could use IS NULL/IS NOT NULL for null values there might be a function for empty/not empty for string values.

Thanks for the help,

Razzle
Go to Top of Page

Razzle00
Starting Member

35 Posts

Posted - 03/13/2007 :  13:51:52  Show Profile  Reply with Quote
Just to further explain the madness to my question. I come from a Visual FoxPro background where it was a standard practice to evaluate empty strings in a database using an Empty() function.

Example
The following example opens the customer table in the testdata database. FOR ... ENDFOR is used to create a loop in which EMPTY( ) s used to determine if TAG( ) returns the empty string. The name of each structural index tag is displayed with its candidate status
.
Example
The following example opens the customer table in the testdata database. FOR ... ENDFOR is used to create a loop in which EMPTY( ) s used to determine if TAG( ) returns the empty string. The name of each structural index tag is displayed with its candidate status
.


CLOSE DATABASES
OPEN DATABASE (HOME(2) + 'data\testdata')
USE customer     && Open customer table

FOR nCount = 1 TO 254
   IF !EMPTY(TAG(nCount))  && Checks for empty string
   ? TAG(nCount)  && Display tag name
   ? CANDIDATE(nCount)  && Display candidate status
   ELSE
      EXIT  && Exit the loop when no more tags are found
   ENDIF
ENDFOR

Go to Top of Page

snSQL
Flowing Fount of Yak Knowledge

USA
1837 Posts

Posted - 03/13/2007 :  13:56:07  Show Profile  Reply with Quote
Correct, there isn't a specific keyword, you could of course also just use an empty string with two single quotes and nothing between them.
Select * from Table1 where Mytextfld <> ''

Edited by - snSQL on 03/13/2007 13:57:04
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000