SQL Server 2005 : how to check for empty string - sql-server-2005

I have a Procedure which returns MobileAreaCode+Mobile if they are not null
I just want to add to it support for empty strings as well I tried(without the handle for empty string it works)
ALTER PROCEDURE PROC_NAME
#Identification INT
AS
BEGIN
SELECT
CASE WHEN MobileAreaCode is NOT NULL OR Mobile is NOT NULL
OR MobileAreaCode<>'' OR Mobile<>''
THEN
MobileAreaCode+Mobile
END
FROM
TABLE_NAME
WHERE
id = 123456789
END
GO
which doesn't work and results the following error:
Incorrect syntax near the keyword 'FROM'.

You can use Use NULLIF (Transact-SQL).
select nullif(MobileAreaCode, '')+nullif(Mobile, '') as MobileAreaCodeMobile
from YourTable
SQL Fiddle

ALTER PROCEDURE PROC_NAME
#Identification INT
AS
BEGIN
SELECT
CASE WHEN (MobileAreaCode is NOT NULL) AND (Mobile is NOT NULL)
AND (len(MobileAreaCode)>0) AND (len(Mobile)>0)
THEN
MobileAreaCode+Mobile
END
FROM
TABLE_NAME
WHERE
id = 123456789
END
GO

Related

DB2 SQL - Filter non-xml, invalid xml data using XMLCAST(XMLQUERY (Xpath) function in case statement

Need help in filtering bad xml data from the column which is blob format. Using the below query to achieve this and facing the below error.
Query:
SELECT COLUMN1 AS NAME, CASE WHEN COLUMN2 = '' THEN XMLCAST( XMLQUERY( '$file/*:value' PASSING XMLPARSE( DOCUMENT TABLE."DATA" ) AS "file" ) AS VARCHAR(10)) ELSE COLUMN2 END AS INFO FROM TABLE
Error:
[Code: -16110, SQL State: 2200M] XML syntax error. Expected to find "Comment or PI".. SQLCODE=-16110, SQLSTATE=2200M, DRIVER=4.22.29
There is no "safe" XMLPARSE function in Db2, but you may create it.
--#SET TERMINATOR #
-- Returns NULL on bad document
CREATE FUNCTION XMLPARSE_SAFE (P_DOC CLOB (1M))
RETURNS XML
CONTAINS SQL
DETERMINISTIC
NO EXTERNAL ACTION
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN END;
RETURN XMLPARSE (DOCUMENT P_DOC);
END#
-- Usage
SELECT S, XMLPARSE_SAFE (S) AS DOC
FROM
(
VALUES
'<value>Not Eligible</value>'
, '<value>Not Eligible</alue>'
) T (S)#

I am creating a procedure in MariaDB, but it is giving Error "#1064 "

I am creating a procedure for a split string in MariaDB but it is giving error
"#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'TRUNCATE TABLE splittedstringtable"
CREATE PROCEDURE splitstring( IN splittedstring varchar(256))
BEGIN
CREATE TABLE IF NOT EXISTS splittedstringtable (ID INT NOT NULL AUTO_INCREMENT,NAME VARCHAR(256) NOT NULL,PRIMARY KEY (ID))
TRUNCATE TABLE `splittedstringtable`
DECLARE x INT DEFAULT 0
DECLARE y INT DEFAULT 0
SET y = 1
IF NOT splittedstring IS NULL
THEN
SELECT LENGTH(splittedstring) - LENGTH(REPLACE(splittedstring, ',', '')) INTO #noOfCommas
IF #noOfCommas = 0
THEN
INSERT INTO splittedstringtable(NAME) VALUES(splittedstring)
ELSE
SET x = #noOfCommas + 1
WHILE y <= x DO
SELECT split_string(splittedstring, ',', y) INTO #engName
INSERT INTO splittedstringtable(NAME) VALUES(#engName)
SET y = y + 1
END WHILE
END IF
END IF
END
In MariaDB/MySQL statements are terminated with semicolons ;. Add semicolon after each statement.
You might also want to reconsider to use temporary table instead of normal table as you might run into trouble when multiple users are running the procedure at same time.

How to set NULL in a Coalesce

Quick question on Coalesce:
clw.ClawbackPercent = Coalesce(#ClawbackPercent, clw.ClawbackPercent)
Lets say for column 'ClawbackPercent' I have a value of 100.
If I execute a proc and set parameter #ClawbackPercent to have the value NULL, it keeps the value 100 in the row for that column which is great.
However, if I want to set 100 to actually be NULL, what do I need to write in the exec proc statement or what do I need to add in the Coalesce statement?
Thank you
It sounds like you want 100 to be the Default value of a stored proc parameter, not necessarily to replace all NULLs with this value. If this is the case, you don't want a COALESCE but you do need to provide a default value for the parameter on the proc definition.
e.g.
CREATE PROC dbo.MyProc (
#MyParam INT = 100
)
AS
-- My code here
If somebody executes this proc without specifying a value for #MyParam, the default of 100 will be assigned. If they explicitly specify #MyParam = NULL then NULL will be assigned..
Then probably you should not use coalesce, instead you can use case statement as below:
clw.ClawbackPercent = CASE WHEN #ClawbackPercent = 100
THEN NULL
ELSE
#ClawbackPercent END
in the select statement
You have to write in the following way:
CREATE PROCEDURE sp_test(
#ClawbackPercent VARCHAR(30)
) AS
BEGIN
SELECT COALESCE(#ClawbackPercent, '100')
END
--call as below and if you want to return second value then, EXEC sp_test NULL
--if your second, thirds... parameters are in INTEGER then simply CAST to VARCHAR
EXEC sp_test 'NULL'

ESCAPE SEQUENCE NOT SUPPORT IN SQL

My Table Contains
Id(int) name(nvarchar(300)) path(nvarchar(3000))
--------------------------------------------------------------
8 Subunit1_1 વસૂલાત/SubUnit/!##$%^&*()_+{}|:"<>?,.;'[]\-=
my Query:
select * from tbl1 where Path = 'વસૂલાત/SubUnit/!##$%^&*()_+{}|:"<>?,.;''[]\-='
I am Getting Empty Table.backslash and single quotes are used.
Use N prefix in your search string something like this...
select * from tbl1
where Path = N'વસૂલાત/SubUnit/!##$%^&*()_+{}|:"<>?,.;''[]\-='
Because you have these unicode characters in your strings, you need to tell sql server explicitly that string may contain some unicode character by prefixing it with N.
Same is true when you are inserting, updating unicode data in sql server.
Your Updated Stored Procedure
CREATE PROCEDURE [dbo].[spSCS_ManageOrgunits]
#DomainId int,
#orgunitpath nvarchar(3000),
#iDisplayStart int,
#iDisplayLength int
AS
BEGIN
SET NOCOUNT ON;
IF #orgunitpath = ''
BEGIN
SELECT a.[row],a.OrgUnitId,a.did,a.OrgUnitName,a.OrgUnitPath,a.ScheduledStatus,a.AutoSyncStatus
FROM
(
SELECT ROW_NUMBER() OVER (ORDER BY OrgUnit_tbl.OrgUnitId) AS row,OrgUnitId,did,OrgUnitName,OrgUnitPath,ScheduledStatus,AutoSyncStatus
FROM OrgUnit_tbl
WHERE did = #DomainId AND OrgUnitPath = #orgunitpath
) AS a
WHERE a.[row] >= #iDisplayStart AND a.[row] < #iDisplayStart+#iDisplayLength
END
ELSE
BEGIN
SELECT OrgUnitId,did,OrgUnitName,OrgUnitPath,ScheduledStatus,AutoSyncStatus
FROM OrgUnit_tbl
WHERE did = #DomainId AND OrgUnitPath = #orgunitpath
END
END

Using SYSTEM_USER as a parameter to a table valued function

I've created a table valued function:
CREATE FUNCTION TestFunction
(
#username VARCHAR(80)
)
RETURNS TABLE
AS
RETURN
(
SELECT 0 AS TestValue
)
Then try to call it as so:
SELECT TestValue
FROM dbo.TestFunction(SYSTEM_USER)
but get the error:
Incorrect syntax near the keyword 'SYSTEM_USER'
I've even tried making this a table valued function which is not inline, but I get the same error.
Am I missing something? Why am I getting this error?
On my 2k8 server I can only reproduce that with SQL Server 2000 (80) Compatibility level set, check the level of your 2005 database.
Meantime you can;
declare #su varchar(30) = SYSTEM_USER
select * from dbo.TestFunction(#su)