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.
3 14310
Hi, LimaCharlie,
You can use this UDF function: - CREATE FUNCTION ToDate(@StrDate VARCHAR(20))
-
RETURNS DATETIME
-
AS
-
BEGIN
-
DECLARE @Dt DATETIME, @FormattedDate VARCHAR(20)
-
SET @FormattedDate = LEFT(@StrDate, 3) + ' ' + RIGHT(@StrDate, 1) + ' 20' + SUBSTRING(@StrDate, 4, 2)
-
SET @Dt = CAST(@FormattedDate AS DATETIME)
-
RETURN @Dt
-
END
-
-
Then you can sort correctly using this query: - SELECT PayPeriod
-
FROM Table
-
ORDER BY dbo.ToDate(PayPeriod)
Or you can use a single query (without UDF): - SELECT PayPeriod
-
FROM Table
-
ORDER BY CAST(LEFT(PayPeriod, 3) + ' ' + RIGHT(PayPeriod, 1) + ' 20' + SUBSTRING(PayPeriod, 4, 2) AS DATETIME)
But it looks messy.
I hope this helps
and to answer your 2 other questions...
To get the month number, you can use this function: - CREATE FUNCTION ToMonth(@StrMonth CHAR(3))
-
RETURNS TINYINT
-
AS
-
BEGIN
-
DECLARE @Month TINYINT, @FormattedDate VARCHAR(20)
-
SET @FormattedDate = @StrMonth + ' 1 2000'
-
SET @Month = MONTH(CAST(@FormattedDate AS DATETIME))
-
RETURN @Month
-
END
To get number of days in a month for a given year (because of feb): - CREATE FUNCTION NoDays(@Month TINYINT, @Year INT)
-
RETURNS TINYINT
-
AS
-
BEGIN
-
DECLARE @NoDays TINYINT, @FormattedDate VARCHAR(20), @DT DATETIME
-
SET @FormattedDate = CAST(@Month AS VARCHAR) + '/1/' + CAST(@Year AS VARCHAR)
-
SET @DT = CAST(@FormattedDate AS DATETIME)
-
SET @DT = DATEADD(m, 1, @DT)
-
SET @DT = DATEADD(d, -1, @DT)
-
SET @NoDays = DAY(@DT)
-
RETURN @NoDays
-
END
Hi galexyus,
Thank you so much!
I got it already.
You're such a big help.
Thanks.
God Bless.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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)) + '/'...
|
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...
|
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
|
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){
|
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:
| |
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...
|
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?
|
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
|
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...
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
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
| |
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...
| | |