Monthly Archive for August, 2006

Sql Date Format - Month and Year Only

i remember there is one time where i have to edit an old module where in the date field in sql is not a date data type but instead, it is a char type… they did that because they wanted to store only the month and year of the date in sql… at first it wasn’t a big deal after all until i have to return the data with a given date range… now that’s a problem… my problem…

since the date is in char type, it won’t be easy to select the data with a given range..
and so i came up with this solution… this is how i make a date range with char data type..

say we have to select all datas with these given date range: from 04/2003 to 03/2006

my query would be:

select field1, field2, dateField
from   table1
where  (cast(substring(dateField,1,2) as int) >= 4 and cast(substring(dateField,4,4) as int)=2003) or
       (cast(substring(dateField,1,2) as int) <= 3 and cast(substring(dateField,4,4) as int)=2006) or
       (cast(substring(dateField,1,2) as int) between 1 and 12 AND cast(substring(dateField,4,4)
       as int) > 2003 and cast(substring(dateField,4,4) as int) < 2006

now lets disect the code…
for the first where condition:
(cast(substring(dateField,1,2) as int) >= 4 and cast(substring(dateField,4,4) as int)=2003)
this will return all the data with the date range from 04/2003 to 12/2003…

for the second where condition:
(cast(substring(dateField,1,2) as int) <= 3 and cast(substring(dateField,4,4) as int)=2006)
this will return all the data with the date range from 01/2006 to 03/2006..

for the third and last where condition:
(cast(substring(dateField,1,2) as int) between 1 and 12 AND cast(substring(dateField,4,4) as int) > 2003 and cast(substring(dateField,4,4) as int) < 2006
this will return all the data with the date from 01/2004 to 12/2004 and from 01/2005 to 12/2005

if you don’t know how to use the substring function in sql… just google it… there are lots of threads and forums out there…

a smarter way to do this is convert the varchar type date into a real date format… in that way, you can easily make a date range because it is now in date type…
say you have “08/2006″ as august 2006, all you have to do is insert a “01/” on the left side to make it “01/08/2006″.. after that you can now convert it to date format… since you only need the month and year, the value of the day does not matter…

to insert the “01/” in the left side of your varchar date and convert it to date format:

convert(datetime, ‘01/’ + dateField,103)
*101 means the date format will be dd/mm/yy

now every time you need to the month and year.. use the code month() to return only the value of month and year() for the value of year…

sample:
say we have “30/03/1984″ in dateField

select month(dateField)
from sampleTbl

this will return “03″… and if you use the year() function.. it will return “1984″

another way is to use a contiguous string format… just convert your varchar date into this format ‘200608′ (means agust 2006).. then use <,>,= operators to create a range… just use substring() function to arrange your data into this format…

Prayers Do Come True

There are three essential parts in prayer. one is you pray, second is you believe, third is you act. All of these are needed for your prayer to be answered.

You pray, its too obvious this one is needed in prayer. duh?!? but of course you have to know what’s for you and what’s not for you”. You know what i mean!!!

You believe, I remember in one of spongebob episodes wherein spongebob said: “If you believe in yourself and with a tiny pinch of magic, all of your dreams can come true.” Hehe, that’s not my point. What i mean is believe that God will answer your prayer, as in really believe in Him, not even a single doubt in your heart. Kind of like nothing to worry about. I know it’s hard to do but its a must. Once you worry, you already doubted the power of God, and if you have a doubt on God, then you should have not pray at all.

You act, another essential thing in prayer. Mercy is in God; deeds are in men. What you pray for won’t pop-up in just a blink of an eye. Of course you have to strive for it, do what ever it is you need and have to do.

with all these stuffs… your prayers will be answered… 100% sure… if you think you’ve done all of these and your prayer hasn’t been answered yet… then check the second part.. maybe there’s too little doubt in your heart.. too little to be seen… and remember, you can’t fool God so don’t even think of faking it…

Old Days

I remember last week when i got a free time and ended up reading my friendster profile specifically my friend’s testimonials to me, it was a really great way to spend a dead afternoon. A great time really! I really felt good reading it. I started to realize how I became part of their lives, friends who are real, friends from college and high school. I never thought it was that fun reading it. It makes my heart beat faster, memories run through my brain, old good days. Everyone’s laughing, foolin’ around, and in short everybody’s happy. Different images flashes in the air as i read every one of it, oh how i really miss the old days back from high school and college. I never thought growing up is this hard. I just left school for about 4 months and I already miss everything, sigh! When will the old times happen again?

Comment

I just recently started my technical blog in wordpress.com. I’m just a newbie in my job so you could count my blog post in your fingers. I post evry problems that i encountered in my career as a software developer. I also explained evry details how i manage to solve it. After a few postings, i started to get tired of doing it regularly, but not until this single email from wordpress tellin’ me that i got my first comment flashes in my screen. My heart beats so fast and i don’t know why, maybe so eager to know what kind of comment would it be. Then there it is, a simple thank you message from someone who lives in amsterdam i think, saying that she also had encountered the same problem as i had and thanks to me that i posted a solution for it. Well it really put a big smile on my face, i never thought that I could help someone on their career through my blog. Now I’m gonna’ do my tech blog regularly thinkin’ that somehow I’m helpin’ somebody in their career on the other part of the world.

check out my tech blog at http://richardsantos.wordpress.com/technical/

SQL isZero() Solution For Divide By Zero Error

I had a query once that contains a divide operation….

SELECT   x, y, ROUND(x/y) as quotient
FROM     dataTbl

It always returns a correct value…
but divide by zero error occurs whenever
there is a zero(0) not NULL value in y column…
now, what i want is instead of having
an error, i want it to return a zero(0) value also…
so I wonder if there is a function isZero(), I can simply
change the y value from 0 to NULL to avoid such error…
I was thinking of a code like this: isZero(y, NULL)…
unfortunately, there is no such functions…
I googled for the correct syntax and I came up with
the: NULLIF(fieldName, condition) function…

SELECT   x, y, ROUND(x/NULLIF(y, 0)) as quotient
FROM     dataTbl

by using the query above, divide by zero error is impossible…
what happen is SQL returns a NULL in y column if its value is zero(0)…
though the error was avoided… the result still does not satisfy me…
it only returns a NULL value in quotient field instead of zero(0)…
well, the solution is simple:

SELECT   x, y, ISNULL(ROUND(x/NULLIF(y, 0)), 0) as quotient
FROM     dataTbl

by using the ISNULL(fieldName, replacementValue) function,
It sort of like reversed the effect of NULLIF function…
what happen in this function is, if the value IS NULL
then replace it with what ever the replacementValue is…
in my query, it replaces the NULL value by zero(0)…

now that’s satisfying…. divide by zero error was avoided
and quotient field is never NULL….