I have a date column (in YYYY-MM-DD format) in a big query table. I am unable to apply a where clause to the date column. I am using the following queries:
SELECT * FROM [dataSet_Id.TableName] where CR_DT=DATE("2016-01-01")
SELECT * FROM [dataSet_Id.TableName] where CR_DT=DATE("2016-01-01") where CR_DT=20160101
So how do I do it?
I got it work, If I use Standard SQL Dialect instead of Legacy SQL
Sample queries to handle date in where clause:
SELECT * from demoschema.demotable where dob = date('2016-08-10');
SELECT * from demoschema.demotable where dob = '2016-08-11';
If you want to use Standard SQL Dialect, just go to Show options then you will find SQL Version field which is use for enabling Standard SQL. .Dialect.
If the type of your CR_DT column is String then:
SELECT * FROM [dataSet_Id.TableName] where CR_DT = '2016-01-01'
If the type of your CR_DT column is TIMESTAMP then:
SELECT * FROM [dataSet_Id.TableName] where DATE(CR_DT) = DATE(timestamp('2016-01-01'))
Related
Is it possible to use the LEFT() operator in MSSQL on a datetime.
I'm asking because this is my db:
In a SQL query i want now to SELECT only this objects WHERE Rueckmeldetatum = a date in form like (2023-01-27) so my string comperable has no time just a date. But with this SQL Query I'm getting no results:
SELECT TOP (1000) [KNR]
,[Rueckmeldedatum]
FROM [Fertigung].[dbo].[Box1Auswertung]
WHERE LEFT(Rueckmeldedatum,10) ='2023-01-27'
But normally or what i want to get is the 20th entry from the picture.
You should cast the datetime to date, then compare to a date literal:
SELECT TOP (1000) [KNR], [Rueckmeldedatum]
FROM [Fertigung].[dbo].[Box1Auswertung]
WHERE CAST(Rueckmeldedatum AS date) = '20230127';
Or, better yet, use this sargable version:
SELECT TOP (1000) [KNR], [Rueckmeldedatum]
FROM [Fertigung].[dbo].[Box1Auswertung]
WHERE Rueckmeldedatum >= '20230127 00:00:00' AND
Rueckmeldedatum < '20230128';
You should use this instead of LEFT or casting, then, if the index is available, your query can use this:
SELECT TOP (1000) [KNR]
,[Rueckmeldedatum]
FROM [Fertigung].[dbo].[Box1Auswertung]
WHERE Rueckmeldedatum >= '2023-01-27' AND Rueckmeldedatum < DATEADD(DAY, 1, '2023-01-27')
Also, make your query from the application parameterized.
My question is: I'm trying to select data whose date is my expression.
For example:
select *
from cutting
where cut_date = 14-07-2017
But it shows an error:
Operand type clash: date is incompatible with int
In SQL Server.
Please help I am just learning basics of SQL
Thanks.
You need to surround(enclose) the date with ':
select * from cutting where cut_date ='14-07-2017'
Or better yet, the proper way is to set the date format yourself:
select * from cutting where CONVERT(VARCHAR(10),cut_date,10) = '2017-07-14'
Or:
select * from cutting where FORMAT(cut_date,'yyyy-MM-dd') = '2017-07-14'
In SQL DATETIME - format: YYYY-MM-DD HH:MI:SS.
Replace date in where clause with "YYYY-MM-DD" format;
select *
from cutting
where cut_date = "2017-07-14"
I am trying to convert Timestamp data type columns to Date datatype using:
bq query -q --destination_table=NEW_DATE_TABLE --replace "SELECT DATE(CURR_DT) AS CURR_DT from TEST.DATE_TABLE"
The new table shows the column as STRING rather than date. Is there a way to convert timestamp to date data type.
Requested Screenshot
If you use Standard SQL, you can do the following:
SELECT * REPLACE(EXTRACT(DATE FROM curr_dt)) AS curr_dt FROM test.date_table
If curr_dt is repeated field, then the solution will look the following:
SELECT * REPLACE(
ARRAY(
SELECT EXTRACT(DATE FROM curr_dt) FROM t.curr_dt
) AS curr_dt)
FROM test.date_table t
Consider below!
Works in both Legacy and Standard SQL
SELECT CAST(DATE(CURR_DT) AS DATE) AS CURR_DT FROM TEST.DATE_TABLE
Added to address comment
Try below - as I mentioned above - it works for both Legacy and Standard
SELECT CAST(DATE(CURR_DT) AS DATE) AS CURR_DT
FROM (SELECT CURRENT_TIMESTAMP() AS CURR_DT)
if you are interested in making your case working with Legacy SQL - provide more details about CURR_DT field
Try this
SELECT TIMESTAMP_SECONDS(CAST(CURR_DT AS INT64)) AS CURR_DT FROM TEST.DATE_TABLE
I'm trying to search in an database for records with a specific date. I've tried to search in the following ways:
SELECT *
FROM TABLE_1
WHERE CAL_DATE=01/01/2015
and
SELECT *
FROM TABLE_1
WHERE CAL_DATE='01/01/2015'
I'm working with an Access database, and in the table, the dates are showing in the same format (01/01/2015). Is there something I'm missing in the SQL statement?
Any of the options below should work:
Format the date directly in your query.
SELECT *
FROM TABLE_1
WHERE CAL_DATE=#01/01/2015#;
The DateValue function will convert a string to a date.
SELECT *
FROM TABLE_1
WHERE CAL_DATE=DateValue('01/01/2015');
The CDate function will convert a value to a date.
SELECT *
FROM TABLE_1
WHERE CAL_DATE=CDate('01/01/2015');
The DateSerial function will return a date given the year, month, and day.
SELECT *
FROM TABLE_1
WHERE CAL_DATE=DateSerial(2015, 1, 1);
See the following page for more information on the above functions: techonthenet.com
Try using CDATE function on your filter:
WHERE CAL_DATE = CDATE('01/01/2015')
This will ensure that your input is of date datatype, not a string.
SELECT * from Table1 WHERE (CDATE(ColumnDate) BETWEEN #03/26/2015# AND #03/19/2015#)
if this works .. vote for it.. we use above query for searching records from 26th march to 19 the march.. change the dates accordingly..
SELECT * from Table1 WHERE (CDATE(ColumnDate) BETWEEN #03/26/2015# AND #03/19/2015#)
if this works .. vote for it.. we use above query for searching records from 26th march to 19 the march.. change the dates accordingly..
I have a table with one column dateX formatted as datetime and containing standard dates.
How can I select all records from this table where this dateX equals a certain date, e.g. May 9, 2014 ?
I tried the following, but this returns nothing even if I have several records with this date.
SELECT *
FROM dbo.LogRequests
WHERE (CONVERT(VARCHAR(10), dateX, 101) = '09/05/14')
Edit: In the database the above example looks as follows, using SQL 2012: 2014-05-09 00:00:00.000
The easiest way is to convert to a date:
SELECT *
FROM dbo.LogRequests
WHERE cast(dateX as date) = '2014-05-09';
Often, such expressions preclude the use of an index. However, according to various sources on the web, the above is sargable (meaning it will use an index), such as this and this.
I would be inclined to use the following, just out of habit:
SELECT *
FROM dbo.LogRequests
WHERE dateX >= '2014-05-09' and dateX < '2014-05-10';
For Perfect DateTime Match in SQL Server
SELECT ID FROM [Table Name] WHERE (DateLog between '2017-02-16 **00:00:00.000**' and '2017-12-16 **23:59:00.999**') ORDER BY DateLog DESC
SELECT *
FROM LogRequests
WHERE cast(dateX as date) between '2014-05-09' and '2014-05-10';
This will select all the data between the 2 dates