I am using Oracle sql developer.And I have to categorize some data into each Date from a table.
I used TO_CHAR function to change the date format.
select to_char(txn_date, 'dd-mon-rrrr') my_day, some_coloums
from mwt_wallet_transactions
order by my_day;
It's ok with upper one. But I want to get it with given date like this:
select to_char( txn_date, 'dd-mon-rrrr') my_day, some_coloums
from mwt_wallet_transactions
where my_day like '08-feb-2015'
order by my_day;
I searched this over the internet, most of them asked to use the CONVERT function like this:
select convert (VARCHAR(10), txn_date , 23 ) my_day, some_colomns
from mwt_wallet_transactions
where my_day like '08-feb-2015'
order by my_day;
But it doesn't work for me. Help me to solve this!
where my_day like '08-feb-2015'
I think you got confused between SELECTION and FILTER PREDICATE.
Also, remember, ''08-feb-2015' is NOT a DATE, it is a string.
You want to filter the rows based on a DATE value. So, convert the literal on the R.H.S. into DATE using TO_DATE or use ANSI Date literal if you don't have a time portion.
Now, remember, a DATE has both date and time elements, so you need to -
either use TRUNC on the date column to get rid off the time element
or, use a DATE range condition to for better performance as it would use any regular index on the date column.
I am assuming my_day as the date column. Modify the filter as:
Using ANSI Date literal: fixed format 'YYYY-MM-DD'
where my_day >= DATE '2015-02-08' and my_day < DATE '2015-02-09'
Or, TO_DATE with proper format model. Remember, TO_DATE is NLS dependent, so I have used NLS_DATE_LANGUAGE to make it NLS independent.
WHERE my_day >= TO_DATE('08-feb-2015','dd-mon-yyyy','NLS_DATE_LANGUAGE=american')
AND my_day < TO_DATE('09-feb-2015','dd-mon-yyyy','NLS_DATE_LANGUAGE=american')
Above. my_day is assumed as the static date column, and not the column alias.
You can't use select list column aliases in the WHERE clause. Use derived table to access it:
select * from
(
select *some_computed_value* as my_day, some_colomns
from mwt_wallet_transaction
)
where *my_day conditions*
order by my_day
My solution:
I checked this solutions and it's working well,
select * from
(select cast( TRUNC(txn_date) as date) my_day ,some_columns
from mwt_wallet_transactions
)
where my_day = '08-FEB-15'
order by my_day
;
or we can use:
select * from
(select to_char( txn_date, 'dd-mon-rrrr') my_day
from mwt_wallet_transactions
)
where my_day = '08-feb-2015'
order by my_day;
Related
I've got this query:
SELECT user_id, from_loc_id, to_loc_id, to_char(dstamp, 'hh24:mi:ss')
FROM inventory_transaction
WHERE code = 'Pick'
AND substr(work_group,1,6) = 'BRANCH'
AND dstamp BETWEEN to_date('24/02/2022 17:00:00', 'dd/mm/yyyy hh24:mi:ss') AND
to_date('24/02/2022 18:00:00', 'dd/mm/yyyy hh24:mi:ss')
ORDER BY user_id;
That's the output:
My expected output is:
I was trying to use lag, but didn't really worked.
I've just realized I need to add a second ORDER BY, so first by user, second by to_char(dstamp, 'hh24:mi:ss').
All solutions much appreciate. Thank you.
You can use NUMTODSINTERVAL function with day argument and applying SUBSTR to extract hours:minutes:seconds portion as your data resides within a specific date such as
SELECT t.user_id,
t.dstamp,
SUBSTR(
NUMTODSINTERVAL(dstamp - LAG(dstamp)
OVER (PARTITION BY user_id ORDER BY dstamp),'day'),
12,8) AS time_diff
FROM t
Demo
Edit : The case above is applied for the column dstamp is considered to be of date data type, if its data type is timestamp, then use the following query containing date cast instead
SELECT t.user_id,
t.dstamp,
SUBSTR(
NUMTODSINTERVAL(CAST(dstamp AS date) - LAG(CAST(dstamp AS date))
OVER (PARTITION BY user_id ORDER BY CAST(dstamp AS date)),'day'),
12,8) AS time_diff
FROM t
Demo
Not sure how to use the NOW() function in presto. Seems like it should be straight forward, but i'm getting no luck
SELECT DISTINCT field
FROM table
WHERE field BETWEEN '2019-01-01' and NOW()
field = varchar
You should match data types to avoid implicit conversions:
SELECT DISTINCT field
FROM table
WHERE CAST(field AS DATE) BETWEEN DATE '2019-01-01' AND CURRENT_DATE;
SELECT DISTINCT field
FROM table
WHERE DATE(field) BETWEEN DATE '2019-01-01' AND CURRENT_DATE;
I have the following values in my table
Select * From TimeSheetLogs where InTimeStamp <= '1/22/2013'
when i execute the above query i get null value
but as you can see i have 3 data with datefield as 1/22/2013
Then what i am doing wrong?
Thanks
that is correct because
'1/22/2013 19:21' > '1/22/2013 00:00'
You probably need to truncate your dates first to remove the time portion. Then convert both sides to date datatype. The string '1/22/2013' is a string, not a date. By looking at data your InTimeStamp is timestamp datatype. You cannot compare date or timestamp to character w/out converting the char-s to date. I'm not sure what Database are you using. This is how you'd convert in Oracle using to_date function .
SELECT in_date, compare_date
FROM
(-- This is your InTimeStamp on the fly
SELECT trunc(to_timestamp('2013-01-22 16:21:19.273', 'yyyy-mm-dd hh24:mi:ss.ff')) in_date -- this is your InTimeStamp
, to_date('1/22/2013', 'mm/dd/yyyy') compare_date
FROM dual
)
WHERE in_date <= compare_date
/
Now you can compare two dates below - this is the output of above query:
in_date compare_date
---------- ------------
1/22/2013 1/22/2013
Select * From TimeSheetLogs
where InTimeStamp <= select convert(datetime,'1/22/2013',101)
As per my comment to #Zdravko's good answer, you just need to cast your date:
Select * From TimeSheetLogs where CAST(InTimeStamp as Date) <= '1/22/2013'
I want to order values from database query by date but I want also to change their format
SELECT to_char( date, 'DD.MM.YYYY') "date1", date as date2
from ..
order by date2
This SQL has just one problem. I don't want date2 int the output.
Thanks for your help
Just use
order by date
or
order by tableName.date
use this:
SELECT to_char( date, 'DD.MM.YYYY') "date1"
from ..
order by date
try this:
SELECT to_char( date, 'DD.MM.YYYY') "date1"
from ..
order by to_date(date,'DD-MM-YYYY')
I suspect the issue you are running into is that the column is named date, which is an Oracle reserved word. If that is the case, you need to enclose it in quotes in order to avoid the error.
SELECT to_char( "DATE", 'DD.MM.YYYY') "date1"
from tablename
order by "DATE"
I have a datetime field in my Postgresql, named "dt".
I'd like to do something like
SELECT * FROM myTable WHERE extract (date from dt) = '01/01/11'
What is the right syntax to do that?
Thanks!
I think you want to cast your dt to a date and fix the format of your date literal:
SELECT *
FROM table
WHERE dt::date = '2011-01-01' -- This should be ISO-8601 format, YYYY-MM-DD
Or the standard version:
SELECT *
FROM table
WHERE CAST(dt AS DATE) = '2011-01-01' -- This should be ISO-8601 format, YYYY-MM-DD
The extract function doesn't understand "date" and it returns a number.
With PostgreSQL there are a number of date/time functions available, see here.
In your example, you could use:
SELECT * FROM myTable WHERE date_trunc('day', dt) = 'YYYY-MM-DD';
If you are running this query regularly, it is possible to create an index using the date_trunc function as well:
CREATE INDEX date_trunc_dt_idx ON myTable ( date_trunc('day', dt) );
One advantage of this is there is some more flexibility with timezones if required, for example:
CREATE INDEX date_trunc_dt_idx ON myTable ( date_trunc('day', dt at time zone 'Australia/Sydney') );
SELECT * FROM myTable WHERE date_trunc('day', dt at time zone 'Australia/Sydney') = 'YYYY-MM-DD';