Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

to_char Expand / Collapse
Author
Message
Posted Thursday, August 20, 2009 8:47 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, April 23, 2014 3:06 PM
Points: 319, Visits: 757
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
Post #774285
Posted Thursday, August 20, 2009 8:54 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 1:33 AM
Points: 1,949, Visits: 8,310
Please see convert in BOL for other 'styles' of conversion



Clear Sky SQL
My Blog
Kent user group
Post #774292
Posted Thursday, August 20, 2009 9:09 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:24 PM
Points: 12,906, Visits: 31,984
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #774305
Posted Thursday, August 20, 2009 9:13 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, April 23, 2014 3:06 PM
Points: 319, Visits: 757
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
Post #774308
Posted Thursday, August 20, 2009 9:21 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:24 PM
Points: 12,906, Visits: 31,984
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #774318
Posted Thursday, August 20, 2009 9:24 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, April 23, 2014 3:06 PM
Points: 319, Visits: 757
Thanks a million
Post #774322
Posted Thursday, August 20, 2009 7:03 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 12:56 PM
Points: 36,958, Visits: 31,466
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #774710
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse