SQL Select Where Date <= To date - sql

Hi I am a newbie in SQL syntax.
I would like to have a query results in SQL using the below syntax.
Select * from tblSales where duedate <= todate
But when i run it was an error.
Please advise me for the correct syntax.
Thanks

In your question the RDBMS is not mentioned..
For SQL Server your query should be
Select * from tblSales where duedate <= GETDATE()
where GETDATE() return the current system date and duedate should be a date datatype.
If you are using oracle to get current system date you can use SYSDATE

select * from(
select datediff(day,getdate(),Duedate )as Diff,
SInumber ,InvoiceNo,Customer ,Tradedate,PaymentTerms,Duedate,Amount
from tblSales )as D where DIFF <= '10'
This is the Query i want..and it solved!

You could try this:
SELECT * FROM tblSales WHERE DATE(duedate) <= CURDATE();
I don't test it yet. Hope it works

Related

sql data not returning based on date

i have a column in my database wich stores a date as date/time. I have a sql query to select all records where the date matches the one in the query. but it is not returning any data.
select Name,Dateadded from table
this returns results like
Bob Smith 2009-12-11 09:35:53.000
I changed my query to be the below and no results are returned.
select Name,Dateadded from table where dateadded = '2009-12-11'
I tried the converting to date and still no luck, I have to enter in a date between query to get it working
SELECT
Name, convert(varchar(10), Dateadded , 103)
FROM
table
can anyone tell me where I'm going wrong? I have tried using 'like' and still does not work if I do 'between' two date ranges it works
It may be like this:
WHERE Dateadded >= '2014-07-24' AND Dateadded < '2014-07-25'
WHERE Dateadded >= '2014-07-24' AND Dateadded < DATEADD(dd, 1, '2014-07-24')
or with convert:
WHERE convert(date,Dateadded) = '2014-07-24'
and also you can do something like that
WHERE DAY(Dateadded) = 24 AND MONTH(Dateadded) = 07 AND YEAR(Dateadded) = 2014
Convert datetime to date in your where clause:
select Name,Dateadded
from table
where Convert(date, dateadded) = '2009-12-11'
Casting to date as in other answers will work. For performance, it may be better (depending on indexes) to use:
select Name,Dateadded
from [table]
where dateadded >= '2009-12-11'
and dateadded < '2009-12-12'
Use = only when you expect exact match. In your case you can use :
select Name,Dateadded from table where dateadded like '2009-12-11%'
You can try
select Name,Dateadded from table where date(dateadded) = '2009-12-11'
as you are trying to match only date.
Note : after seeing comments , it seems you are using sql server 2012, above works in mysql not sure about sql server 2012
The following codes will work:
SELECT Name,Dateadded FROM table WHERE Dateadded LIKE '2009-12-11%'
or
SELECT Name,Dateadded FROM table WHERE Dateadded >= '2009-12-11 00:00:00' AND Dateadded <= '2009-12-11 23:59:59'
or
SELECT Name,Dateadded FROM table WHERE Dateadded BETWEEN '2009-12-11 00:00:00' AND '2009-12-11 23:59:59'

Display a date that is the same as today in SQL Server [duplicate]

I have a table TEST with a DATETIME field, like this:
ID NAME DATE
1 TESTING 2014-03-19 20:05:20.000
What I need a query returning this row and every row with date 03/19/2014, no matter what the time is. I tried using
select * from test where date = '03/19/2014';
But it returns no rows. The only way to make it work that I found is to also provide the time portion of the date:
select * from test where date = '03/19/2014 20:03:02.000';
use range, or DateDiff function
select * from test
where date between '03/19/2014' and '03/19/2014 23:59:59'
or
select * from test
where datediff(day, date, '03/19/2014') = 0
Other options are:
If you have control over the database schema, and you don't need the
time data, take it out.
or, if you must keep it, add a computed column attribute that has the time portion of the date value stripped off...
Alter table Test
Add DateOnly As
DateAdd(day, datediff(day, 0, date), 0)
or, in more recent versions of SQL Server...
Alter table Test
Add DateOnly As
Cast(DateAdd(day, datediff(day, 0, date), 0) as Date)
then, you can write your query as simply:
select * from test
where DateOnly = '03/19/2014'
Simple answer;
select * from test where cast ([date] as date) = '03/19/2014';
I am using MySQL 5.6 and there is a DATE function to extract only the date part from date time. So the simple solution to the question is -
select * from test where DATE(date) = '2014-03-19';
http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html
This works for me for MS SQL server:
select * from test
where
year(date) = 2015
and month(date) = 10
and day(date)= 28 ;
select * from test
where date between '03/19/2014' and '03/19/2014 23:59:59'
This is a realy bad answer. For two reasons.
1.
What happens with times like 23.59.59.700 etc.
There are times larger than 23:59:59 and the next day.
2.
The behaviour depends on the datatype.
The query behaves differently for datetime/date/datetime2 types.
Testing with 23:59:59.999 makes it even worse because depending on the datetype you get different roundings.
select convert (varchar(40),convert(date , '2014-03-19 23:59:59.999'))
select convert (varchar(40),convert(datetime , '2014-03-19 23:59:59.999'))
select convert (varchar(40),convert(datetime2 , '2014-03-19 23:59:59.999'))
-- For date the value is 'chopped'.
-- For datetime the value is rounded up to the next date. (Nearest value).
-- For datetime2 the value is precise.
use this
select * from TableName where DateTimeField > date() and DateTimeField < date() + 1
Try this
select * from test where Convert(varchar, date,111)= '03/19/2014'
you can try this
select * from test where DATEADD(dd, 0, DATEDIFF(dd, 0, date)) = '03/19/2014';
There is a problem with dates and languages and the way to avoid it is asking for dates with this format YYYYMMDD.
This way below should be the fastest according to the link below. I checked in SQL Server 2012 and I agree with the link.
select * from test where date >= '20141903' AND date < DATEADD(DAY, 1, '20141903');
Bad habits to kick : mis-handling date / range queries
You can use this approach which truncates the time part:
select * from test
where convert(datetime,'03/19/2014',102) = DATEADD(dd, DATEDIFF(dd, 0, date), 0)
-- Reverse the date format
-- this false:
select * from test where date = '28/10/2015'
-- this true:
select * from test where date = '2015/10/28'
Simply use this in your WHERE clause.
The "SubmitDate" portion below is the column name, so insert your own.
This will return only the "Year" portion of the results, omitting the mins etc.
Where datepart(year, SubmitDate) = '2017'
select *, cast ([col1] as date) <name of the column> from test where date = 'mm/dd/yyyy'
"col1" is name of the column with date and time
<name of the column> here you can change name as desired
select *
from invoice
where TRUNC(created_date) <=TRUNC(to_date('04-MAR-18 15:00:00','dd-mon-yy hh24:mi:ss'));
Test this query.
SELECT *,DATE(chat_reg_date) AS is_date,TIME(chat_reg_time) AS is_time FROM chat WHERE chat_inbox_key='$chat_key'
ORDER BY is_date DESC, is_time DESC
select * from invoice where TRANS_DATE_D>= to_date ('20170831115959','YYYYMMDDHH24MISS')
and TRANS_DATE_D<= to_date ('20171031115959','YYYYMMDDHH24MISS');
SELECT * FROM test where DATEPART(year,[TIMESTAMP]) = '2018' and DATEPART(day,[TIMESTAMP]) = '16' and DATEPART(month,[TIMESTAMP]) = '11'
use trunc(column).
select * from test t where trunc(t.date) = TO_DATE('2018/06/08', 'YYYY/MM/DD')

Select records after specific date in SQL

I have a column in my database called "begin_date".
I am trying to select records where the begin_date are greater than a specific date. I put
select * from Table_Name
where begin_date >= '1/1/2014'
However, it returns error message "String to date conversion error".
I am not sure how to modify the query to make it work?
Thanks!
Try using ISO (8601) standard date formats:
select *
from Table_Name
where begin_date >= '2014-01-01';
select *
from table
where data >= '01/01/2014'

How to get DATE from DATETIME Column in SQL? [duplicate]

This question already has answers here:
How to return only the Date from a SQL Server DateTime datatype
(46 answers)
Closed 7 years ago.
I have 3 columns in Table TransactionMaster in sql server
1) transaction_amount
2) Card_No
3) transaction_date-- datetime datatype
So, I want to fetch SUM of transaction_amount where Card_No=' 123' and transaction_date= todays date.<----- excluding time IN SQL
Simply cast your timestamp AS DATE, like this:
SELECT CAST(tstamp AS DATE)
SQLFiddle Demo
In other words, your statement would look like this:
SELECT SUM(transaction_amount)
FROM mytable
WHERE Card_No='123'
AND CAST(transaction_date AS DATE) = target_date
What is nice about CAST is that it works exactly the same on most SQL engines (SQL Server, PostgreSQL, MySQL), and is much easier to remember how to use it.
Methods using CONVERT() or TO_DATE() are specific to each SQL engine and make your code non-portable.
You can use
select *
from transaction
where (Card_No='123') and (transaction_date = convert(varchar(10),getdate(),101))
Use Getdate()
select sum(transaction_amount) from TransactionMaster
where Card_No=' 123' and transaction_date =convert(varchar(10), getdate(), 102)
use the following
select sum(transaction_amount) from TransactionMaste
where Card_No = '123' and transaction_date = CONVERT(VARCHAR(10),GETDATE(),111)
or the following
select sum(transaction_amount) from TransactionMaste
where Card_No = '123' and transaction_date = CONVERT(VARCHAR(10), GETDATE(), 120)
Try this:
SELECT SUM(transaction_amount) FROM TransactionMaster WHERE Card_No ='123' AND CONVERT(VARCHAR(10),GETDATE(),111)
The GETDATE() function returns the current date and time from the SQL Server.

Query datetime in SQL without time

I'm trying to write a SQL query which should just pick the count with specific date not time.
select count(*) from xyz where time='2010-01-21'
but it is not returning any results.
For SQL Server 2008, you should be able to use the date data type:
select count(*) from xyz where cast(time as date) = '2010-01-21'
If you have a date time field, and you wanted to match a date:
select count(*) from xyz where time BETWEEN '2010-01-21' AND '2010-01-22'
MYSQL Date Time ref
Try (MySQL)
SELECT COUNT(*) FROM xyz WHERE DATE(datetime_col) = '2010-01-21'
in T-SQL (MSSQL) (kinda ugly, but should work):
SELECT * FROM xyz WHERE CAST(CONVERT(varchar(8), datetime_col, 112) AS DATETIME) <= '2011-01-21'