Is is possible in a foreach loop to get a greater than and a lesser than position in the same statement.
Currently I have
<xsl:for-each select="(/*/Request/InvoiceDetailRequest/InvoiceDetailOrder/InvoiceDetailItem)[10 >= position()]">
But I want <xsl:for-each select="(/*/Request/InvoiceDetailRequest/InvoiceDetailOrder/InvoiceDetailItem)[10 >= position()] and [10 <= position()">
This doesnt work.
Put both halves of the predicate condition in the same set of square brackets:
(....)[(10 >= position()) and (5 <= position())]
Related
I am using SQL Server and I have a column of type varchar with values in that column like this
2A, 10H, 4, 6T
I need output like this for above examples,
2A --> 2 + 0.25 (A = 0.25) --> 2.25
1H --> 10 + 0.5 (H = 0.5) --> 10.5
4 --> 4
6T --> 6 + 0.75 (T = 0.75) --> 6.75
I know that I have to split it into two parts and then use cast and case-when somehow and then sum it, but couldn't achieve it.
Any help will be appreciated
Assuming the values are always an integer followed by a number, then you could just use REPLACE. I use a JOIN to a VALUES table construct here as you could easily replace it with an actual table to be scalable:
SELECT TRY_CONVERT(decimal(12,2),CONCAT(LEFT(YT.YourColumn,PATINDEX('%[^0-9]%',YT.YourColumn+ '.')-1),V.NumericalValue))
FROM (VALUES('2A'),('10H'),('4'),('6T'))YT(YourColumn)
LEFT JOIN (VALUES('A','0.25'),('H','0.5'),('T','0.75'))V(Character, NumericalValue) ON YT.YourColumn LIKE '%' + V.Character;
Projected code is used to convert a date into integer and vice-versa. I want to know the reason why here we have used this specific hexadecimal codes and the number series to get back the date from int. If there is an article about this code sample it would also help me understand this code actually.
I have tried online Hex to Decimal conversion for this codes and found its a 256^1,256^2... even though trying not able to find the exact reason.
declare #dDate date = '2017-10-12'
declare #iDate int = 0
select #iDate = ( (datepart(year,#dDate)*65536 | datepart(month,#dDate)*256 | datepart(dd,#dDate)))
select (#iDate&0xfff0000)/65536 --year
select (#iDate&0xff00)/256 --Month
select (#iDate&0xff) --Date
& is an operator doing bitwise AND. "|" is bitwise OR. See here and here. Also see here for an explanation on using bitwise AND/OR to store multiple number values in a single number column.
This part:
#iDate&0xfff0000
will "mask", or eliminate/replace-with-zeros, the portion of iDate that isn't from 256^2. Then you divide by 65536 -- which is simply reversing the original math of multiplying the year by 65536.
If the concept of bitwise AND is foreign, I'll give an example that DOESN'T WORK in decimal. Bitwise AND converts the whole thing to binary and then masks things (like IP subnetting, if you're familiar with that).
Anyway, consider a decimal number 20171012. If such a thing as a decimal-wise AND existed, it could look like 20171012&11110000. The "1" places are "keepers" and the "0" places are "throw-aways". If you stack them vertically, the result is to keep the values with a "1" beneath them and replace the values with a "0" beneath them with a "0".
number 20171012
dec-wise AND 11110000
result 20170000
now the result isn't 2017, so you'd have to divide by 10000 to get 2017.
For 20171012&1100 you have to use implied leading zeros:
number 20171012
dec-wise AND 00001100
result 1000
I probably would have converted to int by adding the year*10000 and month * 100 and day. Reverting back I would use a combination of integer division and MOD. But I think the bitwise AND is perhaps a bit more elegant (particularly for getting the month).
Based on your comment, I will include how I have converted dates to int and reverted back:
declare #dDate date = '2017-10-12'
declare #iDate int
set #iDate = year(#dDate) * 10000 + month(#dDate) * 100 + day(#dDate)
select #iDate
select 'year', #iDate/10000 -- basic integer division provides the year
select 'month', (#iDate % 10000)/100 -- combine modulo and integer division to get the month
select 'day', #iDate % 100 -- basic modulo arithmetic provides the day
returns:
20171012
year 2017
month 10
day 12
This is bit manipulation.
Bit Shifting
Decimal 3 = Binary 11
If we do a left shift (<<) 4 bits in 3 it will become 48 which is equal to binary 110000 <- 4 zero bits added due to left shift
But since we don't have bit shifting operators in T-SQL therefore we can do the math.
Left Shifting of n bits in number x = x * 2^n
Therefore, multiple a number with 256 is actually left shift 8 bits from that number (2^8 = 256).
Later on when you do bitwise OR between 2 numbers they actually "concatenate" the bits up.
For example, you need to concatenate 2 binary numbers, (3) 11 and (2) 10, the resultant number should be 1110 = 14
So first we'll do 2 left shift in 3 = 3 * 2^2 = 12 and then we will do bitwise OR this number with the next number
12 = 1100
2 = 0010
OR
---------------
14 = 1110
Your example is actually saving the whole date in an integer variable which is actually efficient way of saving a date.
I have an output of ranges from 150-0. I want to map those to 0 to 1. Or perhaps 0 to (some value less than 1 such as 0.5) where 150 is 0 and 0 is 1 ( or some values less than..).
Is this considered interpolation? What is the formula to derive these values? But preferably, is there a built-in StdLib function I can call?
Divide your number by the (Max - min). This would make 150 be 1 and 0 will be 0, with everything else a number in between. Now, to make it the opposite just do 1 - result.
If you need to map 0-1 to any custom range, you need to multiply range with MAX-MIN and then add MIN to it to get the exact number in range.
Formula will be MIN + (MAX-MIN)*value
where value is range in between 0-1;
MIN is number mapped to 0;
MAX is number mapped to 1;
mathematical range,for example:
greater or equal to 50 and smaller than 100 (>=50 && < 100)
smaller than 10 or greater than 40 (<10 || >40)
I have been thinking about how to represent mathematical range in a file and database, the range may be input by non programmer and I need to keep the input simple,but at another side, it also need to keep the input easy to convert to data and easy to check error input e.g.:"<10 || >100" seems the most simple but it is harder for me to parse the string to get the data,also need to consider input format error
I have been considering some input methods,using >=50 && < 100 as example,which is in key value form:
1.using 1 string to represent whole range:
<rangeInString>=50 && < 100</rangeInString>
2.separate 2 strings,one represent lower bound and another one represent upper bound,then parse each string in program:
<lowerBound> >=50 </lowerBound>
<upperBound> <100 </upperBound>
3.separate lower and upper bound,also separate the sign from number:
<lowerBound>
<sign> >= </sign>
<data>50</data>
</lowerBound>
<upperBound>
<sign> < </sign>
<data>100</data>
</upperBound>
4.separate lower bound and upper bound,also separate sign, and also separate the case that if includes the equal condition:
<lowerBound>
<sign> > </sign>
<isIncludeEqual>true</isIncludeEqual>
<data>50</data>
</lowerBound>
<upperBound>
<sign> < </sign>
<isIncludeEqual>false</isIncludeEqual>
<data>100</data>
</upperBound>
5.auto detect using "&&" or "||",e.g.:>= A with < B,if A < B,must be "&&" e.g.(>= 50 && <100),otherwise it is "||" e.g.(>= 100 || <50):
<A>
<sign> > </sign>
<isIncludeEqual>true</isIncludeEqual>
<data>50</data>
</A>
<B>
<sign> < </sign>
<isIncludeEqual>false</isIncludeEqual>
<data>100</data>
</B>
6.use a field "isAnd" to separate >=50 && < 100 (true) and <=50 || > 100 (false)instead of using field sign "<" and ">" :
<lowerBound>
<isIncludeEqual>true</isIncludeEqual>
<data>50</data>
</lowerBound>
<upperBound>
<isIncludeEqual>false</isIncludeEqual>
<data>100</data>
</upperBound>
<isAnd>true</isAnd>
7.other data model...
I need to consider somethings:
1.easy for non programmer to input
2.easy to convert or parse to data into program
3.easy to check error ,for example,parse string increase the complexity of converting data and checking incorrect format,also there may have other incorrect format,e.g.:<=50 && >100 should not be valid, I may allow auto detect using "&&" or "||" by the sign of input,but it may increase the complexity of the code
can anyone have idea?
Why "encode" it? There's no benefit or need and some hassle to use it.
Just store the exclusive range end values
low_end int,
high_end int,
You can then convert these raw values to useable expressions either in SQL or application code. You don't need to consider inclusive values because "n exclusive" === "n inclusive - 1" for low end and "n exclusive" === "n inclusive + 1" for high end.
Here's an SQL implementation:
where (low_end is null or col > low_end)
and (high_end is null or col < high_end)
If the range end values need to be floating point numbers, you'll need a little more:
low_end int,
low_inclusive boolean,
high_end int,
high_inclusive boolean,
And more code:
where (low_end is null or col > low_end + case when low_inclusive then 0 else 1 end)
and (high_end is null or col < high_end - case when high_inclusive then 0 else 1 end)
This is a good question, what about a combination of interval notation as suggested by Gordon and a given character for infinity. This combined with separate fields (or a parsing algorithm) could accomplish the task of defining any range.
For example, the range (3<x<20) could be written as (3,20). The range (x<=10 || x>30) could be written as the combination of
(-_,10],(30,_).
Where _ represents infinity. Or use the actual Infinity symbol character, ∞, Unicode U+221E.
This way would be pretty clear for those with a mathematics background, I believe, and would provide infinite flexibility.
I hope you find this helpful.
PostgreSQL does ranges natively.
The representation looks like this:
[low, high)
[ or ] = inclusive
( or ) = exclusive
Unbounded looks like this: [low-value, infinity]
http://www.postgresql.org/docs/9.4/static/rangetypes.html
Specifically addressing your options:
Why represent it in a format that you have to parse? A case could be made that you store it in a format that your code can parse, but what if you need to access it with a different programming language?
Same as 1.
Getting close, but you would need to subsume the bounds within a range object that includes && or ||. Also, no need for element, which is implied by "lower" and "upper" and could be replaced by an inclusive flag like you have in 4.
No need for
Unnecessary abstraction...it's just a range
That could work
Other data model:
The data is structured, so could work in json, xml, relational, or even as a set of semantic triples.
I am using SpreadsheetGear and I want to set format for a column as %. currently I am using following code:
worksheet.Cells[0, index].EntireColumn.NumberFormat = #"##0.00\%;[Red](##0.00\%)";
The above code apply formatting which converts:
6 = 6.00%
6.02 = 6.02%
.02 = .02%
But the problem is when I use this value and perform a math operation, excel consider actual value. Example: if a cell is showing 20.00% and I multiply it with 10 it gives me 200.00.
What I want is: When any numeric operation is performed & column type is defined as % system should divide the value by 100 and then it should perform the opeartion.
so if I multiple 24.00% with 10 it should give me .24.
Thank you
If you escape the % (\%), Excel just adds the % and does not adjust the decimal. For example, the following code
//with escape
cells[0, 0].NumberFormat = #"##0.00\%;[Red](##0.00\%)";
cells[0, 0].Value = 0.24;
//with no escape
cells[1, 0].NumberFormat = #"##0.00%;[Red](##0.00)%";
cells[1, 0].Value = 0.24;
//with no formatting
cells[2, 0].Value = 0.24;
cells[0, 1].FormulaR1C1 = "=RC[-1]*100";
cells[1, 1].FormulaR1C1 = "=RC[-1]*100";
cells[2, 1].FormulaR1C1 = "=RC[-1]*100";
will produce
0.24% 24 //with escape
24.00% 24 //with no escape
0.24 24 //with no formatting
Your problem is a math problem.
24% x 10 = 240%
240% = 2.4
"so if I multiple 24.00% with 10 it should give me .24"
If all you are trying to do is show a different output, then the system should interpret multiplying by 10 as removing the % formatting. This is because 24% and .24 are the same numbers. You are basically asking the machine to do multiplication incorrectly. Which is fine if that is what your client really really wants.