The original code is from this website, but somehow, the page got lost so I cannot reference it. But here is the original code:
DECLARE #email_address VARCHAR(2000) = 'aname#acompany.com'
IF (
CHARINDEX(' ',LTRIM(RTRIM(#email_address))) = 0
AND LEFT(LTRIM(#email_address),1) <> '#'
AND RIGHT(RTRIM(#email_address),1) <> '.'
AND CHARINDEX('.',#email_address ,CHARINDEX('#',#email_address)) - CHARINDEX('#',#email_address ) > 1
AND LEN(LTRIM(RTRIM(#email_address ))) - LEN(REPLACE(LTRIM(RTRIM(#email_address)),'#','')) = 1
AND CHARINDEX('.',REVERSE(LTRIM(RTRIM(#email_address)))) >= 3
AND (CHARINDEX('.#',#email_address ) = 0 AND CHARINDEX('..',#email_address ) = 0)
)
print 'valid email address'
ELSE
print 'not valid'
From that, I was going to write a function I could call on to test formats. Figuring I would like to use this later to test for Twitter, and other social IDs, I figured I would make it a code that accepted the sting as well as the format to test.
Here is what I wrote:
CREATE FUNCTION [dbo].[TestForType](#strString VARCHAR(2000), #strFormat VARCHAR(2000))
RETURNS BIT
AS
BEGIN
--DECLARE #strString VARCHAR(2000)= 'aname#acompany.com'
DECLARE #email_address VARCHAR(2000)
--DECLARE #strFormat VARCHAR(2000) = 'email'
DECLARE #bitTrueFalse bit
SET #email_address =#strString
--SET #bitTrueFalse = If(#strFormat = 'email') BEGIN
If(#strFormat = 'email') BEGIN
IF (#email_address <> 'none#pletter.com'
AND CHARINDEX(' ',LTRIM(RTRIM(#email_address))) = 0
AND LEFT(LTRIM(#email_address),1) <> '#'
AND RIGHT(RTRIM(#email_address),1) <> '.'
AND CHARINDEX('.',#email_address ,CHARINDEX('#',#email_address)) - CHARINDEX('#',#email_address ) > 1
AND LEN(LTRIM(RTRIM(#email_address ))) - LEN(REPLACE(LTRIM(RTRIM(#email_address)),'#','')) = 1
AND CHARINDEX('.',REVERSE(LTRIM(RTRIM(#email_address)))) >= 3
AND (CHARINDEX('.#',#email_address ) = 0 AND CHARINDEX('..',#email_address ) = 0)
)
SET #bitTrueFalse = 1
ELSE
SET #bitTrueFalse = 0
END --END IF
ELSE
SET #bitTrueFalse = NULL
END --End IF
RETURN #bitTrueFalse
I got this error:
Msg 156, Level 15, State 1, Procedure TestForType, Line 41
Incorrect syntax near the keyword 'RETURN'.
I don't understand. I want it to return the value of #bitTrueFalse.
Your innermost IF doesn't have a BEGIN, so basically your END are unbalanced.
The END just before the RETURN is the one that belongs to the very first BEGIN (just after the AS) and therefore, your RETURN comes after the end of the function code. Put your RETURN before that very last END and you should be fine
Your code really looks like this:
CREATE FUNCTION [dbo].[TestForType]
(#strString VARCHAR(2000), #strFormat VARCHAR(2000))
RETURNS BIT
AS
BEGIN -- (1)
DECLARE #email_address VARCHAR(2000)
DECLARE #bitTrueFalse bit
SET #email_address = #strString
IF (#strFormat = 'email') BEGIN -- (2)
IF (#email_address <> 'none#pletter.com'
AND CHARINDEX(' ', LTRIM(RTRIM(#email_address))) = 0
.... all those other conditions ......
SET #bitTrueFalse = 1 -- no BEGIN here....
ELSE
SET #bitTrueFalse = 0
END -- END IF (2)
ELSE
SET #bitTrueFalse = NULL
END --- (1)
-- this ends up being *AFTER* the last END
RETURN #bitTrueFalse
Related
This is my code:
CREATE FUNCTION [dbo].[fnAppEmailCheck]
(#email VARCHAR(255))
--Returns true if the string is a valid email address.
RETURNS bit
AS
BEGIN
DECLARE #valid bit
IF #email IS NOT NULL
SET #email = LOWER(#email)
SET #valid = 0
IF #email LIKE '[a-z,0-9,_,+,-]%#[a-z,0-9,-]%.[a-z][a-z]%'
AND LEN(#email) = LEN(dbo.fnAppStripNonEmail(#email))
AND #email NOT like '%#%#%'
AND CHARINDEX('.#',#email) = 0
AND PATINDEX('%[a-zA-Z]%', #email) <> 0
AND CHARINDEX('..',#email) = 0
AND CHARINDEX(',',#email) = 0
AND RIGHT(#email,1) between 'a' AND 'z'
SET #valid = 1
RETURN #valid
END
My fnAppStripNonEmail function :
CREATE FUNCTION [dbo].[fnAppStripNonEmail]
(#Temp VarChar(1000))
RETURNS VarChar(1000)
AS
BEGIN
DECLARE #KeepValues AS varchar(50)
SET #KeepValues = '%[^a-z,^0-9,#,.,-]%'
WHILE PATINDEX(#KeepValues, #Temp) > 0
SET #Temp = STUFF(#Temp, PATINDEX(#KeepValues, #Temp), 1, '')
RETURN #Temp
END
In the screenshot, every format works fine as per my requirements but it flags 22#gmail.com as 1. I want my email ID to have at least one alphabet.
CREATE FUNCTION dbo.CheckValidEmail(#EMAIL varchar(100))RETURNS bit as
BEGIN
DECLARE #bitEmailVal as Bit
DECLARE #EmailText varchar(200)
SET #EmailText=ltrim(rtrim(isnull(#EMAIL,'')))
SET #bitEmailVal = case when #EmailText = '' then 0
when #EmailText like '% %' then 0
when #EmailText like ('%["(),:;<>\]%') then 0
when substring(#EmailText,charindex('#',#EmailText),len(#EmailText)) like ('%[!#$%&*+/=?^`_{|]%') then 0
when (left(#EmailText,1) like ('[-_.+]') or right(#EmailText,1) like ('[-_.+]')) then 0
when (#EmailText like '%[%' or #EmailText like '%]%') then 0
when #EmailText LIKE '%#%#%' then 0
when #EmailText NOT LIKE '_%#_%._%' then 0
else 1
end
RETURN #bitEmailVal
END
GO
Here is the problem, the system I use can create a saved search which basically generates a 'Where' clause and saves it in the database as type Image ..
I am trying to convert the image entry to a readable format, it is saved in 2 forms (Unicode and non unicode) now I can get the non unicode entries fine and they display correctly, however when I try and convert the Unicode entries it does not display correctly an example of what I am trying to convert is
0x01000000FFFEFF00FFFEFF0001050000FFFEFF05510075006F007400650006000000060000000100000001000000000000000000000080000000000000FD00000000000000000700000001050000FFFEFF254E006F0072006700720065006E005F00470072006F007500700020004900640020003D00200030007800300030003000300030003000300030003000300030003000300030003200330004000000060000000100000002000000000000000000002380000000000022B00000000000000001000000FFFEFF5454005400490044005F003400330034005200390032003300370020005F005F00530051004C005F00440045004C0049004D005F005F002000510075006F00740065005F002E004E006F0072006700720065006E005F00470072006F00750070005F004900640020003D00200054005400490044005F00340033003400520039003200330037002E004E006F0072006700720065006E005F00470072006F00750070005F00490064000000000001050000FFFEFF0C45006D0070006C006F0079006500650020003D0020003F0004000000060000000100000002000000000000000000000080000000000001030101000000000001000000FFFEFFA754005400490044005F00320035005200310032003400350034002E0054005400490044005F0032003300520038003600340052003100320034003500340020005F005F00530051004C005F00440045004C0049004D005F005F002000510075006F00740065005F002E0053006800690070005F0054006F005F0043006F006D00700061006E0079005F004900640020003D00200054005400490044005F00320035005200310032003400350034002E0043006F006D00700061006E0079005F0049006400200041004E004400200054005400490044005F00320035005200310032003400350034002E004100630063006F0075006E0074005F004D0061006E0061006700650072005F004900640020003D00200054005400490044005F003200330052003800360034005200310032003400350034002E0045006D0070006C006F007900650065005F00490064000000000001050000FFFEFF0C45006D0070006C006F0079006500650020003D0020003F0004000000060000000100000002000000000000000000000080000000000001030101000000000001000000FFFEFFB354005400490044005F00320035005200310032003400350034002E0054005400490044005F00320033005200380037003000390052003100320034003500340020005F005F00530051004C005F00440045004C0049004D005F005F002000510075006F00740065005F002E0053006800690070005F0054006F005F0043006F006D00700061006E0079005F004900640020003D00200054005400490044005F00320035005200310032003400350034002E0043006F006D00700061006E0079005F0049006400200041004E004400200054005400490044005F00320035005200310032003400350034002E0049006E007400650072006E0061006C005F0043006F006D006D00650072006300690061006C005F00530061006C00650073005F004900640020003D00200054005400490044005F0032003300520038003700300039005200310032003400350034002E0045006D0070006C006F007900650065005F00490064000000000001050000FFFEFF13510075006F0074006500200054006F00740061006C0020003E003D00200032003000300030000400000006000000010000000200000000000000000000008000000000001847010101080000000000000000409F400000000001000000FFFEFF15510075006F00740065005F0020005F005F00530051004C005F00440045004C0049004D005F005F0020000000000001050000FFFEFF1F46006F006C006C006F007700200055007000200053007400610074007500730020003D00200054006F00200046006F006C006C006F007700200055007000040000000600000001000000020000000000000000000000800000000000392F0000000000000001000000FFFEFF15510075006F00740065005F0020005F005F00530051004C005F00440045004C0049004D005F005F0020000200000001050000FFFEFF1F46006F006C006C006F007700200055007000200053007400610074007500730020003D00200049006E00200046006F006C006C006F007700200055007000050000000600000001000000020000000000000000000000800000000000392F0000000000000001000000FFFEFF15510075006F00740065005F0020005F005F00530051004C005F00440045004C0049004D005F005F0020000000000001050000FFFEFF1F46006F006C006C006F007700200055007000200053007400610074007500730020003D0020004E006F00200046006F006C006C006F007700200055007000050000000600000001000000020000000000000000000000800000000000392F0000000000000001000000FFFEFF15510075006F00740065005F0020005F005F00530051004C005F00440045004C0049004D005F005F0020000000000001050000FFFEFF20510075006F0074006500200046006F006C006C006F00770020004500780070006900720065007300200049006E002000440061007900730020003E002000310004000000060000000100000002000000000000000000000080000000000039DD0000000000000001000000FFFEFF4854005400490044005F003700310035004C003100340034003600330020005F005F00530051004C005F00440045004C0049004D005F005F002000510075006F00740065005F002E00510075006F00740065005F005F004900640020003D00200054005400490044005F003700310035004C00310034003400360033002E00510075006F00740065005F005F00490064000000000001050000FFFEFF11510075006F00740065005F0020004900640020004E006F007400200049006E00200004000000060000000100000002000000000000000000000080000000000018F5000000040000000100000000000000000000000000000001000000FFFEFF15510075006F00740065005F0020005F005F00530051004C005F00440045004C0049004D005F005F0020000000000003000000010000000000000000002F9680000000000018F5000000000000000000000000
the SQL code I am using to convert is the following
with saved_lookups_cte as
(select
table_id,
table_name,
saved_lookups_id,
saved_lookup_name,
case
when charindex('WhereDelim',query_text) > 0 then
substring(query_text,len(query_text) - charindex(reverse('WhereDelim'),reverse(query_text)) + 3,4000)
else query_text
end query_text
, sql_tree_binary
from (select
table_id,
table_name,
saved_lookups_id,
saved_lookup_name,
case isUnicode
when 1 then [Production_ED].dbo.[RemoveNonASCII] (cast(cast(substring(sql_tree_binary, startIndex, queryLength * 2) as nvarchar(max))as varchar(max)))
else cast(substring(sql_tree_binary, startIndex, queryLength) as varchar(8000))
end query_text
, sql_tree_binary
from (
select
saved_lookup_name,
case
when substring(sql_tree_binary,1,5) = 0x0100000000 then 0
when substring(sql_tree_binary,1,5) = 0x01000000FF then 1
end isUnicode,
cast(case
when substring(sql_tree_binary,1,5) = 0x0100000000 then
case
when substring(sql_tree_binary,6,1) = 0xFF then substring(sql_tree_binary,8,1) + substring(sql_tree_binary,7,1)
else substring(sql_tree_binary,6,1)
end
when substring(sql_tree_binary,1,5) = 0x01000000FF then
case
when substring(sql_tree_binary,12,1) = 0xFF then substring(sql_tree_binary,14,1) + substring(sql_tree_binary,13,1)
else substring(sql_tree_binary,12,1)
end
end as int) queryLength,
case
when substring(sql_tree_binary,1,5) = 0x0100000000 then
case
when substring(sql_tree_binary,6,1) = 0xFF then 9
else 7
end
when substring(sql_tree_binary,1,5) = 0x01000000FF then
case
when substring(sql_tree_binary,12,1) = 0xFF then 15
else 13
end
end startIndex,
s.table_id,
t.table_name,
s.saved_lookups_id,
s.sql_tree_binary
from
Production_Ed.dbo.Saved_Lookups s inner join
tables t on s.table_id = t.tables_id
-- where substring(saved_lookups_id,1,1) <> 0x00
where saved_lookup_name = 'DE Quotes Open Account Manager >=2000'
) x
) y
)
select * from saved_lookups_cte
The part of the query that does the conversion is
when 1 then [Production_ED].dbo.[RemoveNonASCII]
(cast(cast(substring(sql_tree_binary, startIndex, queryLength * 2) as
nvarchar(max))as varchar(max)))
now this actually returns nothing, however if I set the startIndex to 17 and change the queryLength to 3000 * 2 (unicode byte = 2) I get
??Quote?????Norgren_Group Id = 0x0000000000000023???A??????????????? ????????????? ??????????????????????? ? ??????????????????????????????A?????????? ? ???A??Aa??TTID_25R12454.TTID_23R864R12454 __SQ
Returned, although I can read parts, it is not the full query ... yet even if I extend the length I get no more.
Any help would be greatly appreciated.
Code for the RemoveNonASCII function:
ALTER FUNCTION [dbo].[RemoveNonASCII]
(
#nstring nvarchar(max)
)
RETURNS varchar(max)
AS
BEGIN
DECLARE #Result varchar(max)
SET #Result = ''
DECLARE #nchar nvarchar(1)
DECLARE #position int
SET #position = 1
WHILE #position <= LEN(#nstring)
BEGIN
SET #nchar = SUBSTRING(#nstring, #position, 1)
IF ASCII(#nchar) between 32 and 255
SET #Result = #Result + #nchar
SET #position = #position + 1
END
RETURN #Result
END
GO
Ok, so I found the issue, inside the hex string there were null values, which caused the rest of the returned varchar string to become null .. So I just changed the function to:
AS
BEGIN
DECLARE #Result varchar(max)
SET #Result = ''
DECLARE #nchar nvarchar(1)
DECLARE #position int
SET #position = 1
WHILE #position <= LEN(#nstring)
BEGIN
SET #nchar = SUBSTRING(#nstring, #position, 1)
IF ASCII(#nchar) between 1 and 125
SET #Result = #Result + #nchar
SET #position = #position + 1
END
RETURN #Result
END
Eradicating 0 being the ASCII Null value!
I have a query which so far returns the following;
Stock Code BomReference Description
2134601A 5134601A ***DISC*** 004601 EXP Pack I PC Spoo (NF) 500MLX6
2134601A 5109052 40010934 IPC2101 UK PACK PC SHAMPOO (NF) 500MLX6
2134601A 5134601B 40010908 004601 EXP PACK PC SHAMPOO 500MLX6
2134601A 5109052L 40010909 IPC2101L UK PACK IPC SPOO 500ML X 6
The code is as follows;
SELECT BomComponents.StockCode, BomHeaders.BomReference, BomHeaders.Description
FROM BomComponents INNER JOIN
BomHeaders ON BomComponents.HeaderID = BomHeaders.ID
WHERE StockCode = '2134601A'
I want to be able to select just the first word/number from Description and then group the bom reference and Description together to result in the following.
StockCode BomReference Description
2134601A 5134601A, 5109052, 5134601B, 5109052L ***DISC***, 40010934, 40010908, 40010909
Any help would be massively helpful.
Creating the function below should accomplish this for you. I use it VERY frequently.
select dbo.getTokenValue([Your String], [Delimiting Character], [Position])
In this case:
select dbo.getTokenValue(Description, ' ', 1)
/****** Object: UserDefinedFunction [dbo].[getTokenValue] Script Date: 7/8/2014 1:08:08 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*
Accepts the string, delimeter and the position of the required value and returns the value
*/
create function [dbo].[getTokenValue] (#tokenvalue varchar(200), #Delimeter char, #pos int)
returns varchar(200)
Begin
Declare #DelimPos int
Declare #remSubstr varchar(200)
Declare #FinalStr varchar(200)
Declare #Count int
Declare #Countdelim int
set #Finalstr = ''
Set #Countdelim = 0
Set #remSubstr = #tokenValue
Set #Count = #pos-1
set #countdelim = 1
while #Count <> 0
Begin
Set #DelimPos = charindex(#Delimeter,#remSubstr)
If #DelimPos = 0
Break;
set #remSubstr = substring(#remSubstr,#DelimPos+1,Len(#remSubstr)-#DelimPos)
set #Count = #Count -1
set #CountDelim = #CountDelim + 1
End
If #Pos > #CountDelim
Begin
set #Finalstr = null
return #FinalStr
end
else
Begin
Set #DelimPos = charindex(#Delimeter,#remSubstr)
if #DelimPos = 0
Set #Finalstr = #remsubstr
else
Set #FinalStr = substring(#remSubstr,1,#DelimPos-1)
end
return #FinalStr
end
GO
Can i write the following query in other way?
ALTER PROC Proc_AssetManagement_SubscriptionCheck #IMEINumber NVARCHAR(100)
,#PhoneNo NVARCHAR(200)
,#SIMNo NVARCHAR(100)
,#Id INT
,#Message NVARCHAR(MAX) OUTPUT
AS
BEGIN
DECLARE #SimMessage NVARCHAR(200) = ''
DECLARE #PhoneMessage NVARCHAR(200) = ''
SET #Message = ''
SELECT #Message = (
CASE
WHEN COUNT(IMEINumber) > 0
THEN ('IMEI Already Exists\n')
ELSE ' '
END
)
FROM tblAssetSubscriptionDetails
WHERE AssetMgmtId <> #Id
AND (IMEINumber = #IMEINumber)
SELECT #PhoneMessage = (
CASE
WHEN COUNT(PhoneNo) > 0
THEN ('PhoneNo Already Exists\n')
ELSE ' '
END
)
FROM tblAssetSubscriptionDetails
WHERE AssetMgmtId <> #Id
AND (PhoneNo = #PhoneNo)
SELECT #SimMessage = (
CASE
WHEN COUNT(SimNo) > 0
THEN ('SimNo Already Exists')
ELSE ' '
END
)
FROM tblAssetSubscriptionDetails
WHERE AssetMgmtId <> #Id
AND (SIMNo = #SIMNo)
SET #Message = #Message + '' + #PhoneMessage + '' + #SimMessage
END
I want to reduce the number of queries and want to get the message in single query rather then 3 different queries. Can i do it? If so then how?
My purpose is that I want to get a message formed like this.
If IMEINumber is found in the table then it will show IMEI number already exists.
If SIm No is found in the table along with IMEINumber then it will show IMEI number already exists\nSim No already exists and so on...
Would this fit your need ? (Using a CTE: http://msdn.microsoft.com/en-us/library/ms175972.aspx. I wrote it partially in Notepad++ so I hope there aren't syntax error.)
The CTE tASD (stands for tblAssetSubscriptionDetails) gathers only the rows that will be useful. EXISTS should normally be better than Count() > 0 since it doesn't actually need to count everything.
WITH tASD(AssetMgmtId, IMEINumber, PhoneNo, SIMNo)
AS
(
SELECT AssetMgmtId, IMEINumber, PhoneNo, SIMNo
FROM tblAssetSubscriptionDetails
WHERE AssetMgmtId <> #Id
AND
(
IMEINumber = #IMEINumber
OR PhoneNo = #PhoneNo
OR SIMNo = #SIMNo
)
)
SELECT #Message = (
CASE
WHEN EXISTS (SELECT null FROM tASD WHERE IMEINumber = #IMEINumber)
THEN ('IMEI Already Exists\n')
ELSE ''
END
)
+
CASE
WHEN EXISTS (SELECT null FROM tASD WHERE PhoneNo = #PhoneNo)
THEN ('PhoneNo Already Exists\n')
ELSE ''
END
)
+
CASE
WHEN EXISTS (SELECT null FROM tASD WHERE SIMNo = #SIMNo)
THEN ('SimNo Already Exists')
ELSE ''
END
)
Say I've got a function or stored procedure that takes in several VARCHAR parameters. I've gotten tired of writing SQL like this to test if these parameters have a value:
IF #SomeVarcharParm IS NOT NULL AND LEN(#SomeVarcharParm) > 0
BEGIN
-- do stuff
END
There's gotta be a better way to do this. Isn't there?
You can do ISNULL(#SomeVarcharParam, '') <> '' or you can create a UDF that returns a bit:
create function dbo.IsNullOrEmpty(#x varchar(max)) returns bit as
BEGIN
IF #SomeVarcharParm IS NOT NULL AND LEN(#SomeVarcharParm) > 0
RETURN 0
ELSE
RETURN 1
END
And call that using IF NOT dbo.IsNullOrEmpty(#SomeVarcharParam) BEGIN ...
Keep in mind that when calling a UDF, you MUST prefix the owner of it (here, dbo.)
IF COALESCE(#SomeVarcharParm, '') <> ''
BEGIN
-- do stuff
END
If I'm concatenating or coalescing a string inline (within a select statement), and I want to check if the column is NULL or Empty, I do this:
ISNULL('starting to build string '
+ NULLIF(some_table..some_col_that_might_be_null_or_empty, '')
, 'string to append if the resulting concatenation is null')
The NULLIF on the inner part of the expression will force the column to be NULL if it's empty, then the outer ISNULL expression can depend on consistent input and react accordingly.
Here is my function that "extends" ISNULL and checks for empty as well. The test value is checked for null and if it is not null it is trimmed and then checked for length.
The function returns the test string if it is NOT Null or Empty, otherwise the second string is returned.
CREATE FUNCTION [dbo].[ISNULLOREMPTY]
(
#value NVARCHAR(max),
#return NVARCHAR(max)
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
IF (#value IS NULL)
BEGIN
RETURN #return
END
ELSE
BEGIN
IF (LEN(LTRIM(#value)) = 0)
BEGIN
RETURN #return
END
END
RETURN #value;
END
GO
I upvoted Atron's answer though he technically implemented IfNullOrWhiteSpace.
Here's my implementation of IfNullOrEmpty():
IF EXISTS (SELECT * FROM sys .objects WHERE object_id = OBJECT_ID(N'[dbo].[IfNullOrEmpty]' ) and type in ( N'FN'))
DROP FUNCTION dbo.IfNullOrEmpty
go
CREATE FUNCTION dbo.IfNullOrEmpty(#value varchar(max), #substitute varchar(max)) returns varchar(max) as
BEGIN
IF #value IS NOT NULL AND LEN(#value) > 0
RETURN #value
RETURN #substitute
END
I realize this is an old question, but this is what I use in MSSQL:
LEN(ISNULL(#asdf, ''))>0
Example:
DECLARE #asdf varchar(10)
SET #asdf = NULL --You can change this value to test different outputs
BEGIN IF LEN(ISNULL(#asdf, '')) > 0
PRINT #asdf
ELSE
PRINT 'IS NullOrEmpty'
END
--You can use it inline like this:
PRINT CASE LEN(ISNULL(#asdf, '')) WHEN 0 THEN 'IS NullOrEmpty' ELSE #asdf END
I think this is simpler and more straight forward than the other solutions because it is literally checking if the string is null or has a length of 0.
You don't need to check for null before calling LEN. You can just use LEN(#SomeVarcharParm) > 0. This will return false if the value is NULL, '', or ' '. This is because NULL > 0 returns false. See for yourself run:
SELECT
CASE WHEN NULL > 0 THEN 'NULL > 0 = true' ELSE 'NULL > 0 = false' END,
CASE WHEN LEN(NULL) > 0 THEN 'LEN(NULL) = true' ELSE 'LEN(NULL) = false' END,
CASE WHEN LEN('') > 0 THEN 'LEN('''') > 0 = true' ELSE 'LEN('''') > 0 = false' END,
CASE WHEN LEN(' ') > 0 THEN 'LEN('' '') > 0 = true' ELSE 'LEN('' '') > 0 = false' END,
CASE WHEN LEN(' test ') > 0 THEN 'LEN('' test '') > 0 = true' ELSE 'LEN('' test '') > 0 = false' END
You can just do IF #SomeVarcharParam <> '' since the condition will evaluate to NULL and the branch won't be taken if the parameter is null
Use this function (based on Derek's):
CREATE FUNCTION dbo.isNullOrEmpty(#x varchar(max)) RETURNS BIT AS
BEGIN
IF #x IS NOT NULL AND LEN(#x) > 0
RETURN 0
RETURN 1
END
as
dbo.isNullOrEmpty(#someVar)
or
WHERE dbo.isNullOrEmpty(#someVar) = 1
in a stored procedure or query.
please use case command.case structure is:
case when condition then 'some value' else 'some other value' end
some example:
declare #SomeVarcharParm as nvarchar(20)=''
declare #SomeVarcharParm2 as nvarchar(20)=''
--select 1 or 2
select case when #SomeVarcharParm is null or #SomeVarcharParm='' then 1 else 2 end
--if null or empty set 'empty' value else set #SomeVarcharParm value
select #SomeVarcharParm2=case when #SomeVarcharParm is null or #SomeVarcharParm='' then 'empty' else #SomeVarcharParm end
--use null or empty in update command
update tbl1
set field1=case when field1 is null or field1='' then #SomeVarcharParm2 else field1 end
where id=1