help with access query - ms-access-2007

i have table MyTbl that contain date1, date2, date3
if date1=null i need that in field memo will be 'A'
elae if date2=null i need that in field memo will be 'B'
else if date3=null i need that in field memo will be 'C'
is it can made on access 2007 in query ?
thank's in advance

If I understand correctly, you should be able to do this with using IIF -
memo: IIf(IsNull([date1]=True),"A",IIf(IsNull([date2])=True,"B",IIf(IsNull([date3])=True,"C")))

Related

Get all rows with a list of date between two date columns in SQL

I have a table named tableA which has two date columns. Currently, I am using the below query to fetch data.
"select * from tableA where IN_Date between date1 and date2"
IN_DATE is input param from the proc
Now instead of one date IN_DATE, I want to pass a list of dates but I am not sure how to update the query. Please help.
TableA
id date1 date2
The solution to your problem
select * from tableA where (
(IN_Date between date1 and date2) or
(IN_Date between date3 and date4) or
(IN_Date between date5 and date6)
)
What you are trying to do simply is not possible.
The syntax of the between clause is:
... expression1 BETWEEN expression2 AND expression3 ...
Each expression must resolve to a single value (not a list of values). Furthermore expression2 be < expression3, otherwise results are undefined.
Where expressionN is a column name, then the single value is the value in the row currently being evaluated.
This suggests that you may be approaching this incorrectly. Please provide some sample data, and expected results. This will allow a better understanding of what you are trying to do. A description of what you are wanting to achieve would also be helpful, rather than a description of how you are trying to achieve it.
You may use a string tokenization approach like below, where the IN_DATE string parameter has comma separated list of dates in the form of YYYY-MM-DD.
select *
from tableA t
where exists
(
select 1
from xmltable
(
'for $id in tokenize($s, ",") return <i>{normalize-space ($id)}</i>'
passing IN_DATE as "s"
columns
tok char(10) path '.'
) v
where date (to_date (v.tok, 'YYYY-MM-DD')) between t.date1 and t.date2
)

Updating table column date with case expression?

I need to update column8 with data depending on two other columns with dates (date1 and date2) - if date1 is later than date2 then column8 is YES otherwise its NO.
I wrote this:
update table1
set delay = case when date1 > date2 then 'YES'
when date2 is NULL then 'YES'
else 'NO'
end
The problem is that it is probably not comparing the dates but length of the expression, because I have no in every column except for null columns... It tells me that it probably does not know I want it compare as a date But the columns in database are in date format YYYY-MM-DD.
Is there any way to update my code or in adding something to make compare the dates and not the string lengths?
Thanks!
You can try CAST function:
WHEN CAST(date1 AS DATE) > CAST(date2 AS DATE)

Bad date format Oracle SQL

I'm facing an issue with date format. I need the row with the max(date1)
I don't think that my query is incorrect :
SELECT column1,
column2,
date1
FROM table
WHERE date1 = (SELECT MAX(date1) FROM table);
When i use this query in dev environment with TOAD, i'm receiving that result :
column1;column2;date1
aaaaa;bbbbb;19/09/2014 14:13:21
But, when i ask our infrastructure to make this query in production, they returned me that result :
column1;column2;date1
ccccc;dddddd;14/09/26
The date isn't in good format... What can i do the receive the good date ? Does i have to specified the format in my query ? Or does the infra team messed up somethings while giving me the result ?
Thanks in advance for your help
date1 is a date column - it doesn't have any intrinsic format - that's up to the client and the environment to determine when printing it.
If you want to control the format, you need to do so explicitly with the to_char function:
SELECT column1,
column2,
TO_CHAR(date1, 'DD/MM/YYYY hh24:mi:ss')
FROM table
WHERE date1 = (SELECT MAX(date1) FROM table);

SQL Server: how to add case statement to select

I am using the following select to query a date from a database table.
The input (ms) for this query results from an xml string and the stored procedure then loops through all the single values in the xml to return a certain number (integer) for each of them.
This works fine so far.
Is there a way that I can return a placeholder number (like 99999) if the input (ms) is empty / nothing ?
Currently the below returns 0 in such a case which I cannot use to identify this as 0 can also be a valid result in other cases.
My stored procedure so far:
SELECT ms as date,
type,
(
SELECT COUNT(calendar_dt)
FROM Calendar
WHERE day_of_week NOT IN (1, 7)
AND calendar_dt > GETDATE()
AND calendar_dt <= ms
) as bDays
FROM #dates
FOR XML PATH('ms'), ELEMENTS, TYPE, ROOT('ranks')
Many thanks in advance for any help with this, Tim.
If the column "ms" is actually NULL or populated, just use ISNULL.
http://technet.microsoft.com/en-us/library/ms184325.aspx
SELECT ISNULL(ms, 99999) AS date
However, if that column can contain an empty string, which is not the same as NULL, then also use NULLIF.
http://technet.microsoft.com/en-us/library/ms177562.aspx
SELECT ISNULL(NULLIF(ms,''), 99999) AS date

Using a variable in an SQL query (Oracle DBMS)?

I am typing SQL queries to get some data from a software tool which is based on an Oracle database. I am using the typical SELECT-statement.
Now, in my SQL-query I am using at different places the date "02.05.2012". Is there a way to define a variable date_string at the beginning and then use at all relevant places this variable?
This would simplify things a lot. Thanks for all hints and tips!
You might try to rewrite your query to return the literal from an inline view ...
select
my_date,
...
from(
select to_date('02.05.2012','DD.MM.YYYY') my_date from dual),
table2
where
some_column <= my_date
What you look for is a bind variable.
select to-date(:date, 'dd.mm.yyyy') date1
, to-date(:date, 'dd.mm.yyyy') + 1 date2
from dual
On runtime you need to pass the value to the bind variable. It all depends on your programming language how to bind the variable, but there is plenty documentation for that.
DEFINE only works if you use sql*plus, and that's usually not the case inside a "software tool" :)
EDIT:
I'm beginning to understand now. It's just a textarea where you can enter a query and it will execute it and return the result. In that case you either write some complicated pl/sql code, or enter all the dates manually, or use a cross join with a select from dual:
with (select to_date('02.05.2012', 'dd.mm.yyyy') my_date from dual) d
select *
from some_table t
cross join d -- no ON required
If you want to select using the current date you can use sysdate.
Using SQLPLUS you can define your own variables:
SQL> define mydate ="01-05-2012"
SQL> select to_date('&mydate','DD-MM-YYYY') from dual;
01-MAY-12
try the following :
SELECT *
FROM table1
WHERE to_char(date1,'DD/MM/YYYY') = '&&date'
AND to_char(date2,'DD/MM/YYYY') = '&&date'
AND to_char(date3,'DD/MM/YYYY') = '&&date'
you will get a prompt to enter the value for the &&date , if you want to enter different values for each date, you should type &date instead of &&date
DEFINE is useful for your requirement.
DEFINE NAME="example"
access with &NAME