SQL: conditional SELECT - sql

I use the following SQL query to receive all entries that are older than payment_target days.
SELECT * from orders WHERE(EXTRACT(EPOCH FROM age(CURRENT_DATE,
date_trunc('day', orders.created_at))) - orders.payment_target*86400 >= 0)
However, I want to modify this query in a way that orders.invcoice_sent_at is used as the calculation basis if it is not null. Otherwise, `orders.created_at' should be used.
I tried it with the following query but I guess it is more pseudo code than valid SQL. I don't know how I can set the attribute to be used in the statement block.
SELECT * from orders where (EXTRACT(EPOCH FROM age(CURRENT_DATE,
date_trunc('day', IF orders.invoice_sent_at IS NOT NULL
BEGIN orders.invoice_sent_at END
ELSE
BEGIN orders.created_at END ))) - orders.payment_target*86400 >= 0)

Instead of if you can use coalesce which will work on every database supporting ANSI SQL:
coalesce( orders.invoice_sent_at, orders.created_at) - orders.payment_target*86400 >= 0)

If I understand your question correctly, you can use a declared variable.
this will also make your code more readable. something along the lines of:
DECLARE #DUEDATE DATETIME
SET #DUEDATE = ISNULL(("GET THE VALUE FROM orders.invoice_sent_at"),("GET THE VALUE FROM orders.created_at"))
use #DUEDATE in your select.

SQL Fiddle
SELECT *
from orders
where CURRENT_DATE - PaymentTarget > coalesce( InvoiceSent, Created );

Related

SQL, LEFT operator on datetime

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.

Is there a way to define a named constant/parameter in a single-statement SQL query?

Tried to do something like:
WITH
dates as (SELECT '2015-01-01' as start, '2016-01-01' as end)
SELECT * FROM my_table WHERE start_date >= dates.start AND end_date <= dates.end
But got the error message "Relation 'dates' does not exist" (in Vertica). Is there any proper way to define a constant/parameter. In real example the query contains multiple selects over a defined time range, hence I'd like to maintain the values constants/parameters in a single place to allow them to be reused in the nested subqueries.
IF possible, I'd like to refrain from DECLARE/SET-like statements, where a separate line is required.
I would still do what #GordonLinoff mentioned (I doubt it has much impact on the query plan), but in case you really don't want to or like the feature I will show at the end...
You mentioned you were going to use vsql. You can do variables there.
\set start '2015-01-01'
\set end '2016-01-01'
SELECT *
FROM my_table
WHERE start_date >= :start
AND end_date <= :end;
Which is I guess kind of nice because you can also do things like:
\set start ''`date "+%Y-%m-%d %H:%M:%S"`''
Or echo the result of any command into a variable that you can use as a variable in your sql statement. Note that this variable is quite literal and you need to include any punctuation, quoting, etc. It isn't just a value, it's more like a template.
You need to have dates in the FROM clause if you want it in the query. You can do this as:
WITH dates as (SELECT '2015-01-01' as start, '2016-01-01' as end)
SELECT t.*
FROM my_table t JOIN
dates d
ON t.start_date >= d.start AND t.end_date <= d.end;
Note: You can also do this with a CROSS JOIN. I often write queries as:
WITH params as (
SELECT '2015-01-01' as start, '2016-01-01' as end
)
SELECT t.*
FROM params CROSS JOIN
my_table t
WHERE t.start_date >= params.start AND t.end_date <= params.end;

SQL: How to use 'LIKE' with a date 'between'?

So, i´m trying to select rows between two dates.
In db, the dates also have time.
Therefor i need to use LIKE.
SQL
$query = "SELECT * FROM table WHERE date >= LIKE :selectedDateFrom AND <= LIKE :selectedDateTo";
$query_params = array(':selectedDateFrom' => $selectedDateFrom.="%", ':selectedDateTo' => $selectedDateTo.="%");
This one returns error!
How should it look like?
In db, the dates also have time.
Therefor i need to use LIKE.
No, you don't.
To select all date/times where the date component is between (from) and (to), inclusive, you can write it as
SELECT *
FROM table
WHERE date >= :selectedDateFrom
AND date < :selectedDateToPlusOne
(Note the < instead of <=, and set the second parameter to one day after the last day you want to include in your results.) This works even when the column includes times.
you can't use like with dates in SQL
SO use this:
$query = "SELECT * FROM table WHERE date >= :selectedDateFrom AND date <= :selectedDateTo";
You'd strip the time part from a datetime with DATE().
SELECT *
FROM mytable
WHERE date(mydate) >= :selectedDateFrom
AND date(mydate) <= :selectedDateTo;
Or with BETWEEN for better readability:
SELECT *
FROM mytable
WHERE date(mydate) BETWEEN :selectedDateFrom AND :selectedDateTo;

Using case statements with IsDate in a SQL where clause

I am trying to clean up the where clause statement in the following code:
SELECT
CONVERT(datetime, [UTC_Time_Stamp], 127) AS TimeStamp
FROM
Table
WHERE
CASE
WHEN ISDATE([UTC_Time_Stamp]) = 1
THEN CONVERT(datetime, [UTC_Time_Stamp], 127)
ELSE CAST('1/1/1900' AS datetime)
END > CAST('11/09/2012' AS datetime)
AND
CASE
WHEN ISDATE([UTC_Time_Stamp]) = 1
THEN CONVERT(datetime, [UTC_Time_Stamp], 127)
ELSE CAST('1/1/3000' AS datetime)
END < CAST('11/10/2012' as datetime)
ORDER BY
TimeStamp;
UTC_Time_Stamp is stored as a string and is sometimes null. I was previously running into a conversion error inside my where clause. I fixed the error following advice from this question here, but I feel like there has to be a simpler way to achieve the same result.
You need to do the conversion within a case statement. SQL is a descriptive language and does not guarantee the order of processing. So, a where clause does not necessarily happen before other processing, even when it is in a subquery or a CTE. However, SQL does guarantee the order of processing in a case statement (that contains no expressions with aggregation functions).
You can simplify your statement by using a subquery:
select TimeStamp
FROM (select t.*,
Case When ISDATE([UTC_Time_Stamp]) = 1 Then CONVERT(datetime, UTC_Time_Stamp, 127) end) as TimeStamp
from Table t
) t
WHERE coalesce(TimeStamp, cast('1/1/1900' as datetime)) > cast('11/09/2012' as datetime) and
coalesce(TimeStamp, cast('1/1/3000' as datetime)) < cast('11/10/2012' as datetime)
ORDER BY TimeStamp;
This does the conversion to TimeStamp, for valid values. You can then use the variable in the outer query.
I would also encourage you to get used to the ANSI standard format for dates (YYYYMMDD or YYYY-MM-DD) instead of ambiguous formats like '11/10/2012'. It may be clear to you that this means 2012-11-10 . . . or is that 2012-10-11 . . . but the format is ambiguous.
I like CTEs for this, or you could also do temp tables, table variables or an inline derived table like #Derek.
Basically, we're going to grab the proper datatype first, and then have a much easier time creating our query:
;with CTE as (
-- Bring back the column as datetime
select case when isdate(UTC_Time_Stamp) = 1 then cast(UTC_Time_Stamp as datetime) end as UTC_Time_Stamp
from [Table]
)
-- Simple select with the proper datatype
select convert(varchar(50), UTC_Time_Stamp, 127) as [TimeStamp]
from CTE
-- May still need gt and lt functionality
where UTC_Time_Stamp between cast('11/09/2012' as datetime) and cast('11/10/2012' as datetime)
It seems like you're using some arbitrarily small and large values for TimeStamp when you have non-dates, which is probably unnecessary given your comparison, so I've removed them.
Note that I am using the datetime datatype in the CTE and for the comparison, only converting it to a string for presentation.
Also note that between is inclusive, so you might need to go back to your separate > and < where clause.
It's easier to do something like this (using whatever convert/between clause in the predicate that you see fit):
SELECT CONVERT(datetime, [UTC_Time_Stamp], 127) as TimeStamp
FROM (
select [UTC_Time_Stamp]
from Table
WHERE ISDATE([UTC_Time_Stamp]) = 1
) a
WHERE
convert(datetime, [UTC_Time_Stamp], 127) between '11/9/2012' and '11/10/2012'
ORDER BY TimeStamp;
In this time for me this solves the problem of working with temporary table alias
or sub queries that can slow down the selection of millions of records.
select your_column
from your_table
where case when ISDATE(your_column) = 1
then Cast(your_column as date)
end Between '01/01/2016' and '01/01/2017'
order by your_column
Regards

SQL date Statement

I need some help figuring out and SQL Statement.
I know what I want I just cant express it.
Im using php, so it doesnt need to be exclusivly SQL, its to act as a filter.
Pseudo code
$query="SELECT * FROM MyTable WHERE 'TIS' is not older than 2 days or empty = ''$ORDER"; }
TIS in the name of the column in my table were I store dates in this format 03-12-09 (d,m,y).
The $ORDER is for ordering the values based on values from other fields not dates.
Im looking at
SELECT *
FROM orders
WHERE day_of_order >
(SELECT DATEADD(day,-30, (SELECT MAX(day_of_order) FROM orders)) AS "-30 Days");
But i dont quite think im on the rigth track with this.
Thanks
Try the following:
SELECT *
FROM MyTable
WHERE COALESCE(TIS, SYSDATE) > SYSDATE - INTERVAL '2' DAY
$ORDER
I don't know what database you're using - the above uses Oracle's method of dealing with time intervals. If you're using SQL Server the following should be close:
SELECT *
FROM MyTable
WHERE COALESCE(TIS, GETDATE()) > DATEADD(Day, -2, GETDATE())
$ORDER
In MySQL try this:
SELECT *
FROM MyTable
WHERE COALESCE(TIS, NOW()) > DATE_SUB(NOW(), INTERVAL 2 DAYS)
$ORDER
I hope this helps.
So, I was pretty lost in all this.
How did it got solved:
First I understood that the Statement I was using was not supported by MySql thanks to eligthment from Bob Jarvis.
_ Second In a comment by vincebowdren wich "strongly" adviced me to change the data type on that field to Date wich indeed I had not, it was a string.
It was pretty Dumb for me to try using SQL operations for Dates on a field that had String values.
So I just RTFM: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
and:
mysql> SELECT something FROM tbl_name
-> WHERE DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= date_col;
Then proceeded to change the field value to date.
and this is my perfectly working query:
$query="SELECT * FROM MyTable WHERE DATE_SUB(CURDATE(),INTERVAL 2 DAY) <= TIS OR TIS = 0000-00-00 $ORDER "; }
I would like to thank the posters for their aid.