MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now!
  #1  
Old January 14th, 2004, 11:05 AM
ecads ecads is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 18 ecads User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 sec
Reputation Power: 0
manual query date formating

I am trying to setup some software that allows me to define expressions manually.

Using the SQL Enterprise manager on a certain table, I am trying to run a query but I am stuck.

There is a field in TableA that is called expDate which is set as 01/12/2004 as the value.

What I am trying to do is convert the output to say 12-Jan-2004 using a built in function, but I can't seem to find the right function.

Does anybody know the function to do this? Here is what I was thinking might work (but didn't):

SELECT DATE_FORMAT('dd-mmm-yyyy',expDate) AS expire from TableA WHERE (lastName = 'me')

If I leave out the DATE_FORMAT and just put SELECT expDate it returns one row named expire with the value of 01/12/2004

Any Ideas? Thanks.

Reply With Quote
  #2  
Old January 14th, 2004, 01:57 PM
shammat shammat is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2003
Location: Munich, Bavaria
Posts: 971 shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 44 m 15 sec
Reputation Power: 57
Quote:
Here is what I was thinking might work
What made you think it might work?

Did you read the manual?

Reply With Quote
  #3  
Old January 14th, 2004, 02:13 PM
ecads ecads is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 18 ecads User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 sec
Reputation Power: 0
I'm a linux person who normally uses mySQL for stuff. I'm being forced to try and make this work. It's hard finding an answer on the internet or even in a manual when the wording can be considred different from what I am used to.

If it was a mySQL based server, I would have no problem finding the answer:

date_format('field','formatting') as newfield

so I figured I give that at try and it failed. I was just asking a legit question and trying to find an answer. Looking at that link really hasn't told me much to the degree what I was looking for.

I was hoping that somebody here has run into this or knows how to do it and would kindly share an example, that is all I was hoping for.

Reply With Quote
  #4  
Old January 14th, 2004, 02:32 PM
ecads ecads is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 18 ecads User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 sec
Reputation Power: 0
Ok. I came up with this, but does anybody know of an easier way to write this to acheive the same effect?:

DATENAME([day],expDate)+'-'+SUBSTRING(DATENAME([month],expDate),0,4)+'-'+DATENAME([year],expDate) as expire

Returns (example): 14-Jan-2004 from the actual database entry 01/14/2004.

And shammat thanks for the link , but it still is new water for me from what I am used to

Reply With Quote
  #5  
Old January 14th, 2004, 03:18 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,343 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 8 h 5 m 22 sec
Reputation Power: 891
easier? that's somewhat subjective

i would use CONVERT style 113 into a CHAR(11) field and then REPLACE to change the spaces to dashes

select replace(convert(char(11),expDate,113),' ','-')

if you're using sql server, you should have Books OnLine (BOL) on one of your drives somewhere

if not, holler, i'll give you the link where you can download the whole thing (it's 38megs or something)

or just use BOL on the web like i do
__________________
r937.com | rudy.ca

Reply With Quote
  #6  
Old January 14th, 2004, 03:34 PM
ecads ecads is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 18 ecads User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 sec
Reputation Power: 0
Thank you Much easier and efficient.

I'll have to check out those BOL and see if I can solve the next problem

Do you know if it's possbile to nest multiple REPLACE statements to parse a field more than once?

Ex: M,0,23|Tu,0,23|W,0,23

Into:
M 0-23
Tu 0-23
W 0-23

And so on.. (Turning the | into a newline break and then changing the first ',' into a space and then the next into a - )....

Thanks for your help with the date item.. thats a big timesaver

Reply With Quote
  #7  
Old January 14th, 2004, 03:54 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,343 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 8 h 5 m 22 sec
Reputation Power: 891
Quote:
Originally posted by ecads
Do you know if it's possbile to nest multiple REPLACE statements to parse a field more than once?
yes it is, and at this point you should be testing stuff like that yourself, it's certainly going to get you answers faster than any forum

Reply With Quote
  #8  
Old January 14th, 2004, 03:59 PM
ecads ecads is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 18 ecads User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 sec
Reputation Power: 0
Thats what I've been doing and came up with that monsterous DATENAME one. I'll find it eventually, just sucks when your boss is saying he needs it in a short amount of time

I tried doing something like REPLACE(fieldName,'aa','bb')+REPLACE(fieldName,'cc','dd') as newField

But that just seemed to merge it into a bigger mess heh. Thanks for your help, i'll poke around more and see what I can come up with.

Reply With Quote
  #9  
Old January 14th, 2004, 04:12 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,343 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 8 h 5 m 22 sec
Reputation Power: 891
you were concatenating, you just need to nest them, as you suspected in the previous post

replace(replace(fieldName,'aa','bb'),'cc','dd'))


Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > manual query date formating


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway