Redshift query between date - sql

I'm quite new to Redshift SQL.
select * from myredshift_tbl
where local_date between \'2016-01-01\' and \'2017-02-01\';
But got this error:
[amazon][500310] invalid operation syntax error at or near "\". I believe Redshift use single quote and I need to escape single quote.

If the column local_date is in date format, use:
select * from myredshift_tbl
where local_date between '2016-01-01' and '2017-02-01';
If the column local_date is timestamp:
select * from myredshift_tbl
where local_date between '2016-01-01 00:00:00' and '2017-02-01 23:59:59';

SELECT * FROM schemaName.TableName WHERE datetime > '2017-02-09
00:00:00' AND datetime < '2017-06-09 00:00:00';
The above query Works with Redshift to fetch all the entries in a table.
NOTE: The table I applied the query on had column/field 'datetime' of type 'timestamp'.
I tested this query on Redshift with the help of Workbench J.

Related

Google BigQuery Date(String format) failed to convert/cast as Date

I am trying to convert/cast the date in the big query into date format. My query is like this:
SELECT CAST(t.date AS date)
FROM `table` t;
But I got an Error code of Invalid date:'20151108'. it gave me different error date when I run the query.
Any thoughts?
try
SELECT PARSE_DATE('%Y%m%d', t.date) FROM table t

SQL query not returning data

I am trying to retrieve data from a Intersystems Cached database using a where clause with a timestamp, but nothing works.
Query:
select *
from dbo.iSkillsetStat
where Timestamp >= '2014-07-29 00:00:00'
Error:
ERROR: [SQLCODE: <-4>:<A term expected, beginning with one of
the following: identifier, constant, aggregate, %ALPHAUP, %EXACT,
%MVR, %SQLSTRING, %SQLUPPER, %STRING, %UPPER, $$, :,
+, -, (, NOT, EXISTS, or FOR>]
[Location: <Prepare>]
If I run the query without the timestamp all the data is returned.
Please any suggestions!
as TIMESTAMP is a reserved word, you should enclose it in double quotes "TIMESTAP"
In similar cases this worked for me:
select * from dbo.iSkillsetStat where Timestamp >= '2014-07-29T00:00:00'
Note the 'T' between the date and time values.
Try this:
select *
from dbo.iSkillsetStat
where Timestamp >= Convert(datetime, '2014-07-29 00:00:00')
Here you are missing 000 in your datetime 2014-07-29 00:00:00.
It should be like this 2014-07-29 00:00:00:000
instead of 2014-07-29 00:00:00
the field timestamp may be a reserved keyword and shouldn't be used for columnnames (have a look at https://www.drupal.org/node/141051 #728)
HINT:
if you field is of type TIMESTAMP convert the value before you compare it:
SELECT *
FROM dbo.iSkillsetStat
WHERE columnname >= TIMESTAMP('2014-07-29 00:00:00')

SQL Query taking between specific sysdate?

I am using Oracle DB 10g, I am trying to take data between 2 dates, in the database format of data is :
10/4/2013 9:04:38 AM
I try some sql queries but it gives error...
select * from test_table where test_execution_date between '9/2/2012' and '7/2/2013'
select * from test_table where test_execution_date between '9/2/2012 9:04:38 AM' and '7/2/2013 9:04:38 AM'
It gives always same error : ORA-01843: not a valid month
Try using
TO_DATE(thedatevalue,'MM/DD/YYYY')
You need to convert string to date
select * from test_table where test_execution_date between
TO_DATE('9/2/2012', 'DD/MM/YYYY') and TO_DATE('7/2/2013', 'DD/MM/YYYY')

Parsing date in sql query

I am using SQL Server 2005.
My Trade_Date column is of datatype datetime.
It has values as: 2/12/2013 11:59:00 PM , 2/13/2013 11:59:00 PM
i.e. different dates with time.
I want to compare date [only date] in where clause.
I am trying with following query:
select *
from foclosing
where CONVERT(varchar(11), Trade_Date) like '2/12/2013 %'
or
select *
from foclosing
where Trade_Date = CONVERT(datetime, '2/12/2013')
but both of these queries are not working.
What can be the issue?
select * from foclosing
where Trade_Date >= '20130212' AND Trade_Date < '20130213'
Use yyyymmdd which is safe for SQL Server
Don't apply functions to columns, then compare (your 1st query)See mistake 2 here: https://www.simple-talk.com/sql/t-sql-programming/ten-common-sql-programming-mistakes/
Don't compare locale-based dates as varchar (your 1st query)
Trade_Date has a time element which is why your 2nd query fails
In summary, a date conversion should not be used for examples like this: datetime is a range

Simple DateTime sql query

How do I query DateTime database field within a certain range?
I am using SQL SERVER 2005
Error code below
SELECT *
FROM TABLENAME
WHERE DateTime >= 12/04/2011 12:00:00 AM
AND DateTime <= 25/05/2011 3:53:04 AM
Note that I need to get rows within a certain time range. Example, 10 mins time range.
Currently SQL return with Incorrect syntax near '12'."
You missed single quote sign:
SELECT *
FROM TABLENAME
WHERE DateTime >= '12/04/2011 12:00:00 AM' AND DateTime <= '25/05/2011 3:53:04 AM'
Also, it is recommended to use ISO8601 format YYYY-MM-DDThh:mm:ss.nnn[ Z ], as this one will not depend on your server's local culture.
SELECT *
FROM TABLENAME
WHERE
DateTime >= '2011-04-12T00:00:00.000' AND
DateTime <= '2011-05-25T03:53:04.000'
You need quotes around the string you're trying to pass off as a date, and you can also use BETWEEN here:
SELECT *
FROM TABLENAME
WHERE DateTime BETWEEN '04/12/2011 12:00:00 AM' AND '05/25/2011 3:53:04 AM'
See answer to the following question for examples on how to explicitly convert strings to dates while specifying the format:
Sql Server string to date conversion
This has worked for me in both SQL Server 2005 and 2008:
SELECT * from TABLE
WHERE FIELDNAME > {ts '2013-02-01 15:00:00.001'}
AND FIELDNAME < {ts '2013-08-05 00:00:00.000'}
You can execute below code
SELECT Time FROM [TableName] where DATEPART(YYYY,[Time])='2018' and DATEPART(MM,[Time])='06' and DATEPART(DD,[Time])='14
SELECT *
FROM TABLENAME
WHERE [DateTime] >= '2011-04-12 12:00:00 AM'
AND [DateTime] <= '2011-05-25 3:35:04 AM'
If this doesn't work, please script out your table and post it here. this will help us get you the correct answer quickly.
select getdate()
O/P
----
2011-05-25 17:29:44.763
select convert(varchar(30),getdate(),131) >= '12/04/2011 12:00:00 AM'
O/P
---
22/06/1432 5:29:44:763PM
Others have already said that date literals in SQL Server require being surrounded with single quotes, but I wanted to add that you can solve your month/day mixup problem two ways (that is, the problem where 25 is seen as the month and 5 the day) :
Use an explicit Convert(datetime, 'datevalue', style) where style is one of the numeric style codes, see Cast and Convert. The style parameter isn't just for converting dates to strings but also for determining how strings are parsed to dates.
Use a region-independent format for dates stored as strings. The one I use is 'yyyymmdd hh:mm:ss', or consider ISO format, yyyy-mm-ddThh:mi:ss.mmm. Based on experimentation, there are NO other language-invariant format string. (Though I think you can include time zone at the end, see the above link).
if you have a type of datetime and you want to check between dates only ,,,use cast to select between two dates ....
example...
... where cast( Datetime as date) >= cast( Datetime as date) AND cast( Datetime as date) <= cast( Datetime as date)