Working with to_char in where clause oracle [duplicate] - sql

This question already has answers here:
Oracle Date TO_CHAR('Month DD, YYYY') has extra spaces in it
(6 answers)
Closed 8 years ago.
I have a table called room_allocation and want to extract details of those who were admitted in the month of January.So I have used the following query:
select * from room_allocation where to_char(adm_date,'Month')='January ';
the output for this is:
no data found
but when I give it as:
select to_char(adm_date,'Month') from room_allocation;
I get the output as:
TO_CHAR(ADM_DATE,'MONTH')
October
November
December
January
So please tell me why its not working in the first case.
Thank you.

Use the format modifier FM. This will remove the trailing space.
SELECT *
FROM room_allocation
WHERE to_char(adm_date,'FMMonth')='January';

Maybe the extra space you have at the end of January.
select * from room_allocation where to_char(adm_date,'Month')='January ';
Remove that and try it again

Related

MSACCESS SQL Date greater than 3 days ago - Pass Through query to Oracle [duplicate]

This question already has answers here:
Add days Oracle SQL
(6 answers)
Closed 12 months ago.
I am having an issue trying to add a portion to my WHERE clause to get only the last 3 days of data rather than grab the whole table. The date format comes through as 'dd-Mon-yy'
Here is my current WHERE clause
WHERE ("IAINVN00"."REF_LOCN" LIKE '51C%'
OR "IAINVN00"."REF_LOCN" LIKE '511%')
This works fine, just brings back way too much data. When I add:
and "IAINVN00"."ADJDATE" >= (Date()-3)
This brings back an error of "ODBC--call failed. [Oracle][ODBC][Ora]ORA-00936: missing expression (#936)"
I have tried using this as well and get the same error
DateAdd("d",-3,Date())
In order to fix this, instead of using Date, I needed to use SysDate.
and "IAINVN00"."ADJDATE" >= sysdate - 3

how to use year function and groupby to do a dynamic search?

Run a query that returns the number of trouble tickets in the VTM024… table, but only the ones created in the year 2015. Note: when setting the where criteria, use a function to extract the Year portion from the timestamp field.
Using the Year function in the first answer, write SQL that shows the number of trouble tickets created in each year dynamically, and do this using a “GROUP BY” clause in the SQL. It should return something like:
2012 10
2013 54
2014 111
etc
I was able to answer first question myself:
SELECT COUNT(CREATE_TIME) FROM TRACS_DW1.VTM024TROUBLE_TKT
WHERE EXTRACT (YEAR FROM CREATE_TIME) = 2015
I need help with 2nd question. I cant get groupby and extract functions together.
i have added a picture of my table.
Add EXTRACT (YEAR FROM CREATE_TIME) as year to your select query and group by the year field.

Extract Last Name only from Lastname,Firstname [duplicate]

This question already has answers here:
How to Select a substring in Oracle SQL up to a specific character?
(8 answers)
Closed 5 years ago.
I just started working with Oracle recently so please bear with me.
I have a column of names as LASTNAME,FIRSTNAME and I need to extract JUST the last name. I know how to do this in excel, where I want to so search for the "," find the length of characters before the comma and return those characters.
I know to use SUBSTR as my LEFT() function, but I'm stuck from there.
One method in Oracle is regexp_substr():
select regexp_substr(name, '[^,]*')

why we are using fm and trim in char functions [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Query #1
Select *
from EMPLOYEE
where trim( to_char(joining_date,'Month')) ='January';
Query#2
select *
from employee
where to_char(joining_date,'Month')='January';
Query #3
select *
from employee
where to_char(joining_date,'fmMonth')='January';
If I do first query I am getting answer.
If I do 2nd query I am not getting answer .
If I use fm in 3rd query I am getting answer
What is the reason behind this.
Cause Oracle (I guess you're working with Oracle) adds blank spaces (padding) so that the results have always the same "size" (format) when formatting values.
Fake example, without trim or fm (oh, how nicely formatted is this !)
01 January 2015
12 September 2015
With Fm version or trim, you remove these "padding" spaces.
Fake example, with trim or fm (oh, that's ugly, but at least I don't have all these spaces)
1 January 2015
12 September 2015
With FM (stands for format mask) prefix, you are applying the format_mask. This means that you are asking query engine that zeros and blanks are meant to be suppressed.
If you are asking why trim or FM then you should try running these two queries to understand it:
select length(trim(to_char(to_date('15052015', 'DDMMYYYY'), 'MONTH'))) from dual;
select length(to_char(to_date('15052015', 'DDMMYYYY'), 'MONTH')) from dual;
select length(trim(to_char(to_date('15092015', 'DDMMYYYY'), 'MONTH'))) from dual;
select length(to_char(to_date('15092015', 'DDMMYYYY'), 'MONTH')) from dual;
Since your NLS setting by default for NLS_DATE_LANGUAGE would be ENGLISH, May takes 3 characters while September takes 9 characters. Hence by default it will add 6 additional characters to May as well.
The FM amodifier, used in format models in the TO_CHAR function, control blank padding and exact format checking.
The SQL TRIM() removes leading and trailing characters(or both) from a character string.

How can we calculate the number of days between two dates in oracle-sql ? exact syntax? [duplicate]

This question already has an answer here:
Get the number of days between two dates in Oracle, inclusive of the dates
(1 answer)
Closed 7 years ago.
I have tried this
select datediff(day,doi,dateeg) from Cardholders;
error:ORA-00904 : "DATEDIFF" invalid identifier
here doi and dateeg are of date datatype in cardholders relation ?
In Oracle, just use subtraction. This will probably do what you want:
select trunc(dateeg) - trunc(doi) from Cardholders