January 9, 2014 at 8:06 am
I'm trying to convert SQL Server Dates to Char Field in the format of DD/MM/YY.
Sometimes the SQL Server Column had the Time (all zeros) and in other cases it may contain the time.
Any help would be appreciated.
Thanks.
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
January 9, 2014 at 8:20 am
I got it.
,RIGHT('0' + CAST(DATEPART(mm,PURCHASE_DATE) as varchar(2)),2)+ '/' +
RIGHT('0' + CAST(DATEPART(dd,PURCHASE_DATE) as varchar(2)),2)+ '/' +
CAST(DATEPART(yyyy,PURCHASE_DATE) as varchar(4)) AS PURCHASE_DATE
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
January 9, 2014 at 8:25 am
I came up with something like this:
declare @SSDate datetime = '01/22/2001'
declare @CharSSDate varchar(10)
set @CharSSDate = right('0' + cast(MONTH(@SSDate) as varchar(2)),2) + '/' + right('0' + cast(DAY(@SSDate) as varchar(2)),2) + '/' + RIGHT(CAST(year(@SSDate) as varchar(4)),2)
print @SSDate
print @CharSSDate
Looks much like yours however I used the MONTH, DAY, & YEAR functions.
Kurt
Kurt W. Zimmerman
SR DBA
Lefrak Organization
New York, NY
http://www.linkedin.com/in/kurtwzimmerman
January 9, 2014 at 8:26 am
Your first post indicated you wanted a 2 digit year. Your solution provides you a 4 digit year. Just saying.
Kurt
Kurt W. Zimmerman
SR DBA
Lefrak Organization
New York, NY
http://www.linkedin.com/in/kurtwzimmerman
January 9, 2014 at 9:02 am
Oh my, so much code! My laziness wants to write less.
declare @SSDate datetime = '20140109'
declare @CharSSDate char(10)
SELECT CONVERT( char(10), @SSDate, 103), --4-digit year
STUFF( CONVERT( char(10), @SSDate, 103), 7, 2, '')--2-digit year
You shouldn't store dates as strings. It will just give you headaches and use more space than needed. You should leave format for presentation only.
January 9, 2014 at 9:17 am
Luis Cazares (1/9/2014)
Oh my, so much code! My laziness wants to write less.
declare @SSDate datetime = '20140109'
declare @CharSSDate char(10)
SELECT CONVERT( char(10), @SSDate, 103), --4-digit year
STUFF( CONVERT( char(10), @SSDate, 103), 7, 2, '')--2-digit year
You shouldn't store dates as strings. It will just give you headaches and use more space than needed. You should leave format for presentation only.
Actually, don't need the STUFF:
declare @SSDate datetime = '20140109'
declare @CharSSDate char(10)
SELECT CONVERT( char(10), @SSDate, 103), --4-digit year
CONVERT( char(10), @SSDate, 3) --2-digit year
Books Online is your friend:
http://msdn.microsoft.com/en-us/library/ms187928(v=sql.100).aspx
January 9, 2014 at 9:29 am
Kurt W. Zimmerman (1/9/2014)
Your first post indicated you wanted a 2 digit year. Your solution provides you a 4 digit year. Just saying.Kurt
Sorry about the Year.
It first was supposed to be a 2 digit year but evolved to be a 4 digit year.
I forgot to mention this..
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
January 9, 2014 at 9:32 am
Luis Cazares (1/9/2014)
Oh my, so much code! My laziness wants to write less.
declare @SSDate datetime = '20140109'
declare @CharSSDate char(10)
SELECT CONVERT( char(10), @SSDate, 103), --4-digit year
STUFF( CONVERT( char(10), @SSDate, 103), 7, 2, '')--2-digit year
You shouldn't store dates as strings. It will just give you headaches and use more space than needed. You should leave format for presentation only.
I agree with you that the dates should not be stored as strings.
The Data is coming from one vendor (AS400) with mixed dates and strings.
The destination Vendor (Oracle wants the data in a string format.
Nice code.
Thanks.
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
January 9, 2014 at 9:33 am
Thanks Lynn. 🙂
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
January 24, 2014 at 6:27 pm
Lynn Pettis (1/9/2014)
Luis Cazares (1/9/2014)
Oh my, so much code! My laziness wants to write less.
declare @SSDate datetime = '20140109'
declare @CharSSDate char(10)
SELECT CONVERT( char(10), @SSDate, 103), --4-digit year
STUFF( CONVERT( char(10), @SSDate, 103), 7, 2, '')--2-digit year
You shouldn't store dates as strings. It will just give you headaches and use more space than needed. You should leave format for presentation only.
Actually, don't need the STUFF:
declare @SSDate datetime = '20140109'
declare @CharSSDate char(10)
SELECT CONVERT( char(10), @SSDate, 103), --4-digit year
CONVERT( char(10), @SSDate, 3) --2-digit year
Books Online is your friend:
http://msdn.microsoft.com/en-us/library/ms187928(v=sql.100).aspx
+1000
--Jeff Moden
Change is inevitable... Change for the better is not.
January 28, 2014 at 7:30 am
Maybe I'm thinking too simple for this question, but why not use:
select convert(VarChar(32), PURCHASE_DATE, 3)
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply