Remove first leading 0 from varchar with MSSQL - sql-server

How I can remove only the first 0 character from a varchar?
For example '000303' ==> '00303'
I tried this without success, all the 0 characters are removed:
SELECT SUBSTRING('000303', PATINDEX('%[^0]%', '000303'),LEN('000303'));
Thanks

Try using STUFF Function
SELECT CASE WHEN LEFT('000303',1) = '0' THEN STUFF('000303',1,1,'') ELSE '000303' END
or Use RIGHT Function
SELECT CASE WHEN LEFT('000303',1) = '0' THEN RIGHT('000303', LEN('000303')-1) ELSE '000303' END
Instead of LEFT('000303',1) = '0' check you can also use
charindex('0','000303') = 1 or
'000303' LIKE '0%' (ughai suggestion)

Try this:
SELECT RIGHT(MyColumn, LEN(MyColumn) - 1)
This will remove the first character from the varchar column.
If it is specific to 0 then try this:
SELECT CASE WHEN LEFT(MyColumn,1) = '0'
THEN RIGHT(MyColumn, LEN(MyColumn) - 1)
ELSE
MyColumn END

Something like
SELECT CASE WHEN LEFT(Mycol,1) = '0' THEN SUBSTRING(MyCOL, 2, LEN(MyCOL)) END

Related

How do you write an IF ELSE inside a SELECT statement in MSSQL 2008?

Here is what I am trying to do:
SELECT iif(d.ItemType = 'INVOICE', 0, iif(d.ItemType = 'PACKING', 0, 1)) AS MissingDocuments FROM dbo.DocumentDetails AS D
Unfortunately realized this is not compatible with MSSQL2008. So tried writing an IF ELSE but that also is not working.
SELECT IF d.ItemType = 'INVOICE'
0
ELSE
BEGIN
d.ItemType = 'PACKING'
0
ELSE
1
END AS MissingDocuments
FROM dbo.DocumentDetails AS D
can you please tell me what am I doing wrong here ?
Use CASE ... WHEN. The most concise logic seems to be:
SELECT
CASE WHEN d.ItemType IN ('INVOICE', 'PACKING') THEN 0 ELSE 1 END
AS MissingDocuments
FROM dbo.DocumentDetails AS d
i.e. the Document is missing if it isn't 'Invoice' or 'Packing'
I think You Looking For Case When
Try This..
SELECT
case when d.ItemType = 'INVOICE' or d.ItemType = 'PACKING'
then 0
ELSE
1
END AS MissingDocuments
FROM dbo.DocumentDetails AS D

returning a bit type in a stored procedure

I have a stored procedure in which I am validating some columns to return a value of 1 or 0.
The stored proc code block is:
, CASE WHEN
(PolicyStartMileage < LossMileage)
AND (PolicyStartDate <= LossDate)
AND (Certified = 0)
THEN '1'
ELSE '0'
END
AS Certifiable
My question is, how do I return '1' or '0' as a bit type?
In my codebehind, I am using:
Boolean.Parse(item.GetDataKeyValue("Certifiable").ToString());
in order to read the value (true or false).
If you want the data to be a true BIT data type you need to do a cast, otherwise it will default to numeric.
CASE WHEN
(PolicyStartMileage < LossMileage)
AND (PolicyStartDate <= LossDate)
AND (Certified = 0)
THEN CAST(1 AS BIT)
ELSE CAST(0 AS BIT)
END
AS Certifiable
If you do the cast, you can call the GetBoolean() method from the data reader directly avoiding the parse
, CASE WHEN
(PolicyStartMileage < LossMileage)
AND (PolicyStartDate <= LossDate)
AND (Certified = 0)
THEN 1
ELSE 0
END
AS Certifiable
when you use Quotes around 1 or 0 it treats it as a string not
integer.
Try to remove the quotes '' around 1 and 0 so that it behaves as integers rather than string. like this:
, CASE WHEN
(PolicyStartMileage < LossMileage)
AND (PolicyStartDate <= LossDate)
AND (Certified = 0)
THEN 1
ELSE 0
END
AS Certifiable

How do I put a CASE statement in a SUBSTRING statement?

The purpose of this that I'm trying to compare the usernames of two email addresses and see if they're the same. Really, all I want is for this to work.
When I run the query, all I get is: Invalid length parameter passed to the LEFT or SUBSTRING function.
Note: I changed the query. This better illustrates what I'm trying to do.
Note2: I've made the changes so it works properly, but now if the parameter is '', then I get: Invalid length parameter passed to the LEFT or SUBSTRING function.
declare #ReportParameter1 nvarchar(16)
set #ReportParameter1 = 'manmoon#test1.com'
declare #ReportParameter2 nvarchar(16)
set #ReportParameter2 = ''
select 'test'
where SUBSTRING (case #ReportParameter1 when '' then 'x#' else #ReportParameter1 end, 1, Charindex('#', case #ReportParameter1 when '' then 'x#' else #ReportParameter1 end) - 1) =
SUBSTRING (case #ReportParameter2 when '' then 'x#' else #ReportParameter2 end, 1, Charindex('#', case #ReportParameter2 when '' then 'x#' else #ReportParameter2 end) - 1)
Here's the where clause I used to fix the problem. However, this will teach me to be more careful when copying and pasting.
WHERE (substring(#ReportParameter1, 1, case when (CHARINDEX('#', #ReportParameter1) - 1) < 1 then 1 else CHARINDEX('#', #ReportParameter1) - 1 end) = SUBSTRING(#ReportParameter2, 1, CHARINDEX('#', #ReportParameter2) - 1))
The error comes from not comparing the result of the substring to anything. A string is not a boolean expression.
Edit:
Now that you edited your question, that answer doesn't make any sense any more.
I tested your substring expressions with empty strings, and I don't get any error. If you on the other hand have strings that are not empty, but doesn't contain any # character, then you get the error that you describe.
To handle that you could do like this:
... where case
when #ReportParameter1 = '' or charindex('#', #ReportParameter1) = 0 then 'x'
else substring(#ReportParameter1, 1, charindex('#', #ReportParameter1) - 1)
end =
case
when #ReportParameter2 = '' or charindex('#', #ReportParameter2) = 0 then 'x'
else substring(#ReportParameter2, 1, charindex('#', #ReportParameter2) - 1)
end
Note however that two strings that are not email addresses would compare as equal, as 'x' = 'x', so you might want to use different fallback values in the expressions...
Edit 2:
Come to think of it, you don't need to check for empty strings if you check for the # character, as an empty string can't contain a # character:
... where case
when charindex('#', #ReportParameter1) = 0 then 'x'
else substring(#ReportParameter1, 1, charindex('#', #ReportParameter1) - 1)
end =
case
when charindex('#', #ReportParameter2) = 0 then 'x'
else substring(#ReportParameter2, 1, charindex('#', #ReportParameter2) - 1)
end
put the substring in the case... you basically want a static value on one case... on the other ... use the substring on the "else"
case GPS_Quotes.[Sales Engineer]
when
'' then 'some constant value'
else
substring(GPS_Quotes.[Sales Engineer], 1, ...{I don't understand what your are trying to do})
end

How to get all values including null in a SQL Server case statement?

I have a big stored procedure, and basically I want to select all values (including null) if my variable #DimBrowserId is set to 0. I am using a case statement, however this is only catching values that actually have something and ignoring the NULL valued fields. Because I am using the = clause in the WHERE I cannot do IS NULL. I do not want to have to write 2 IF statements because the stored procedure would then be enormous, so I want to know how to get null values as well. Here is my code:
SELECT
DATEPART(yy, DATEADD(mi, #Mdelta, d.DimDateValue)),
DisableCount = COUNT(*)
FROM
dbo.FactDisable AS f
JOIN
dbo.DimDate AS d ON f.DimDateId = d.DimDateId
JOIN
dbo.DimDevice AS v ON f.DimDeviceId = v.DimDeviceId
WHERE
d.DimDateValue >= #StartDateGMT
AND d.DimDateValue <= #EndDateGMT
AND f.IsTest = #IncludeTest
AND f.DimProductId = #DimProductId
AND v.DimBrowserId = CASE
WHEN #DimBrowserId = 0 THEN v.DimBrowserId
ELSE #DimBrowserId
END
GROUP BY
DATEPART(yy, DATEADD(mi, #Mdelta, d.DimDateValue))
The code is near the CASE clause.
Thanks
Change that line to be
AND (#DimBrowserID = 0 OR #DimBrowserID = v.DimBrowserId)
If #DimBroserID is 0 then no filtering will be applied for this line.
Use ISNULL:
SELECT DATEPART(yy,DATEADD(mi,#Mdelta,d.DimDateValue)),
DisableCount=COUNT(*)
FROM dbo.FactDisable AS f
JOIN dbo.DimDate AS d ON f.DimDateId = d.DimDateId
JOIN dbo.DimDevice AS v ON f.DimDeviceId = v.DimDeviceId
WHERE d.DimDateValue >= #StartDateGMT AND d.DimDateValue <= #EndDateGMT
AND f.IsTest = #IncludeTest AND f.DimProductId = #DimProductId
AND v.DimBrowserId = CASE WHEN ISNULL(#DimBrowserId,0) = 0 THEN v.DimBrowserId ELSE #DimBrowserId END
GROUP BY DATEPART(yy,DATEADD(mi,#Mdelta,d.DimDateValue))
CASE WHEN COALESCE(#MightBeNull, 0) = 0 THEN ZeroResult ...
will be treated as zero if #MightBeNull is null, and whatever #MightBeNull is if it's not null.
Assuming null means any browser, a better data model for this scenario might be to set an ID that identifies any browser, instead of setting it to null.
You probably know what you are running into is NULL does not equal NULL in a comparison.
Assuming you don't have control of the data model to fix that, one option would be to coalesce your NULL values to an unused id.
The resulting WHERE clause would look like this, assuming -1 is the unused value you choose.
AND COALESCE(v.DimBrowserId, -1) = CASE WHEN #DimBrowserId = 0 THEN COALESCE(v.DimBrowserId, -1) ELSE #DimBrowserId END

Error converting varchar to numeric with MSSQL

Query
SELECT TOP 1000
CASE WHEN VRI.Square_Footage <> ''
THEN VRI.Square_Footage
ELSE
CASE WHEN VRI.Property_Type = 'LAND'
THEN CAST((CONVERT(NUMERIC(38, 3),VRI.Acres)*43560) AS DECIMAL)
ELSE
VRI.Lot_Size
END
END
FROM View_Report_Information_Tables AS VRI
Even if I checked for VRI.Acres with isnumeric(), it still yield the same exact error? How can I fix this problem?
ISNUMERIC doesn't guarantee that it will successfully cast to decimal.
SELECT ISNUMERIC('£100') /*Returns 1*/
SELECT CONVERT(NUMERIC(38, 3),'£100') /*Error*/
Additionally all branches of the case statement need to return compatible datatypes. It looks like VRI.Square_Footage is a string.
Does this work for you?
SELECT TOP 1000 CASE
WHEN ISNUMERIC(VRI.Square_Footage + 'e0') = 1 THEN
VRI.Square_Footage
WHEN VRI.Property_Type = 'LAND'
AND ISNUMERIC(VRI.Acres + 'e0') = 1 THEN CAST((
CONVERT(NUMERIC(38, 3), VRI.Acres) * 43560 ) AS DECIMAL)
WHEN ISNUMERIC(VRI.Lot_Size + 'e0') = 1 THEN VRI.Lot_Size
END
FROM View_Report_Information_Tables AS VRI
Here run this
SELECT ISNUMERIC('d25')
That can be converted to float but not decimal/numeric
SELECT CONVERT(FLOAT,'2d5')
As you can see, you can't depend on numeric for decimal/numeric data types
Take a look at IsNumeric, IsInt, IsNumber for some code that will show you how you can check

Resources