Select part of a string between two values SQL 2008 - sql-server-2008

I'm attempting to select a part of a strint between 2 values, i've managed to get it working to about 90% but then get an error -
SUBSTRING(TranText, CHARINDEX('x', TranText) + 1, LEN(TranText) - CHARINDEX('x', TranText) - CHARINDEX('/', REVERSE(TranText)))
The field it is querying is like so
Start Date : 01/02/2013 50 x 156.00/MX + 207.64
with the desired result being
156.00
Now I think the issue is because sometimes the X can have a space before or after it, or no space at all. It gets through about 114,000 rows before throwing
Invalid length parameter passed to the LEFT or SUBSTRING function.
But am struggling to resolve.

The main root cause could be because LEN(#QRY) - CHARINDEX('x', #QRY)-CHARINDEX('/', REVERSE(#QRY)) is less than zero ie, negative value. This in turn will throw error in SUBSTRING since the minimum index for SUBSTRING is zero. Therefore we check that condition in a case statement.
SELECT CASE WHEN LEN(TranText) - CHARINDEX('x',TranText)-CHARINDEX('/', REVERSE(TranText)) >= 0 THEN
SUBSTRING(TranText, CHARINDEX('x', TranText) + 1, LEN(TranText) - CHARINDEX('x', TranText) - CHARINDEX('/', REVERSE(TranText)))
ELSE NULL END
FROM YOURTABLE

Try this. Used REPLACE function and then parsed.
DECLARE #string AS VARCHAR(100), #result AS VARCHAR(100)
DECLARE #nStart AS int
SET #string = 'Start Date : 01/02/2013 50 x 156.00/MX + 207.64'
SET #result = REPLACE(#string, '/', '')
SET #nStart = CHARINDEX('x', #result) + 1
SET #result = SUBSTRING(#result, #nStart, CHARINDEX('M',#result) - #nStart)
SELECT #result

Related

Adding a single Digit to a 4-digits long number in mysql

I'm trying to get a mysql script, that changes every 4-digit long number "into" a 5-digit long, by adding a "0" at the start of each number. This is, what I tried:
SELECT * FROM `customer_address_entity_text` WHERE CHAR_LENGTH(value) < 5;
SELECT CONCAT("0", CAST(value as CHAR(50)) AS value;
but it shows an error, that there is no field "value" found:
#1054 - Unknown field 'value' in field list (translated)
would be nice, if someone could help me with this.
(it also gives out this error, when I'm not trying to Cast 'value' to a CHAR)
tl;dr: I want 'value = "0" + value' in mysql
example:
'value = 1234; value = "0" + value; value = 01234' and that in mysql
Two issues:
There is a missing closing parenthesis for CONCAT(
Your second SELECT has no FROM clause, so indeed there is no value field there.
So move that CONCAT expression inside the first SELECT clause and balance the parentheses:
SELECT c.*, CONCAT("0", CAST(value as CHAR(50))) AS value
FROM `customer_address_entity_text` c
WHERE CHAR_LENGTH(value) < 5;
If your purpose is to pad all values with zeroes so they get 5 digits, so that it also transforms 1 to "00001" and 12 to "00012", then use LPAD:
SELECT c.*, LPAD(value, 5, "0") AS value
FROM `customer_address_entity_text` c;
To update the value field:
UPDATE `customer_address_entity_text`
SET value = LPAD(value, 5, "0");
Or, with your original concat version:
UPDATE `customer_address_entity_text`
SET value = CONCAT("0", CAST(value as CHAR(50)))
WHERE CHAR_LENGTH(value) < 5;

SQL server triming

I am trying to format the licenseenum column in my table by removing everything starting the before space and also I would like to remove any character in licenseenum column starting after '-' including '-'
For example:
current data in Licenseenum GA 350-0
What I'm trying to get 350
Here is my code
select Licenseenum, SUBSTRING (Licenseenum, 4, LEN(Licenseenum)-1)
from licensee
this results
350-0
How would I remove -0 from the results?
Thanks for the help
Try it like this
DECLARE #YourString VARCHAR(100)='GA 350-0';
SELECT SUBSTRING(#YourString,CHARINDEX(' ',#YourString,1),CHARINDEX('-',#YourString,1)-CHARINDEX(' ',#YourString,1));
UPDATE 1
This is quite the same, but better to read
DECLARE #YourString VARCHAR(100)='GA 350-0';
WITH Positions AS
(
SELECT CHARINDEX(' ',#YourString,1) AS posBlank
,CHARINDEX('-',#YourString,1) AS posMinus
)
SELECT SUBSTRING(#YourString,posBlank,posMinus-posBlank)
FROM Positions;
UPDATE 2 Avoid the leading blank...
My logic needs small correction in order to cut the blank before the number:
SELECT SUBSTRING(#YourString,posBlank+1,posMinus-posBlank-1)
Would be the same with the first example...
Please try the below code. Its working fine in SQL Server 2012.
DECLARE #Licenseenum varchar(max)
SET #Licenseenum ='GA 350-0'
DECLARE #TempLicense VARCHAR(100)
DECLARE #License VARCHAR(100)
IF (len(#Licenseenum ) - len(replace(#Licenseenum ,' ',''))>=1)
SET #TempLicense = (SELECT REVERSE(LEFT(REVERSE(#Licenseenum ),CHARINDEX(' ', REVERSE(#Licenseenum ), 1) - 1)))
ELSE
SET #TempLicense = #Licenseenum
SELECT #License = (SELECT LEFT(#TempLicense,LEN(#TempLicense) - charindex('-',reverse(#TempLicense),1)))
SELECT #License AS Licenseenum

Multiple string values parameter passing to SP in SSRS

I have read and tried most of the related topics on this forum, but most of them don't have a lot of feedback.
So I am using SSRS 2008 R2 with Report Builder 3.
I have a simple SP as follows that filters my data using the IN.
But for some reason its not passing the parameters correctly to my sp and only filter's by the first option I select, hope you can assist.
--SP
#Warehouse varchar(max)
AS
BEGIN
SELECT WBal.Warehouse ,WBal.StockCode,((WBal.[Current] - WBal.Bal1)) CMov,((WBal.Bal1 - WBal.Bal2)) P1Mov,((WBal.Bal2 - WBal.Bal3)) P2Mov
,((WBal.Bal3 - WBal.Bal4)) P3Mov,((WBal.Bal4 - WBal.Bal5)) P4Mov,((WBal.Bal5 - WBal.Bal6)) P5Mov,((WBal.Bal6 - WBal.Bal7)) P6Mov
,((WBal.Bal7 - WBal.Bal8)) P7Mov,((WBal.Bal8 - WBal.Bal9)) P8Mov,((WBal.Bal9 - WBal.Bal10)) P9Mov,((WBal.Bal10 - WBal.Bal11)) P10Mov
,((WBal.Bal11 - WBal.Bal12)) P11Mov
FROM
(
SELECT [StockCode]
,[Warehouse]
,(QtyOnHand * UnitCost) [Current]
,(OpenBalCost1 * OpenBalQty1) Bal1
,(OpenBalCost2 * OpenBalQty2) Bal2
,(OpenBalCost3 * OpenBalQty3) Bal3
,(OpenBalCost4 * OpenBalQty4) Bal4
,(OpenBalCost5 * OpenBalQty5) Bal5
,(OpenBalCost6 * OpenBalQty6) Bal6
,(OpenBalCost7 * OpenBalQty7) Bal7
,(OpenBalCost8 * OpenBalQty8) Bal8
,(OpenBalCost9 * OpenBalQty9) Bal9
,(OpenBalCost10 * OpenBalQty10) Bal10
,(OpenBalCost11 * OpenBalQty11) Bal11
,(OpenBalCost12 * OpenBalQty12) Bal12
FROM [SysproCompanyR].[dbo].[InvWarehouse]
Where Warehouse in (SELECT * FROM dba_parseString_udf((#Warehouse), ' '))
) WBal
END
I have the following udf that apparently will split of the string, but im not that good with this so I don't know if my problem perhaps lies with the udf
UDF
ALTER FUNCTION [dbo].[dba_parseString_udf]
(
#stringToParse VARCHAR(8000)
, #delimiter CHAR(1)
)
RETURNS #parsedString TABLE (stringValue VARCHAR(128))
AS
/*********************************************************************************
Name: dba_parseString_udf
Author: Michelle Ufford, http://sqlfool.com
Purpose: This function parses string input using a variable delimiter.
Notes: Two common delimiter values are space (' ') and comma (',')
Date Initials Description
----------------------------------------------------------------------------
2011-05-20 MFU Initial Release
*********************************************************************************
Usage:
SELECT *
FROM dba_parseString_udf(<string>, <delimiter>);
Test Cases:
1. multiple strings separated by space
SELECT * FROM dbo.dba_parseString_udf(' aaa bbb ccc ', ' ');
2. multiple strings separated by comma
SELECT * FROM dbo.dba_parseString_udf(',aaa,bbb,,,ccc,', ',');
*********************************************************************************/
BEGIN
/* Declare variables */
DECLARE #trimmedString VARCHAR(8000);
/* We need to trim our string input in case the user entered extra spaces */
SET #trimmedString = LTRIM(RTRIM(#stringToParse));
/* Let's create a recursive CTE to break down our string for us */
WITH parseCTE (StartPos, EndPos)
AS
(
SELECT 1 AS StartPos
, CHARINDEX(#delimiter, #trimmedString + #delimiter) AS EndPos
UNION ALL
SELECT EndPos + 1 AS StartPos
, CharIndex(#delimiter, #trimmedString + #delimiter , EndPos + 1) AS EndPos
FROM parseCTE
WHERE CHARINDEX(#delimiter, #trimmedString + #delimiter, EndPos + 1) <> 0
)
/* Let's take the results and stick it in a table */
INSERT INTO #parsedString
SELECT SUBSTRING(#trimmedString, StartPos, EndPos - StartPos)
FROM parseCTE
WHERE LEN(LTRIM(RTRIM(SUBSTRING(#trimmedString, StartPos, EndPos - StartPos)))) > 0
OPTION (MaxRecursion 8000);
RETURN;
END
So what im trying to achieve is, I have a sp that populates my values for my parameter as did the setup like follow in ssrs
SELECT DISTINCT (Warehouse)
FROM [SysproCompanyR].[dbo].[InvWarehouse]
And then in my Dataset for my report where I need to apply this looks I have tried various alternatives in the parameter option:
expression
=JOIN(Parameters!Warehouse.Value,",")
When you pass multiple values from SSRS, it sends the values as comma delimited value1,value2,value3....
In you query you are splitting on white string ' '
Where Warehouse in (SELECT * FROM dba_parseString_udf((#Warehouse), ' '))
It should be using comma , instead of the white space ' '
Where Warehouse in (SELECT * FROM dba_parseString_udf((#Warehouse), ','))

summing time column in mysql

I have a column in one of my tables, which is TIME format (00:00:00). I am trying to sum the entire column and display it as same (00:00:00).
I have tried using the following but it is not giving me anywhere near the correct answer.It's giving me 22.12:44:00 and manual calcaulation tells me it should be close to 212:something:something
SELECT SEC_TO_TIME( SUM( TIME_TO_SEC( vluchttijd ) ) ) AS totaltime FROM tbl_vluchtgegevens
Any recommendations?
You can try like this:-
SELECT SEC_TO_TIME(SUM(SECOND(vluchttijd ))) AS totaltime FROM tbl_vluchtgegevens;
or try this(althoug this is not a good approach):
SELECT concat(floor(SUM( TIME_TO_SEC( `vluchttijd ` ))/3600),":",floor(SUM( TIME_TO_SEC( `vluchttijd ` ))/60)%60,":",SUM( TIME_TO_SEC( `vluchttijd ` ))%60) AS total_time
FROM tbl_vluchtgegevens;
Edit:-
Try this:-
select cast(sum(datediff(second,0,dt))/3600 as varchar(12)) + ':' +
right('0' + cast(sum(datediff(second,0,dt))/60%60 as varchar(2)),2) +
':' + right('0' + cast(sum(datediff(second,0,dt))%60 as varchar(2)),2)
from TestTable
Working SQL Fidlle
In MySQL, the TIME type is rather limited in range. Moreover many time function do not accept values greater that 23:59:59, making it really usable only to represent the time of the day.
Given your needs, your best bet is probably to write a custom function that will mimic SEC_TO_TIME but allowing much greater range:
CREATE FUNCTION SEC_TO_BIGTIME(sec INT)
RETURNS CHAR(10) DETERMINISTIC
BEGIN
SET #h = sec DIV 3600;
SET #m = sec DIV 60 MOD 60;
SET #s = sec MOD 60;
RETURN CONCAT(
LPAD(#h, 4, '0'),
':',
LPAD(#m, 2, '0'),
':',
LPAD(#s, 2, '0')
);
END;
And here is how to use it:
create table tbl (dt time);
insert tbl values
('09:00:00'), ('01:00:00'), ('07:50:15'), ('12:00:00'),
('08:30:00'), ('00:45:00'), ('12:10:30');
select SEC_TO_BIGTIME(sum(time_to_sec(dt))) from tbl;
Producing:
+--------------------------------------+
| SEC_TO_BIGTIME(SUM(TIME_TO_SEC(DT))) |
+--------------------------------------+
| 0051:15:45 |
+--------------------------------------+
See http://sqlfiddle.com/#!8/aaab8/1
Please note the result is a CHAR(10) in order to overcome TIMEtype limitations. Depending how you plan to use that result, that means that you may have to convert from that string to the appropriate type in your host language.
This worked for me:
SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(vluchttijd))) AS totaltime FROM tbl_vluchtgegevens;

Hundred year date conversion in ssis derived column

I'm trying to convert a Hundred Year Date (HYD) format to a regular date format through SSIS derived column transform. For example: Convert 41429
to 06/04/2013. I can do it with formatinng code within a script (and maybe I simply have to go this route) but feel there has to be a way to do so within a derived column that I'm just not getting. Any help is appreciated.
This is what I came up with. Are you sure your conversion is correct? My answer is 1 day. off.
DECLARE #t1 as date = '01/01/1900';
DECLARE #t2 as DATE = '12/31/1900';
DECLARE #hyd as INT;
-- This example shows that we need to add 1
SELECT #hyd = DATEDIFF (d, #t1, #t2) + 1 -- 364 + 1
SELECT #hyd
set #t2 = '06/04/2013';
SELECT #hyd = DATEDIFF (d,#t1, '06/04/2013') + 1-- 41427
SELECT #hyd
SELECT DATEADD (d, #hyd, '01-JAN-1900')
SELECT DATEADD (d, 41429, '01-JAN-1900')
A hundred year date is a calculation based on the number of days since 1899-12-31. It's an "Excel Thing". It also has a bug in it that you must account for.
The equivalent TSQL logic would be
DECLARE
#HYD int = 41429;
SELECT
#HYD =
CASE
WHEN #HYD > 60
THEN #HYD -1
ELSE
#HYD
END;
SELECT
DATEADD(d, #HYD, '1899-12-31') AS HYD;
Armed with that formula, I can write the following Expression in a Derived Column Transformation (assuming you have a column named HYD)
(HYD > 60) ? DATEADD("d",HYD - 1,(DT_DATE)"1899-12-31") : DATEADD("d",HYD,(DT_DATE)"1899-12-31")
And the results
--or inline SQL...using this
SELECT
case when ([HYD] > 60) then
DATEADD(day,[HYD] - 1,'1899-12-31')
else
DATEADD(day,[HYD],'1899-12-31')
end 'HYD_conv'
FROM
TableName
--and in the where clause if you like...
WHERE
(case when ([HYD] > 60) then DATEADD(day,[HYD] - 1,'1899-12-31') else DATEADD(day,[HYD],'1899-12-31') end) = '2016-01-14'