473,378 Members | 1,507 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Working out 6 month prior to the first day of the month

Hi!
I can work out the first day of the month using this code:
Expand|Select|Wrap|Line Numbers
  1. select 
  2.    to_char(
  3.       trunc(sysdate,'MM'),'DD/MM/YYYY'
  4.    ) as First_Day
  5. from dual;
But why does it not work when I plug it into my code?
Expand|Select|Wrap|Line Numbers
  1. select DUE_DATE from vw_current_task
  2. where
  3. DUE_DATE >  to_date(trunc(sysdate,'MM'),'DD/MM/YYYY') - 6
  4. order by DUE_DATE asc
Sorry its probably a really simple one.
Dec 5 '07 #1
2 1688
Dave44
153 100+
there are a couple issues here. first you are using to_Date on a date, which will cause an error ( i suspect you want to use to_Char there to give you the nice formatted date). However if DUE_DATE is a date and not a string then you want to keep the first of the month as a date, and not convert it to a string.

keep your dates as date datatypes unless you are returning it to be viewed by a report and want it looking in a specific format. rarely ever in the where clause of a query should you be changing dates to strings for comparison, if need be convert the string to a date for the comparison, you are less likely to make an error this way compared to the other way.

Expand|Select|Wrap|Line Numbers
  1. select to_Date(sysdate,'yyyymmdd') from dual;
  2. ORA-01858: a non-numeric character was found where a numeric was expected
  3.  
  4. select to_char(sysdate,'yyyymmdd') from dual;
  5.  
  6. TO_CHAR(
  7. --------
  8. 20071205
  9.  
  10. select to_char(sysdate,'yyyymmdd') - 6 from dual;
  11.  
  12. TO_CHAR(SYSDATE,'YYYYMMDD')-6
  13. -----------------------------
  14.                      20071199
  15.  
NOTE the following the last line there, this is why you want to be careful about comparing strings and not dates.
also you will want to use the add_months(<your_date>,-6) to get 6 months. as you have it written it will minus 6 days not months if it was in date format and not string (the string really screws things up here)
Dec 5 '07 #2
amitpatel66
2,367 Expert 2GB
Hi!
I can work out the first day of the month using this code:
Expand|Select|Wrap|Line Numbers
  1. select 
  2.    to_char(
  3.       trunc(sysdate,'MM'),'DD/MM/YYYY'
  4.    ) as First_Day
  5. from dual;
But why does it not work when I plug it into my code?
Expand|Select|Wrap|Line Numbers
  1. select DUE_DATE from vw_current_task
  2. where
  3. DUE_DATE >  to_date(trunc(sysdate,'MM'),'DD/MM/YYYY') - 6
  4. order by DUE_DATE asc
Sorry its probably a really simple one.
Hi SomaRich,

Welcome to TSDN!!

Please make sure you follow POSTING GUIDELINES every time you POST in this forum

Thanks
MODERATOR
Dec 6 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: GO | last post by:
I have a custom Perl application (programmed by myself) that is used to rename files. For some reason, within the last month, it is no longer working properly. Prior to this I've been using it for...
11
by: Solomon Grundy | last post by:
Select trunc(sysdate,'MM') from dual of course gives me the first day of the month, may I ask what the query is to find the first saturday of the month?
1
by: db | last post by:
I am trying to build a db in Access2000 that will allow me to calculate month end returns on investments. I am about to try VBA for the first time, but wonder if I can do what I want by using...
5
by: BlackFireNova | last post by:
I need to write a report in which one part shows a count of how many total records fall within the working days (Monday - Friday) inside of a (prompted) given date range, in a particular...
0
by: Rob | last post by:
Access Gurus, This script was used to view the reports on the web by choosing a date on or before the current day.Recently it stopped working and i am not able to figure where the problem is. ...
1
by: Rob | last post by:
Access Gurus, This script was used to view the reports on the web by choosing a date on or before the current day.Recently it stopped working and i am not able to figure where the problem is. ...
3
by: Alec | last post by:
Hi All, I want to create a report listing all the people's birthdays that are in the current month, in other words, if I clicked to open the report today, it would show everyone who's birthday...
5
by: tolcis | last post by:
Hi! I have a query that has to return bunch of data based on the calendar month. I have to make sure that it will return data to me for 28 days if it is February and for 31 if it is August(for...
3
by: whatchamakeofit | last post by:
hi. i have the following code: $now=getdate(); $currMonth = $now; $currYear = $now; $currDay = $now; for($i=1; $i<=12; $i++){ $month = date("F",mktime(0, 0, 0, $i)); if ($month ==...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.