SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


to_char


to_char

Author
Message
newbieuser
newbieuser
SSC Eights!
SSC Eights! (865 reputation)SSC Eights! (865 reputation)SSC Eights! (865 reputation)SSC Eights! (865 reputation)SSC Eights! (865 reputation)SSC Eights! (865 reputation)SSC Eights! (865 reputation)SSC Eights! (865 reputation)

Group: General Forum Members
Points: 865 Visits: 915
Hi all,

In oracle I use this query to format the date field. chg_dt is a date field in addchg table

select to_char(chg_dt,'YYYY/DD/MM HH:MI') as date from addchg;

DATECHG
----------------
2002/11/11 12:00

How can I do this in sql server?? Seems to be too complicated... I tried using convert but couldnt get this format

select convert(datetime, chg_dt,112) from addchg

I keep getting 2002-11-11 12:00:00:000

Please help thanks
Dave Ballantyne
Dave Ballantyne
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2938 Visits: 8370
Please see convert in BOL for other 'styles' of conversion



Clear Sky SQL
My Blog
Lowell
Lowell
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28449 Visits: 39963
there's not a specific predefined format for the string you are looking for;
two of the formats are pretty close, so by combining them you can get exactly what you want:

declare @date datetime
set @date=getdate()
SELECT CONVERT(VARCHAR,@date,111) + ' ' + LEFT(CONVERT(VARCHAR,@date,114),5)
--results:
2009/08/20 11:09



Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

newbieuser
newbieuser
SSC Eights!
SSC Eights! (865 reputation)SSC Eights! (865 reputation)SSC Eights! (865 reputation)SSC Eights! (865 reputation)SSC Eights! (865 reputation)SSC Eights! (865 reputation)SSC Eights! (865 reputation)SSC Eights! (865 reputation)

Group: General Forum Members
Points: 865 Visits: 915
Thanks I got that working.

But I've another problem. I have a column a with datatype varchar(19). If I insert a string with 15 characters it should automatically insert spaces to the left for 4 spaces..

I tried using left() but, not sure how I can mention this in the insert..

create table test1 (a varchar(19))

insert into test1 values ('123456789123456')

select left(' ' +a,19) from test1

This inserts spaces when I select.. But how can I automate this if there are 16 characters to insert 3 spaces ..

Thanks
Lowell
Lowell
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28449 Visits: 39963
ok, i think you want to left pad the string so it is right aligned, correct?
two ways I can think of, using either the SPACE or REPLICATE functions, and grabbing the RIGHT of the string.


insert into test1
select RIGHT(SPACE(19) + '456',19) UNION ALL
select RIGHT(replicate(' ',19) + '876842',19)

insert into test1
select RIGHT(replicate(' ',19) + YourColumnName,19) From YourTable




replicate is handy if you need preceeding zeros instead of spaces

Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

newbieuser
newbieuser
SSC Eights!
SSC Eights! (865 reputation)SSC Eights! (865 reputation)SSC Eights! (865 reputation)SSC Eights! (865 reputation)SSC Eights! (865 reputation)SSC Eights! (865 reputation)SSC Eights! (865 reputation)SSC Eights! (865 reputation)

Group: General Forum Members
Points: 865 Visits: 915
Thanks a million
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)

Group: General Forum Members
Points: 87192 Visits: 41113
As a bit of a sidebar... unless this formatting you want to do is strictly for output to a file, you should let the GUI do date formatting so that local "regional" settings will correctly format the date according to the region of the world the user is in. It also takes a bit of a load off the server. If the GUI is a report generator, let that do the formatting as well. For goodness sake, NEVER store formatted dates in the database because you will have to do conversions implicitly or explicitly to do even the simplest of date math in the future.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search