Why isn't my nested SQL SELECT INTO statement working? - sql

The basic SELECT INTO statement works, but I need to get the TransactionNumbers in another table where the date in that table is equal to whatever I setup so it the new table doesn't get all the TransactionNumbers. How to do this? Thanks for helping!
query = select * into [bos_primary_db2].dbo.[TenderEntry] from
[bos_primary_db].dbo.[TenderEntry]
where [TenderEntry].TransactionNumber in
(select TransactionNumber
from [bos_primary_db.dbo].[Transaction]
where [Transaction].Date >= '2012-2-2'
and [Transaction].Date < '2012-2-3')

You can try this:
[Transaction].Date BETWEEN '2012-02-02' and '2012-02-03'

You have to cast your String to a date:
where [Transaction].Date >= cast('2012.2.2' as date,102) and [Transaction].Date < cast('2012.2.3' as Date,102 )

Try this :
WHERE
select TransactionNumber
from [bos_primary_db.dbo].[Transaction]
where Convert(VARCHAR, [Transaction].Date,103) >= CONVERT(VARCHAR,'02/02/2012' ,103)
and CONVERT(VARCHAR,[Transaction].Date,103) < CONVERT(VARCHAR,'03/02/2012',103)

Related

Between date condition is not working in SQL Server

I have a data as below,
Trying to run below query but returning 0 rows,
Below query should return highlighted row data as shown above.
Can anybody please explain me, what i'm missing?
select * from Flt_OperativeFlight_SchedulePeriods
where
(
(cast('2018-04-05' as date) between cast(ScheduleStartDate as date) and cast(ScheduleEndDate as date) )
or
(cast('2018-04-11' as date) between cast(ScheduleStartDate as date) and cast(ScheduleEndDate as date) )
)
and CarrierCode='SQ' and FlightNumber='0004'
You could re-write as:
select *
from Flt_OperativeFlight_SchedulePeriods
where CarrierCode='SQ' and FlightNumber='0004' and
(ScheduleStartDate >= '2018-04-05' and ScheduleEndDate <= '2018-04-11')
It happens because
'2018-04-05' < '2018-04-06'
and
'2018-04-11' > '2018-04-10'
As variant maybe it is what you want
select *
from Flt_OperativeFlight_SchedulePeriods
where CarrierCode='SQ' and FlightNumber='0004' and
(
(ScheduleStartDate between '20180405' and '20180411')
or (ScheduleEndDate between '20180405' and '20180411')
)
You can try this
SELECT *
FROM `Flt_OperativeFlight_SchedulePeriods`
WHERE ScheduleStartDate >= '2018-04-05' AND ScheduleEndDate <= '2018-04-11'
AND CarrierCode='SQ' and FlightNumber='0004'
Seems like you want to get overlapping periods, then you need this logic:
start_1 <= end_2 and end_1 >= start_2
For your query:
where
(
cast('2018-04-05' as date) <= cast(ScheduleEndDate as date)
and
cast('2018-04-11' as date) >= cast(ScheduleStartDate as date)
)
Depending on your logic you might have to < or >

SQL Query with between two dates showing even those not between

select ktl.id, kth.trans_dte, kth.trtype, kp.match_code,ktl.net_amount,ktl.gross_amount,ktl.db_portfolio_type,ktl.cr_portfolio_type from k$transaction_lines ktl
left join k$transaction_header kth on ktl.id=kth.id
left join k$portfolio kp on kp.id = (CASE WHEN ktl.db_portfolio_type = 'C' THEN ktl.db_portfolio ELSE ktl.cr_portfolio END)
where to_char(kth.trans_dte,'DD-MON-YY') >= '22-AUG-16'
and to_char(kth.trans_dte,'DD-MON-YY') <= '27-AUG-16'
and ktl.db_portfolio_type <> 'I'
and ktl.cr_portfolio is not null
order by kth.trans_dte, ktl.id, kp.match_code, kth.trtype
This is my query. I just want to know if I have something wrong with my where clause kth.trans_dte.
I only want to get Transactions from August 22 to Aug 27 but it I am getting transactions before that date, february and march are included when they shouldn't be. I wonder why.. Is there a problem with my code or is it something with the db that I don't know. Thanks!
you convert your field to char with to_char and then try to compare it datewise. to_char does exactly what it says - converts to a char string so you'll get dates according to ascii representation of the string
try using to_date instead - on both sides of the condition (s)
try using BETWEEN instead of 2 conditions .
select ktl.id, kth.trans_dte, kth.trtype, kp.match_code,ktl.net_amount,ktl.gross_amount,ktl.db_portfolio_type,ktl.cr_portfolio_type from k$transaction_lines ktl
left join k$transaction_header kth on ktl.id=kth.id
left join k$portfolio kp on kp.id = (CASE WHEN ktl.db_portfolio_type = 'C' THEN ktl.db_portfolio ELSE ktl.cr_portfolio END)
where (to_char(kth.trans_dte,'DD-MON-YY') >= '22-AUG-16'
and to_char(kth.trans_dte,'DD-MON-YY') <= '27-AUG-16')
and ktl.db_portfolio_type <> 'I'
and ktl.cr_portfolio is not null
order by kth.trans_dte, ktl.id, kp.match_code, kth.trtype
Try this.
try using between Keyword and to_date function in Query like
where kth.trans_dte between to_date('22-AUG-16')
and to_date('27-AUG-16')
and ktl.db_portfolio_type <> 'I'
and ktl.cr_portfolio is not null
order by kth.trans_dte, ktl.id, kp.match_code, kth.trtype
Sorry, I solved it just now. I can't use to_char when doing that condition that is why it gives the wrong results.

is there a way to use an if statement in the WHERE part of the SQL query?

Is there a way to use an if statement in the where part of the SQL query? For example:
SELECT count(*)
from table_name tb
where ( if (#enddate>dateadd("d",2,#date) then date > tb.date
else dateadd("d",2,#date)>tb.date) )
I need to somehow do this check where I check if the date 2 days later is not greater than the end date, otherwise I have to use the end date by default.
You can use CASE expression:
SELECT count(*)
FROM table_name tb
WHERE (CASE WHEN (#enddate>dateadd("d",2,#date)) THEN date > tb.date
ELSE
dateadd("d",2,#date)>tb.date
END);
this answer is tailored onto to the case explained into the question as the issue to solve and not on the title.
to handle that case there is no need for a special solution and/or keyword, it is handled nicely using a regular WHERE clause:
SELECT count(*)
from table_name tb
where (#enddate > dateadd("d",2,#date) AND date > tb.date)
OR
(dateadd("d",2,#date) > tb.date))
I found the following:
CASE
WHEN (DATENAME(dw,#st) = 'Monday')
AND (
((Day(#st) = 01))
OR ((Day(#st) = 02))
OR ((Day(#st) = 03))
)
THEN DATEADD(DAY,-2,#st)
ELSE DATEADD(DAY, - 1, DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0))
END
And it actually works quite nicely.
This will only run when the first working day of the month is a Monday, hence that's when I want to get the weekend's data. Otherwise, I already have it.

Conditionally use WHERE clause in SQL

Just see this:
SELECT clientid,clientname,startdate,enddate,age FROM clients
WHERE clientid IN (1,2,3,4,5)
AND CASE WHEN age>10 THEN enddate>'31-05-2013'
END
My question: I want the second condition enddate>'31-05-2013' only if age > 10
What's wrong in this query?
Select clientid,clientname,startdate,enddate,age from clients
where clientid in (1,2,3,4,5)
and (age <= 10 OR enddate > '31-05-2013')
I don't think you can use a case expression like this. Try this instead:
Select clientid,clientname,startdate,enddate,age from clients
where clientid in (1,2,3,4,5)
and (age<=10 or enddate>'31-05-2013')
There are a couple of things that could be wrong. One is that dates are normally in 2013-05-31 order. That might change depending on locale settings; let us assume so.
Otherwise, you need to write the query more simply as:
SELECT clientid, clientname, startdate, enddate, age
FROM clients
WHERE clientid IN (1,2,3,4,5)
AND ((age > 10 AND enddate > '31-05-2013') OR (age <= 10))
Or, using the case:
SELECT clientid, clientname, startdate, enddate, age
FROM clients
WHERE clientid IN (1,2,3,4,5)
AND CASE
WHEN age > 10 THEN enddate > '31-05-2013'
ELSE TRUE
END
(The default for no ELSE clause is NULL.)
Select clientid,clientname,startdate,enddate,age from clients
where clientid in (1,2,3,4,5)
and ( (age>10 and enddate>'31-05-2013') or (age<=10 and enddate<='31-05-2013') )
I would use unambigious dateformat
Select clientid,clientname,startdate,enddate,age from clients
where clientid in (1,2,3,4,5)
and (age <= 10 OR enddate > '20130531')
See this for reason http://beyondrelational.com/modules/2/blogs/70/posts/10899/understanding-datetime-column-part-ii.aspx

WHERE clause where value is convertible

Is there a way in SQL Server using T-SQL to say:
WHERE CONVERT(date, mat1_04_05, 101) = true
I'm doing some reporting against an app that I don't have source for and the column is varchar and I can't rely on user data.
EDIT
I tried using ISDATE. However I'm still running into a conversion error this is the full query:
SELECT mat1_04_01 AS 'CaseStg',
matter.mat_no AS 'MatNo',
MAX(matter.client) AS 'Client',
MAX(mat1_03_01) AS 'InCo',
MAX(mat1_07_01) AS 'Clm Amt',
MAX(mat1_07_03) AS 'Clm Bal',
MAX(mat1_04_05) AS 'BilSnt',
MAX(mat1_01_07) AS 'Injured',
CONVERT(CHAR, MIN(CONVERT(DATE, usr1_02_01))) AS dos_start,
CONVERT(CHAR, MAX(CONVERT(DATE, usr1_02_02))) AS dos_end
FROM lntmuser.matter
INNER JOIN lntmuser.usertype1
ON lntmuser.matter.sysid = lntmuser.usertype1.mat_id
WHERE Isdate(mat1_04_05) = 1
AND Datediff(DAY, CONVERT(DATE, mat1_04_05, 101), Getdate()) > 31
AND mat1_04_01 LIKE 'BILLING MAILED OUT'
AND matter.status NOT LIKE 'CLOSED'
GROUP BY mat1_04_01,
matter.mat_no
Dude -- it doesn't make sense to use ISDATE() and CONVERT() on the same date field in your WHERE without a control structure. I.e., if ISDATE() = false, then CONVERT() is guaranteed to give you a conversion error.
Try this:
WHERE
...
CASE WHEN ISDATE(myDateField) = 1 THEN DATEDIFF(CONVERT(...)) ELSE 0 END > 31
DO you mean check to see whether that column is a date??
WHERE ISDATE(matl_04_05) = 1
Use ISDATE.
... WHERE ISDATE(mat1_04_05) = 1
Assuming I understood your original question...
IF ISDATE('2009-05-12 10:19:41.177') = 1
PRINT 'VALID'
ELSE
PRINT 'INVALID'
source: http://msdn.microsoft.com/en-us/library/ms187347.aspx
Do you mean something like
SELECT * FROM FOO where ISDATE(mat1_04_05)
For datetime you can;
;with T(F) as (
select 'cake' union
select '01 mar 2011'
)
select cast(f as datetime) from T where isdate(F) = 1
>>F
>>2011-03-01 00:00:00.000