473,407 Members | 2,546 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,407 software developers and data experts.

Suffix in dates

nitindel
Hi All,
I want to add Suffix in dates like nd,st,rd,th to dates like 2,1,3,4 respectively.....

What is the function used in Sql Server and oracle both??

Thanks & Regards
Nitin Sharma
Software Engineer
.Net Technologies
Jun 26 '08 #1
5 8227
deepuv04
227 Expert 100+
Hi,
here is the sample query (sql server) to give suffix to the date
sample code:
Expand|Select|Wrap|Line Numbers
  1.  
  2. DECLARE @Date datetime
  3. -- I am using @date variables, you can change it to column name if you are retrieveing data from table
  4. set @Date = Getdate()
  5.  
  6.   SELECT   CASE WHEN DAY(@Date) in (1,21,31) THEN convert(varchar,DAY(@Date)) + 'st'
  7.             WHEN DAY(@Date) IN (2,22) then convert(varchar,DAY(@Date)) + 'nd'
  8.             WHEN DAY(@Date) IN (3,23) then convert(varchar,DAY(@Date)) + 'rd' 
  9.             ELSE convert(varchar,DAY(@Date)) + 'th ' end +
  10.        DATENAME(month, @Date)  + ', ' + convert(varchar,year(@Date)) as CertificationDate
  11.  
thanks
Jun 26 '08 #2
Hi,
here is the sample query (sql server) to give suffix to the date
sample code:
Expand|Select|Wrap|Line Numbers
  1.  
  2. DECLARE @Date datetime
  3. -- I am using @date variables, you can change it to column name if you are retrieveing data from table
  4. set @Date = Getdate()
  5.  
  6.   SELECT   CASE WHEN DAY(@Date) in (1,21,31) THEN convert(varchar,DAY(@Date)) + 'st'
  7.             WHEN DAY(@Date) IN (2,22) then convert(varchar,DAY(@Date)) + 'nd'
  8.             WHEN DAY(@Date) IN (3,23) then convert(varchar,DAY(@Date)) + 'rd' 
  9.             ELSE convert(varchar,DAY(@Date)) + 'th ' end +
  10.        DATENAME(month, @Date)  + ', ' + convert(varchar,year(@Date)) as CertificationDate
  11.  
thanks


Hi Dear..
Thank you....

I am using the query sent by u...but m having problem in retrrieving the date as like ur query is returning...
I have a date field in the table with datetime as a datatype and i have replaced the Getdate() function with (Select date from tablename)..that is it....


Please tell what all i have to change....

My table name is XYZ
My field name is Date..

Thanks
Nitin
Jun 26 '08 #3
deepuv04
227 Expert 100+
Hi Dear..
Thank you....

I am using the query sent by u...but m having problem in retrrieving the date as like ur query is returning...
I have a date field in the table with datetime as a datatype and i have replaced the Getdate() function with (Select date from tablename)..that is it....


Please tell what all i have to change....

My table name is XYZ
My field name is Date..

Thanks
Nitin
Hi,
what i mean to say is use the query as follows:

Expand|Select|Wrap|Line Numbers
  1. SELECT   CASE WHEN DAY(Date) in (1,21,31) THEN convert(varchar,DAY(Date)) + 'st'
  2.             WHEN DAY(Date) IN (2,22) then convert(varchar,DAY(Date)) + 'nd'
  3.             WHEN DAY(Date) IN (3,23) then convert(varchar,DAY(Date)) + 'rd'
  4.             ELSE convert(varchar,DAY(Date)) + 'th ' end +
  5.             DATENAME(month, Date)  + ', ' + convert(varchar,year(Date)) as CertificationDate
  6. FROM XYZ
  7.  
thanks
Jun 26 '08 #4
Hi,
what i mean to say is use the query as follows:

Expand|Select|Wrap|Line Numbers
  1. SELECT   CASE WHEN DAY(Date) in (1,21,31) THEN convert(varchar,DAY(Date)) + 'st'
  2.             WHEN DAY(Date) IN (2,22) then convert(varchar,DAY(Date)) + 'nd'
  3.             WHEN DAY(Date) IN (3,23) then convert(varchar,DAY(Date)) + 'rd'
  4.             ELSE convert(varchar,DAY(Date)) + 'th ' end +
  5.             DATENAME(month, Date)  + ', ' + convert(varchar,year(Date)) as CertificationDate
  6. FROM XYZ
  7.  
thanks


Thanks a lot..My dear..!! It worked..

Also..tell..What if it is not a date and a simple number field more than 30 or 31 ...

Waiting for the reply...!!

Thanks ,
Nitin Sharma
Jun 26 '08 #5
deepuv04
227 Expert 100+
Thanks a lot..My dear..!! It worked..

Also..tell..What if it is not a date and a simple number field more than 30 or 31 ...

Waiting for the reply...!!

Thanks ,
Nitin Sharma
Hi,

sql server treats number 1 as 1900-01-01. (yyyy-mm-dd)

If you given any number then it starts counting from the date above, and convertrs it as another date. like

if you give the date as 0 then it takes 1900-01-01,
if the date is given as 32 then it will be 1900-02-02

for more details do as follows you will come to know exactly what happening

declare @date as datetime

set @date = 1
-- try with different numbers values

select @date as date


thanks
Jun 26 '08 #6

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

Similar topics

0
by: p.kosina | last post by:
Its just for my convienience: When saving new file in IDLE I HAVE to always manually add suffix .py, otherwise it is saved without ANY suffix. Can it be set somewhere? Thank you Pavel
12
by: Iain Downie | last post by:
Dear list, we are getting a few folk trying to register for our birdwatching surveys with emails of the form: aname@phonecoop.coop, in other words with a 4 character ending (other examples are...
3
by: Green | last post by:
Hi, I have a question that when i surf the internet, i found out that using ..aspx suffix means using asp.net, using .jsp means using javaserver page, etc. But some don't have any suffix. For...
12
by: Dixie | last post by:
I am trying to calculate the number of workdays between two dates with regards to holidays as well. I have used Arvin Meyer's code on the Access Web, but as I am in Australia and my date format is...
9
by: Hamish M | last post by:
Hi I am interested in opinions on this topic. I have heard that a suffix is not a good solution and type casts are much better for example. ...
4
by: Denis Jevon | last post by:
I have a problem which is probably simple to solve but I can't find the answer. In the past I have added pages to sites I have built with names like aboutus.html and when I or anyone else wants to...
1
by: SARAHE | last post by:
I have a C++ programming problem to extract a suffix such as Jr. or Sr. from a string that may vary each time. We are currently working with one assigned variable but I wanted to know how to code it...
3
by: Cyber dorkz | last post by:
hello, i've got this problem , as example : long number = 2000000000 my manual says that you must place a suffix after 2000000000 : long number = 2000000000L. suffix L means int must be...
4
by: Weatherman | last post by:
I've searched for an answer to this and can't find one. I can't view pages with the PHP suffix. The basic (top) part comes up in my browser, but the part with the PHP element does not. I've...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...

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.