Differences between Dates in MSSQL - sql-server-2008-r2

I have a situation where I need to get the differences between two columns.
Column 1: 01-OCT-13 10:27:15
Column 2: 01-OCT-13 10:28:00
I need to get the differences between those above two columns. I tried using '-' operator but the output is not in an expected way.
I need output as follows: 00-00-00 00:00:45
I can get it in Oracle using MOD function. But I am not sure about SQL Server.

You can use DATEDIFF
SELECT DATEDIFF(ss,startDate,endDate)
SQL FIDDLE
DATEDIFF

drop table #t1
create table #t1(clo1 datetime,col2 datetime)
insert into #t1 values('01-OCT-13 10:27:15','01-OCT-13 10:28:00')
select cast((col2-clo1) as time) from #t1
See Demo

Try the following
SELECT DATEDIFF (datepart, Column 1,Column 2)
Values for datepart:
mi : minute
ss,s: second
So,
SELECT DATEDIFF (s, Column 1,Column 2)
Refer this for more

Related

AVG Function in SQL

The two columns in the image are part of a table and I'm trying to get the AVG Sal depending on specific date. For example, the Avg sal for 1981. Because of the date, I can't get it. How do I do this? Thanks!
2 Columns
Try this option:
SELECT AVG(SAL) AS AVG_SAL
FROM yourTable
WHERE EXTRACT(YEAR FROM HIREDATE) = 1981;
More generally, to find averages for all years:
SELECT
EXTRACT(YEAR FROM HIREDATE) AS HIRE_YEAR,
AVG(SAL) AS AVG_SAL
FROM yourTable
GROUP BY
EXTRACT(YEAR FROM HIREDATE);
How about
SELECT AVG(SAL)
FROM MyTable
WHERE HIREDATE BETWEEN '1/1/1981' AND '12/31/1981'
Here we're using the BETWEEN operator which is pretty handy in SQL and is not limited to dates. You might also look through the Oracle documentation for some handy date and time functions.

Db2 convert rows to columns

I need the below results ..
Table :
Order postcode qnty
123 2234 1
Expected result:
Order 123
Postcode 2234
Qnty 1
SQL server:
Select pvt.element_name
,pvt.element_value(select order.postcode
from table name)up
unpivot (element_value for element_name in(order,postcode) as Pvt
How to achieve this in db2?
Db2 for IBM i doesn't have a built-in unpviot function.. AFAIK, it's not available on any Db2 platofrm...unless it's been added recently.
The straight forward method
select 'ORDER' as key, order as value
from mytable
UNION ALL
select 'POSTCODE', postcode
from mytable
UNION ALL
select 'QNTY', char(qnty)
from mytable;
A better performing method is to do a cross join between the source table and a correlated VALUES of as many rows as columns that need to be unpivoted.
select
Key, value
from mytable T,
lateral (values ('ORDER', t.order)
, ('POSTCODE', t.postcode)
, ('QNQTY', varchar(t.qnty))
) as unpivot(key, value);
However, you'll need to know ahead of time what the values you're unpivoting on.
If you don't know the values, there are some ways to unpivot with the XMLTABLE (possibly JSON_TABLE) that might work. I've never used them, and I'm out of time to spend answering this question. You can find some examples via google.
I have created a stored procedure for LUW that rotate a table:
https://github.com/angoca/db2tools/blob/master/pivot.sql
You just need to call the stored procedure by passing the tablename as parameter, and it will return a cursor with the headers of the column in the first column.

Ignore specific rows from a table

I have a table with data like this:
ND
10212121
10232323
10212323
212526
295652
232565
I would like make a select to all ND from this table excluding these starting with 10...using openquery to a oracle database.
Regards
In the following query I check the first two characters of the ND column and compare against 10 to see if they be equal. You did not mention whether or not ND is a numeric type, so I added a cast to varchar2 so that the substring will work.
SELECT ND
FROM yourTable
WHERE SUBSTR(CAST(ND AS varchar2(30)), 1, 2) <> '10'

Line numbering in result grid in MySQL Workbench

Is there any way to add some line numbers in the result grid in MySQL Workbench?
E.g. (red numbers):
I don't want to have to change the SQL query, which I know I can do using tricks like
SELECT #n := #n + 1 `Number of Submissions`, t.*
FROM (SELECT #n:=0) initvars,
( SELECT COUNT(*) AS count
FROM moocdb.submissions
GROUP BY user_id
ORDER BY count DESC
) t
I also don't want to have to export the results.
Not sure if that is a good question for SO, but anyway: no this is not possible. Nobody asked for that so far, so, file a feature request at http://bugs.mysql.com to have that in.
MySQL does not provide row_number like Microsoft SQL Server, Oracle, or PostgreSQL. Fortunately, MySQL provides session variables that you can use to emulate the row_number function.
SET #row_number = 0;
SELECT (#row_number:=#row_number + 1) AS num, col_1
FROM
Table

Excel parameter in t/sql SELECT TOP xxx without using SP

This is really only a short question, but I can't find the answer anywhere.
Is it possible to use an Excel parameter in a t/sql query as a SELECT TOP xxxx, where the parameter here defines how many rows are fetched, without using a stored procedure?
From sql 2005 and up you can write your query as:
Enter the following in your msquery:
{CALL sp_executesql (N'select top (#a) * from mytable', N'#a int', ?)}
I'm not sure about applying the Excel parameter, but to do this in T-SQL do:
select *
from (
select row_number() over (order by [Field]) as rowNum, *
from [myTable] ) s
where s.rowNum < #maxRows
If you are in Excel can you not use Concat to build up the sql query?
Concatenate("select top ", $D2, " from mytable where...")
and use the value of that cell as your tsql expression?
http://msdn.microsoft.com/en-us/library/aa188518(v=office.10).aspx