Searching a SQL database by 'date' - sql

I'm trying to search a SQL database using this SQL query:
SELECT * FROM Reservations WHERE fDate=06/12/13
Here is an image of my SQL Reservations table with dummy data:
What am I doing wrong here?
Thanks,
C.

You'll need to put the date inside ''
SELECT * FROM Reservations WHERE fDate='06/12/13'
But, if what you're showing us a view, then you'll need to use the standard format for dates:
SELECT * FROM Reservations WHERE fDate='2013-12-06'

Try changing date format from 'dd/mm/yyyy' to 'yyyy/mm/dd'.
Like this:
SELECT * FROM Reservations WHERE fDate='2013/11/08'
See sample SQL Fiddle I created

You are missing single quotes around the date ''
It should be simply:
SELECT * FROM Reservations WHERE fDate = '06/12/13'

Related

format the timestamp output for TABLE_DATE_RANGE() BigQuery

I have a requirement to query different tables a once to save my time. Tables names like
abc_yyyymmdd
can be easily query using the
table_date_range(abc_,timestamp('2016-01-01'),timestamp('2016-03-12'))
but I have different format table name
abc_mm_dd_yyyy
is there a way to query in these tables using table_date_range.
In Legacy SQL you can use TABLE_QUERY for this
So it can be something like below
SELECT *
FROM (
TABLE_QUERY(YourDataset, 'LEFT(table_id, 4) = "abc_" AND LENGTH(table_id) = 14
AND CONCAT(SUBSTR(table_id,11,4),'-',SUBSTR(table_id,5,2), -",SUBSTR(table_id,8,2))
BETWEEN "2016-01-01" AND "2016-03-12"')
)
If you can use Standard SQL, you can use the _TABLE_SUFFIX pseudo column to work with any table name format.
Is there an equivalent of table wildcard functions in BigQuery with standard SQL?
In this case, it would be something like:
SELECT ... FROM `mydataset.abc_2016_*` WHERE _TABLE_SUFFIX = '01-01' or _TABLE_SUFFIX = '03-12'

select where substring

SQL newbie here, but I can't find the solution to something that looks easy:
The following query does not seem to have a valid syntax (ORA-00904: invalid identifier), but its logic should be clear. How can I achieve this in a query that needs to be speedy?
SELECT * FROM table WHERE LEFT(column,4)="abcd"
For this purpose, you should use like rather than left(). First, Oracle doesn't support left() (you need substr() instead). Second, like can make use of indexes because the wildcard is not at the beginning of the string:
SELECT *
FROM table
WHERE column like 'abcd%';
Oracle and some other products have substr.
SELECT * FROM tablename WHERE substr(columnname, 1, 4) = 'abcd'
I.e. single quotes for string literals!
ANSI SQL has substring:
SELECT * FROM tablename WHERE substring(columnname from 1 for 4) = 'abcd'
And others have left:
SELECT * FROM tablename WHERE LEFT(columnname,4) = 'abcd'

SQL History Query

I need to select data from a inventory history table. The main part of my query will pull:
select *
from inv_history
where updated_date between '2-sep-14' and '8-sep-14'
and inv_STATUS = 'ON HAND'
Then I also need to include the record of this data before the status went to 'ON HAND'. Any tips would be appreciated.
Introduce a AFTER UPDATE trigger when the inv_status changes to ON HAND
This link could help you.
where updated_date between '2-sep-14' and '8-sep-14'
Your query predicate is incorrect. You must need to apply TO_DATE to the date literals.
To answer your question regarding including the records before the status went to "ON HAND", add the following query to your query using UNION ALL :
select *
from inv_history
where updated_date > ( select min(updated_date) from inv_history where between inv_STATUS = 'ON HAND')
I would rather use subquery factoring to have the rows from inv_history as:
with data as(
Select * from inv_history
where inv_status= 'ON_HAND'
Use the above just once rather then accessing the inv_history twice.

mySql WHERE `xml<date>` = '11/21/2010'

i have a mySql database, and it has an field for each entry called xml, which contains XML.
i would like to know if there is syntax to select xml values like normal fields.
for example if i wanted to select all entries with the date of 11/21/2010
i try something like this, but the syntax is wrong, since i don't know how to work with xml well in mySql.
SELECT *
FROM `table`
WHERE `xml<date>` = '11/21/2010'
Ideally, I would like to compare it to another field:
SELECT *
FROM `table`
WHERE `xml<date>` != `date`
You can use the ExtractValue function with an XPath expression to get your desired field:
SELECT *
FROM `table`
WHERE ExtractValue(xml,'/a/suitable/xpath/expression') != `date`

SQL select using condition that a column starts with a certain string

I am using sql.
How to select all items from a table.
where a column (varchar) path is start with a certain string : "stringABC"
select * from ATable where path like 'stringABC';
The above sql is wrong, can any one help me to fix it?
Many thanks!
select *
from ATable
where path like 'stringABC%'
select * from ATAble where path like 'string%'