Get Data between two dates using Number as Identifier - sql

I am trying to make an SQL query in MS SQL Server where I put an account number to search and it gives me the data between the two date ranges.
code looks like this
select *
from transactions
where accountNo1 = '2005457846' transaction_date between '15-01-2018' and '18-01-2018'
Apparently what am i not doing correctly it tells me
syntax error near transaction_date

You forget to add AND in the query.
select *
from transactions
where accountNo1 = '2005457846' and transaction_date between '15-01-2018' and '18-01-2018'

use " and " also look date format
select * from transactions where accountNo1 ='2005457846' and transaction_date between '15-01-2018' and '18-01-2018'

Related

Fetching records from SQL based on Month name

So this my table structure and data.
Now I want to filter data based on Month by ExpenseDate column.
So how can I achieve that?
I was trying
select * from tblExpenses where (ExpenseDate = MONTH('April'))
But it throws an error: "Conversion failed when converting date and/or time from character string."
Please help. Thank you.
You are putting month() on the wrong column. It can be applied to ExpensesDate:
select *
from tblExpenses
where month(ExpenseDate) = 4;
Note that month() returns a number, not the name of the month.
I think it is more likely that you want records from a particular April, not every April. This would be expressed as:
where ExpenseDate >= '2018-04-01' and ExpenseDate < '2018-05-01'
I think your where clause is just reversed I think you want this (and change the word to a number)
select * from tblExpenses where Month(ExpenseDate) = 4

ORA-01847 error when not using dates in the query

I'm trying to run this query in an Oracle 11g database:
SELECT * FROM BALANCE B
WHERE B.EMPLOY_ID = '0016'
AND B.PROJETCT_ID = '5TM-1305002.01.01.01'
but the following error is displayed:
ORA 01847 : day of the month must be between 1 and last day of the month
The EMPLOY_ID field is varchar2(4) and the PROJECT_ID is a varchar2(20).
I do not understand why the Oracle database is trying to convert the parameter values to date values. What is going on?
What happens if you try this?
SELECT * FROM BALANCE B
WHERE TO_CHAR(B.EMPLOY_ID) = '0016'
AND TO_CHAR(B.PROJETCT_ID) = '5TM-1305002.01.01.01'
It could be due to invalid data in one of date columns in table.
You can check your query with rownum condition, just to make sure that the where clause is correct or not.
If it is working then there is no because of B.EMPLOY_ID and B.PROJETCT_ID columns.

SQL find period that contain dates of specific year

I have a table (lets call it AAA) containing 3 colums ID,DateFrom,DateTo
I want to write a query to return all the records that contain (even 1 day) within the period DateFrom-DateTo of a specific year (eg 2016).
I am using SQL Server 2005
Thank you
Another way is this:
SELECT <columns list>
FROM AAA
WHERE DateFrom <= '2016-12-31' AND DateTo >= '2016-01-01'
If you have an index on DateFrom and DateTo, this query allows Sql-Server to use that index, unlike the query in Max xaM's answer.
On a small table you will probably see no difference but on a large one there can be a big performance hit using that query, since Sql-Server can't use an index if the column in the where clause is inside a function
Try this:
SELECT * FROM AAA
WHERE DATEPART(YEAR,DateFrom)=2016 OR DATEPART(YEAR,DateTo)=2016
Well you can use the following query
select * from Table1
WHERE DateDiff(day,DateFrom,DateTo)>0
AND YEAR(DateFrom) = YEAR(DateTo)
And here is the result:
Enjoy :D !

how make query about some of a field are equal?

I'm trying the code below
SELECT Sum(Price) FROM Faktor WHERE date=date
but it shows total of all price. I want to show the sum of per day like:
date ----- sum
2015/5/1 12345
2015/5/2 54124
I have tried below code too but get error:
SELECT date,Sum(Price) FROM Faktor WHERE date=date
[Err] 42000 - [SQL Server]Column 'Faktor.date' is invalid in the
select list because it is not contained in either an aggregate
function or the GROUP BY clause.
Try
SELECT [date],SUM([Price])
FROM Faktor
GROUP BY [date]
Not sure why you'd use date=date, so I left it out.
Just as the error message tells you, you need to use a group by clause and the date column needs to be in it.
SELECT date, SUM(Price)
FROM Faktor
WHERE date=date -- this looks a bit odd... maybe you want a range of dates or something?
GROUP BY date
With SQL Server all non-aggregated columns from the select statement needs to be grouped (unlike some versions of MySQL for instance).
SELECT
date
,SUM(Price)
FROM
Faktor
WHERE
--add date rules here if you have date criteria i.e. date >= 'someDate'
GROUP BY
date

how to get data whose expired within 45 days..?

HI all,
i have one sql table and field for that table is
id
name
expireydate
Now i want only those record which one is expired within 45 days or 30 days.
how can i do with sql query .?
I have not much more exp with sql .
Thanks in advance,
If you are using mysql then try DATEDIFF.
for 45 days
select * from `table` where DATEDIFF(now(),expireydate)<=45;
for 30 days
select * from `table` where DATEDIFF(now(),expireydate)<=30;
In oracle - will do the trick instead of datediff and SYSDATE instead of now().[not sure]
In sql server DateDiff is quite different you have to provide unit in which difference to be taken out from 2 dates.
DATEDIFF(datepart,startdate,enddate)
to get current date try one of this: CURRENT_TIMESTAMP or GETDATE() or {fn NOW()}
You can use a simple SELECT * FROM yourtable WHERE expireydate < "some formula calculating today+30 or 45 days".
Simple comparison will work there, the tricky part is to write this last bit concerning the date you want to compare to. It'll depend of your environment and how you stored the "expireydate" in the database.
Try Below:-
SELECT * FROM MYTABLE WHERE (expireydate in days) < ((CURRENTDATE in days)+ 45)
Do not execute directly! Depending of your database, way of obtaining a date in days will be different. Go look at your database manual or please precise what is your database.