bond two quarries - c#

SELECT MAX(SerNo) FROM [DeviceList]
SELECT substring (SerNo) FROM [DeviceList]
there are two queries
the first query is gest Max Number in the column, the Second one is Split First 3 Character from that selected number. the number is like "SE001", "SE002".
My requirement is Get max number and split first two string from it (the result is 001 only). need one query for this help me.

just combine MAX() and SUBSTRING() together
FROM [DeviceList]

SELECT RIGHT(SerNo, 3) FROM (SELECT MAX(SerNo) [Serno] FROM [DeviceList])
Should do the trick, even though a little more info would be appreciated


Format the number of decimals based on another column SQL

Overall question:
I have a table in my SQL Server with 3 columns (ID, Value, Decimal).
So what I want to do in theory is SELECT the column Value and it should be formatted to the number of decimals specified in that same row.
What I've tried:
SELECT CONVERT(Decimal(18,s.Decimal), s.Value ) FROM Table s
and similar queries with CAST and FORMAT
What I want:
ID Value Decimal
1 500.754254 2
When I run the query it must return Value as '500.75'
Extra info:
I am displaying the result in a WPF (C#) datagrid, and if it will be easier to format there, I don't mind. I just don't know how to.
This is a presentation issue, and as such, should be handled by the presentation layer. c# have very simple ways to format the output, but as you wrote in your question, you would like to know how to do it in T-SQL - so here is one option, using cast, charindex and left.
First, create and populate sample table (Please save us this step in your future questions):
id int identity(1,1),
[Value] decimal (18,10),
[Decimal] int
INSERT INTO #T ([Value], [Decimal]) VALUES
(500.754254, 2),
(50.75636, 3),
(12345.7254, 1),
(1.424, 4)
The query:
LEFT(CAST([Value] As varchar(18)), CHARINDEX('.', CAST([Value] As varchar(18))) + [Decimal]) As [Left]
ID Value Decimal Left
1 500,7542540000 2 500.75
2 50,7563600000 3 50.756
3 12345,7254000000 1 12345.7
4 1,4240000000 4 1.4240
We can try using FORMAT here:
SELECT FORMAT(123.123456, '##.' + LEFT('##########', Decimal))
FROM yourTable;
The trick above is that the width of the decimal component of the mask is determined by the value of the Decimal column. If you expect to need more than 10 decimal places, then just widen the ### string literal.
Another option might be using ROUND:
SELECT ROUND(Value, Decimal)
FROM yourTable;
However, this sort of formatting is usually best handled in your presentation layer. So, I would probably be looking to WPF/C# for a solution.

How can i find duplicate name/value in SAME row? is it possible to count it?

for exmaple there is 1 colonm named "Name" and i am entering name "David David" (twice) so if i search it with count(*) where name="David" , it returns 1 and is that possible to get the count as 2? how is that possible? i am also searching the same name/value in different rows and tables too and finally adding the count so please help
This will work in MS Access.
SELECT Sum((Len([Name])-Len(Replace([Name],'David','')))/Len('David')) AS name_count
WHERE YOUR_TABLE.Name Like "*David*";
How about having a count of "David David" and adding this onto your other count. Would that achieve what you're after?
you can use group by
COUNT (Name) as count
FROM tablename
WHERE Name='Devid'

workaround ORA-01795 error programmatically

I am getting a ORA-01795: maximum number of expressions in a list is 1000 error for my project that I wish to solve in the code. Basically I am working with 2 databases (one sql server, one oracle) both of which I only have read only access (creating temp tables is not possible).
What i do is select a lists of IDs from DB 1, then search for the information corresponding to those IDs from DB2. This is where I use the 'IN' Clause, which contains more than 1000 IDs. I want to know how I can solve this programmatically- this is what i have so far (pseudo code, my real code is much more complex with too many wrappers):
1. "select ID from table 1 where some condition." [Database 1 conn]
2. string IDs = "";
while (
IDs += rset.getcolumn("ID");
IDs += ", ";
ID.Remove(ID.length - 2); //at this point I have all IDs separated by a comma.
3. "select * from table 2 where ID IN (" +IDs+ ")" //this is where the error occurs.
Is there a workaround to this? One of the idea I have is to split the IDs string into a string array with 999 IDs in each cell, then have a for loop in the sql query string to add in OR conditions, but am having trouble with the code.
There are two pretty standard solutions, 1 being a temporary table if you have rights to create one and then doing a where id in ( select id from temptable). The second is similar to what you suggest, do 999 and then add a union all for the next 999 and so on.
select * from t1 where Id in (1,2 ... ,1000)
union all
select * from t2 where Id in (1001,1002,...)

How To Find Out How Many Zero Present And Display The Zero In SQL Table [duplicate]

This question already has an answer here:
SQL Identity with leading padded zeros
7 answers
SQL Query For Create Table
(ID int,
Value1 int,
Value2 int,
SQL Query For Insert Value
ID Value1 Value2
1 200 300
Query To Display 00 In Result
SELECT RIGHT('00' + CAST([ID] AS varchar(5)) , 3),Value1,Value2
Now The Output Is
001 200 300
My Id Value Is maximum 5 Digit
Example 00001,00010,00100,01000,10000
Now i Want To Display The Result Like
I Need A Query Automatically Find Out How Many Zero Present Before The Number
Example 00001 This Value Have 4zero , 00010 This Value Have 3 Zero.
How To Use This Query
SELECT RIGHT('00' + CAST([ID] AS varchar(5)) , 3),Value1,Value2
For Find Out How Many Zero Present And Display The Result With zero
Numeric data types don't contain leading zeros. If you need to store leading zeros, you need to use a character datatype.
If you do that, however, to store numbers without leading zeros, please note that sorting on a character datatype field is done lexicographically, so you might end up with something like this:
Also, you should note the difference between data storage and data display. You could well store numbers without leading zeros, but make your application display them containing leading zeros!
As other people have noted, something like
will help you select the content of a numeric field as a string with leading zeros.
You could also do that in C# using
String.Format("{0:d3}", fieldvalue);
You are basically saying "I want to store a non-integer value in an integer column, how do I do that?"
Now, the obvious answer is of course: you don't.
The real question is, why do you think you want this? If (as I suspect) it is for later display purposes, then forget about inserting invalid data and focus on the real issue: how do I display my data the way I want to?
And the answer to that is simply": anywhere but in your database :)
You can change your integer to a string before displaying, either in your select statement, or further on in your front end.
You'd have to format the number manually. One way to do that:
select right('00' + cast(ID as varchar(3)),3)
SELECT RIGHT('00' + CAST([ID] AS varchar(5)) , 3)
FROM test
The 3 is the number of characters you want total in the output display
First you can't insert the value 00002 or anything like that with zeros (only in int type)... so the concept is fully wrong... or else change your data type to varchar then you can do these things...
if you are using varchar then simply use the following function

How to declare variables in select statement

Please look at this query.
select name form table
Let suppose this returns 100 name records.
I want to store each record in variable and then use this variable in another select
statement.Perhaps I have to do it with inner select but I do not know how.
How this is possible ?
I am working on three complex queries with complex joins I use the code of product and pass them to queries .If first query return the result fine else I move to second query else to third on.I have done this in c# very easily using loops but it is taking very much time(About 5 minutes for 7000 records)
Now I shall store the code in variable and pass it to first query in SQL if count of it result is greater than 0 fine else I move to 2nd else to third one. And so I shall iterate to all productCode Records. `select * from productsale where productname like' %';
Is the a reason you're not using JOIN's? That is usually the best way to use a value in one table to extract values from other tables.
Something like
select name , dept from name_table nt, dept_table dt
where =
Otherwise you're looking to use cursors. There are a lot of good tutorials on the web, and you may want to find a tutorial specific the DB version you are using.
I hope this helps.