473,799 Members | 2,746 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert month into its number equivalent...(E x. JAN=1,FEB=2...)

17 New Member
Good Day to Everyone,

Need help!

I have this SQL table w/ fieldname "PayPeriod" . Sample value of PayPeriod are SEP06-1, SEP06-2, OCT06-1, OCT06-2... Wherein the first 3 chars is the month, 06 is the year, 1 or 2 is the cutoff where 1 means the 2nd half of the previous month and 2 is the first half of the current month.

Now my questions are:
1.) How can I select these values sorted according to date. Since this is a character data type. Sorting is alphabetical. I need to sort it according to date where JAN05-1 is the first record and the last record is DEC06-2 (assuming that the oldest year is 05 and 06 is the latest).
2.) How can I convert the 3 char month into its number equivalent. Example, JAN is equal to 1, FEB is 2, MAR is 3....
3.) How can I count the no. of days in a month? Is there a function that would result to the no. of days in a given month?

Thank you so much in advance.
Oct 17 '06 #1
3 14310
galexyus
15 New Member
Hi, LimaCharlie,

You can use this UDF function:

Expand|Select|Wrap|Line Numbers
  1. CREATE FUNCTION ToDate(@StrDate VARCHAR(20))
  2. RETURNS DATETIME
  3. AS
  4. BEGIN
  5.     DECLARE @Dt DATETIME, @FormattedDate VARCHAR(20)
  6.     SET @FormattedDate = LEFT(@StrDate, 3) + ' ' + RIGHT(@StrDate, 1) + ' 20' + SUBSTRING(@StrDate, 4, 2)
  7.     SET @Dt = CAST(@FormattedDate AS DATETIME)
  8.     RETURN @Dt
  9. END
  10.  
  11.  
Then you can sort correctly using this query:

Expand|Select|Wrap|Line Numbers
  1. SELECT PayPeriod
  2. FROM Table
  3. ORDER BY dbo.ToDate(PayPeriod)
Or you can use a single query (without UDF):

Expand|Select|Wrap|Line Numbers
  1. SELECT PayPeriod
  2. FROM Table
  3. ORDER BY CAST(LEFT(PayPeriod, 3) + ' ' + RIGHT(PayPeriod, 1) + ' 20' + SUBSTRING(PayPeriod, 4, 2) AS DATETIME)
But it looks messy.

I hope this helps
Oct 17 '06 #2
galexyus
15 New Member
and to answer your 2 other questions...

To get the month number, you can use this function:

Expand|Select|Wrap|Line Numbers
  1. CREATE FUNCTION ToMonth(@StrMonth CHAR(3))
  2. RETURNS TINYINT
  3. AS
  4. BEGIN
  5.     DECLARE @Month TINYINT, @FormattedDate VARCHAR(20)
  6.     SET @FormattedDate = @StrMonth + ' 1 2000'
  7.     SET @Month = MONTH(CAST(@FormattedDate AS DATETIME))
  8.     RETURN @Month
  9. END
To get number of days in a month for a given year (because of feb):

Expand|Select|Wrap|Line Numbers
  1. CREATE FUNCTION NoDays(@Month TINYINT, @Year INT)
  2. RETURNS TINYINT
  3. AS
  4. BEGIN
  5.     DECLARE @NoDays TINYINT, @FormattedDate VARCHAR(20), @DT DATETIME
  6.     SET @FormattedDate = CAST(@Month AS VARCHAR) + '/1/' + CAST(@Year AS VARCHAR)
  7.     SET @DT = CAST(@FormattedDate AS DATETIME)
  8.     SET @DT = DATEADD(m, 1, @DT)
  9.     SET @DT = DATEADD(d, -1, @DT)
  10.     SET @NoDays = DAY(@DT)
  11.     RETURN @NoDays
  12. END
Oct 17 '06 #3
LimaCharlie
17 New Member
Hi galexyus,

Thank you so much!
I got it already.
You're such a big help.

Thanks.
God Bless.
Oct 18 '06 #4

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

Similar topics

19
7291
by: Lauren Quantrell | last post by:
I have a stored procedure using Convert where the exact same Convert string works in the SELECT portion of the procedure but fails in the WHERE portion. The entire SP is listed below. Specifically, I have a problem with this portion in the WHERE clause: DATEADD(Day,tblMyEventTableName.ReminderDays, @DateNow) Between CONVERT(smalldatetime,str(DATEPART(Month, @DateNow)+1) + '/' + str(DATEPART(Day, tblMyEventTableName.TaskDateTime)) + '/'...
4
5392
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and calculate days, months, and years. This is not for a college course. It's for my own personal genealogy website. I'm stumped about the code. I'm working on it but not making much progress. Is there any free code available anywhere? I know it...
6
77857
by: Ashish Sheth | last post by:
Hi All, In C#, How can I get the difference between two dates in number of months? I tried to use the Substract method of the DateTime class and it is giving me the difference in TimeSpan,From which I can get the duration in days, hours and so.. but how can I get the difference in months? Please reply ASAP. it's urgent. -- regards, Ashish Sheth
65
21500
by: kyle.tk | last post by:
I am trying to write a function to convert an ipv4 address that is held in the string char *ip to its long value equivalent. Here is what I have right now, but I can't seem to get it to work. #include <string.h> #include <stdio.h> /* Convert an ipv4 address to long integer */ /* "192.168.1.1" --> 3232235777 */ unsigned long iptol(char *ip){
4
10112
by: gabor | last post by:
hi, i'm trying to get the date of the day one month ago. for example: today = 12.apr.2006 one-month-ago = 12.mar.2006 so:
10
7338
by: Bill | last post by:
Hello -- I'm parsing the output of the finger command, and was wondering something...If I'm given a month abbrievation (such as "Jan"), what's the best way to figure out the month number? I see that there's something called "month_abbr" in the calendar module. However, when I try to do calendar.month_abbr.index("Jan"), I get "_localized_month instance has no attribute 'index'." So it seems that month_abbr isn't a regular list. I'm...
8
1673
by: Richard | last post by:
I wish to have a month select and next to it a day select. Is there no way at all for the day field to pick up the selected month without a previous submit on the part of the user?
5
10055
by: Amitkhare82 | last post by:
Hi All, How can we conevrt the Month no into its equivalent name..(Ex. 1= Jan,2= Feb...) & IF you can tell me how to contatenate the Month & Year in one column. Here is the query which I am writing. SELECT Datepart(Year, UserData.tp_Created), MONTH(UserData.tp_Created), FROM TABEL1
5
1647
by: Lado.Leskovec | last post by:
Hi! I'm writing a script that would sort some events and calculate the sum of them all in the end of the month, which should be equal to the number of days in a month. It seems to be working well, but for some reason on march it calculates one hour less (30 days and 23:00:00) and in october one hour more (31 days 1:00:00). I've tried different years not just 2007. It's all the same. Anyone knows a good reason why this might happen as it...
0
10488
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10237
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10029
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9077
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7567
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4144
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.