I have a string 2021-02-23T06:58:51 that I want to check if it is greater than another date. When I do the below I get an error. Tried CAST still the same error.
select convert(smalldatetime, '2021-02-23T06:58:51') > convert(smalldatetime, GETDATE())
Started executing query at Line 1
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '>'.
Total execution time: 00:00:00.040
As I said in the comments, columns need to be defined as a scalar expression. The expression you have is not a scalar expression, it is a boolean expression:
convert(smalldatetime, '2021-02-23T06:58:51') > convert(smalldatetime, GETDATE())
This doesn't tell SQL Server what to display, and would be an expression you would normally find in the WHERE. I.e. Display rows where the value of {column} is greater than the current date and time.
What you likely want is a CASE expression or IIF:
SELECT CASE WHEN '2021-02-23T06:58:51' > GETDATE() THEN 1 END 0 END;
SELECT IIF('2021-02-23T06:58:51' > GETDATE(),1,0);
Is this what you want?
select IIF(convert(smalldatetime, '2021-02-23T06:58:51') > convert(smalldatetime, GETDATE()), 'True','False')
I have column of type varchar in a table and have different type of date format in it. I want to compare it with specific format of date. If it is true then those rows should be returned only. Format of the date is 'yyyymmdd'
e.g., 20200831 = 'yyyymmdd'
dates are in below format
20200831
31/Aug/2020
08-31-2020
2020-08-31
These are few date format which are present in the table.
If I understand the question correctly, a possible solution is the following statement:
SELECT DateText
FROM (VALUES
('20200831'),
('31/Aug/2020'),
('08-31-2020'),
('2020-08-31')
) v (DateText)
WHERE CONVERT(varchar(8), TRY_CONVERT(date, DateText), 112) = DateText
Result:
DateText
20200831
SELECT *
FROM tableName
WHERE ISDATE(dateCol) = 1 AND ISNUMERIC(dateCol) = 1;
ISDATE() function returns 1 if column value is valid SQL Server date
and ISNUMERIC() returns 1 if column value is valid number, so 20200831 satisfies both conditiion.
If you are using SQL Server 2012 or later, then the TRY_CONVERT function is one option here:
SELECT
dt,
TRY_CONVERT(datetime, dt) AS dt_out
FROM yourTable
WHERE
TRY_CONVERT(datetime, dt) = '2020-08-31';
Demo
Data:
WITH yourTable AS (
SELECT '20200831' AS dt UNION ALL
SELECT '31/Aug/2020' UNION ALL
SELECT '08-31-2020' UNION ALL
SELECT '2020-08-31'
)
Good morning everyone,
Im having an issue that I have not seen before where the date parameter im using is totally ignored and the query is giving me ambigious results.
the code im using is
DECLARE #STARTDATE VARCHAR='03/07/2018'
Select
CONVERT(VARCHAR, CloseDateTime, 103 ) AS INSPECTION_DATE,
CallProperties.ID,
(CallProperties.UserRef) AS ENGINEER,
(CallProperties.CallRef) as ReferenceNo,
(CallProperties.CallTypeName) as JobType,
(SELECT2) AS VEN_POSTCODE,
(ADVANCED_oil.PARENTid) AS PARENTID,
ADVANCED_oil.Panel_CustDetails_C_EAddress,
(ADVANCED_oil.Adv_OilTestComplete) as OilTestTaken
from CallProperties left JOIN ADVANCED_oil ON (ADVANCED_oil.ParentID=CallProperties.ID) where Adv_OilTestComplete='Low oil'
OR Adv_OilTestComplete='Tube restriction'
OR Adv_OilTestComplete='No dip stick'
OR Adv_OilTestComplete='Vendor refused' AND CONVERT(VARCHAR, CloseDateTime, 103 )=#STARTDATE
its supposed to give me one result back but keeps giving me 7 rows, some of which do not have anything to do with the paramenter values....please help!!
Thankyou in advance
Your problem starts with the first line:
DECLARE #STARTDATE VARCHAR = '03/07/2018'
There are two major things wrong and one important but not major:
Dates should be DATES, not strings.
You have declared a string with one character.
You should always use proper ISO standard date formats
Change this to:
DECLARE #STARTDATE DATE = '2018-07-03';
Then do the comparison as a date:
CONVERT(DATE, CloseDateTime) = #STARTDATE
As far as I know, CONVERT() and CAST() to DATE are the only functions that still allow an index to be used.
EDIT:
Your where clause is also malformed. You probably intend:
where Adv_OilTestComplete IN ('Low oil', 'Tube restriction', 'No dip stick', 'Vendor refused') AND
CONVERT(DATE, CloseDateTime) = #STARTDATE
the main issue in you code in filter part
where Adv_OilTestComplete='Low oil'
OR Adv_OilTestComplete='Tube restriction'
OR Adv_OilTestComplete='No dip stick'
OR Adv_OilTestComplete='Vendor refused' AND CONVERT(DATE, CloseDateTime) = #STARTDATE
it has to be like this
where (Adv_OilTestComplete='Low oil'
OR Adv_OilTestComplete='Tube restriction'
OR Adv_OilTestComplete='No dip stick'
OR Adv_OilTestComplete='Vendor refused') AND CONVERT(DATE, CloseDateTime) = #STARTDATE
because And has higher priority than or you can check this reference
SELECT CONVERT(datetime,'17/05/2015 22:15:00',103)
output:
2015-05-17 22:15:00.000
I want include 2 column is Date+Time
Example: Colunm Date and Time
**Date** **Time**
17/05/2015 22:15:00
but Error Query
SELECT CONVERT(datetime,[Date]+' '+[Time],103) FROM LPTables
Conversion failed when converting date and/or time from character string.
Just add the time portion to the date portion:
SELECT DATEADD(ms, DATEDIFF(ms, '00:00:00', [Time]), CONVERT(DATETIME, [Date]))
FROM LPTables
This will give you accuracy to the millisecond.
Just enclose your columns with ().
SELECT CONVERT(datetime,([Date]+' '+[Time]),103) FROM LPTables
WHERE ISNULL([Date],'')!='' AND ISNULL([Time],'')!=''
Sample :
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')