SQL Datediff on multiple levels [closed] - sql

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a lot of issues using the datediff function in SQL Server.
I have two tables that I want to join based on the time gap between observations; the tables contain arrival times of shipments as follows yyyy-mm-dd hh:mm:ss.
I found a different question of which the answer will (hopefully) help me with the join bit. The code provided by said example states the following, and this is where the struggles begin.
Abs(DateDiff("d", pi.Product_Interest_Date, l.Lead_Date)) AS Date_Gap
Source: Join two tables on the same date or closest date (before or after)
However, I need to find the date gap between values based on the full date, so from year to seconds. Am I correct in understanding that the datediff function only accepts one level (i.e. only day or minute), or am I doing something wrong?
If the former is the case, is there an easy way to do what I want to do?

First, the syntax in SQL Server for days is:
abs(DateDiff(day, pi.Product_Interest_Date, l.Lead_Date)) AS Date_Gap
The syntax that you are using is MS Access syntax.
SQL Server does not have an interval type. You can get the difference in seconds and then convert to another unit. For instance, decimal days is:
abs(DateDiff(day, pi.Product_Interest_Date, l.Lead_Date)) / (24.0 * 60 * 60) AS Date_Gap
You don't provide sample data and desired results that describes the exact results you want, but this should put you on the right track. If this doesn't fully answer your question, then you should ask a new question with sample data, desired results, and a clear explanation of the logic.

Related

Conversion error when comparing field with dates [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 months ago.
Improve this question
Can someone tell me what I am possibly doing wrong in this query? I am getting the error message
Conversion failed when converting date and/or time from character
string
So I think it does not like the DATEADD part, but I have used that before (not as part of a join) and it worked fine. I can't figure out any other way to filter out for just the past month though. Thank you!
SELECT ----
FROM dbo.LocationRevenues AS lr
INNER JOIN dbo.Subscriptions AS sub
ON (lr.order_number_id = sub._order_number_id_)
WHERE sub._starts_at_ BETWEEN DATEADD(MONTH, -1, GETDATE()) AND GETDATE()
AND lr.subchannel_code = 'Channel'
AND lr.location_name != 'LocationA';
This script will find the problem for you.
SELECT sub.starts_at_
FROM Subscriptions sub
WHERE ISDATE(sub.starts_at_) = 0
Add another column such as the PK to the select list to help track down the actual row.

DATE BETWEEN issues [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Trying to select data only between a range.
select ITM_NBR,
TOT_IVO_ITM_QTY,
Count(*)
FROM dataset
WHERE
bus_dt BETWEEN '2-14-2020' AND '2-15-2021'
Failed conversion to numeric value. Tried without single ticks and it returned zero rows which makes me believe that the column is stored as a VARCHAR. Tried Casting the bus_dt column into a date format.
CAST(bus_dt AS DATE FORMAT 'mm/dd/yyyy') BETWEEN 2/14/2020 AND 2/15/2021
Again failed conversion.
I feel as if I’ve tried every combination and can’t get anything to work of casting and putting the date values in yyyy-mm-dd, mm/dd/yyyy, etc. formats.
And also when I HELP VIEW to see the column type I get “?”.
Kind of at a loss right now.
After I thought I tried everything I figured it out....
I was not formatting my date literals correctly .... faceplam
DATE'yyyy-mm-dd'....

How to you use Max and Min expression in Ms Access SQL? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
CrimeData Table for 12 months
Crime Took place in Easternmost
I need to find the following:
Q.7 What type of Crime takes place in the …
(a) Easternmost ………………..
(b) Westernmost ………………..
(c) Northernmost………………..
(d) Southernmost………………..
I tried to find the crime took place in the Easternmost using the following SQL code
SELECT Max(CrimeData.Easting) AS MaxOfEasting, CrimeData.Type
FROM CrimeData
GROUP BY CrimeData.Type;
but I got more than one crime and also other Easting numbers. Can you please tell me if there are other good ways to find the solution.
Please see the attached pictures :)
Rather than using Max/Min, have a look at the TOP keyword in SQL. Some SQL might look like:
SELECT TOP 1 CD.*
FROM CrimeData CD
ORDER BY CD.Easting DESC;
Regards,

Sql databases select command [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I am new to databases. And our teacher gave us pretty hard assignment. There are two tables. First table nickname is abilities(of superhero's:) ) and second table name superheros.
We have to select nick of Superhero and his average(medial) range for those who has two abilities?
Image of both tables:
Original here: http://postimg.org/image/85pqbc47n/
I will not give you solution - after all, it's homework and you have to learn something :) But I can give you an advice - try to do one task at a time
first, find those superheroes who has only 2 abilities (actually, you can do this by quering only table with abilities)
second - try to find average range of abilities for all superheroes (here you'll need join)
combine your queries
take a look at join, group by, count and having
Don't feel bad if you can't write it at first attempt, your query is not super easy, but 'm sure you can do this.
You can use HAVING and AVG() for this:
SELECT s.NickName, AVG(a.Range)
FROM abilities a
JOIN superhero s
ON a.ID_SuperHero = s.ID_SuperHero
GROUP BY s.NickName
HAVING COUNT(DISTINCT a.Abilities > 1)

Display endless amount of data in sql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
If I have an endless amount of data, can I display all of it in sql?
I know there is obviously select *, but then it will never complete.
Is there a command for this?
You can use TOP to select subset of total records
SELECT TOP 100 * from table
This selects top 100 records.
By using Order By clause , you can specify the basis on which subset of records is returned.
Now if you are asking about limits of Sql Server database management system then please see this link - Maximum Capacity Specification of Sql Server
Eg
Max Databases per instance of SQL Server ( both 32 bit and 64 bit ) = 32,767
Usually, you will prefer to use some kind of paging, since you cannot actually show "endless amount of data" in user-friendly way on application.