how to transform sql command with max function into int value in ASP
Give it an alias, something like MaxId
:
SqlCommand command = new SqlCommand("Select max(ID) AS MaxId from otazky", connection);
Then you can select it with this alias.
int id = reader.GetInt32(reader.GetOrdinal("MaxId"));
Equivalent of Math.Min & Math.Max for Dates?
There is no overload for DateTime values, but you can get the long value Ticks
that is what the values contain, compare them and then create a new DateTime value from the result:
new DateTime(Math.Min(Date1.Ticks, Date2.Ticks))
(Note that the DateTime structure also contains a Kind
property, that is not retained in the new value. This is normally not a problem; if you compare DateTime values of different kinds the comparison doesn't make sense anyway.)
Find max value of two (or more) properties in list
If you do
int max = list.Max(elem => Math.Max(elem.Nr, elem.OtherNr));
it's still a single-liner but only iterates through the list once. I'd take the single-linedness over the probable slight reduction in efficiency from writing it out by hand.
(Also, don't you need a cast from double
to int
somewhere in there?)
math.min and math.max vs sort for efficiently finding values in a list
Sorting the items means that you won't only find out which one is the largest and smallest, you will also arrange all items in between according to value. Although it's simple to do, it's still extra work that you don't have to make the computer do.
You can use the Min
and Max
methods to get what you want just as easily as sorting the list:
Dim highest As Decimal = listNumber.Min()
Dim lowest As Decimal = listNumber.Max()
How can I include null values in a MIN or MAX?
It's a bit ugly but because the NULL
s have a special meaning to you, this is the cleanest way I can think to do it:
SELECT recordid, MIN(startdate),
CASE WHEN MAX(CASE WHEN enddate IS NULL THEN 1 ELSE 0 END) = 0
THEN MAX(enddate)
END
FROM tmp GROUP BY recordid
That is, if any row has a NULL
, we want to force that to be the answer. Only if no rows contain a NULL
should we return the MIN
(or MAX
).
What a special order MSSQL takes to save .net byte[] data?
BitArray writes the various bits in order as if you were reading, ie. the first bit is bit-7 of the first byte, the eight bit is bit-0 of the first byte, the 9th bit is bit-7 of the second byte etc. So the minimal solution is that you have to invert the bitness (instead of power(2, i)
, do power(2, 64 - i)
. Of course, as you've observed, this is very limited. One option, that will work up to your 64 bits, is to simply use a bigger data type - bigint
. However, for anything larger than 64 bits, you're back where you started.
For a more general solution, you want to use the binary field on a per-byte basis. You can use the substring
function for this.
select
substring(binaryValue, ceiling(cast(@bit as decimal) / 8), 1)
& power(2, @bit % 8);
This gives you a zero if the specified bit is not set, and a non-zero value if it is.
The key here is that the string functions actually work for binary / image as well, so you can eg. access any byte at will just by using substring :)
Related Topics
Safely Rename Tables Using Serial Primary Key Columns
Constraint Defined Deferrable Initially Immediate Is Still Deferred
Getting All Buildings in Range of 5 Miles from Specified Coordinates
A Beginner'S Guide to SQL Database Design
Or Is Not Supported With Case Statement in SQL Server
What's the Difference Between Varchar and Char
MySQL Search and Replace Some Text in a Field
MySQL on Duplicate Key Update For Multiple Rows Insert in Single Query
What MySQL Database Tables and Relationships Would Support a Q&A Survey With Conditional Questions
Difference Between Natural Join and Inner Join
SQL How to Make Null Values Come Last When Sorting Ascending
How to Turn Identity_Insert on and Off Using SQL Server 2008
SQL Server - Stop or Break Execution of a SQL Script
Physical Vs. Logical (Hard Vs. Soft) Delete of Database Record