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


Convert GETDATE() to NUMBER


Convert GETDATE() to NUMBER

Author
Message
rahulsony111
rahulsony111
Old Hand
Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)

Group: General Forum Members
Points: 303 Visits: 265
In a SSIS package i have a column with value GETDATE().
In target table column is defined as number(10,0).
How can i convert or cast?
I tried in internet but getting errors saying as invalid expression.
Please help me out
Lowell
Lowell
One Orange Chip
One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)

Group: General Forum Members
Points: 29609 Visits: 39986
it depends on what your "number" is going to be;
SQL natively keeps the number of days since 01/01/1900:

select convert(decimal(12,0),getdate()) = 40167
select convert(decimal(12,4),getdate()) = 40166.5383



after the decimal point, it is the portion of one day(if 12:00 noon is 0,5, you can see i posted a bit later than that.

if your "number" is the number of seconds since a specific date, you'd have to tell us the starting date. how is your number going to represent time, if it is not a datetime column?

why not store the date no matter what? it's much more accurate and saves the whole convert from datetime to-number-back-todatetime problem.
does that help?

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!

rahulsony111
rahulsony111
Old Hand
Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)

Group: General Forum Members
Points: 303 Visits: 265
Do you know which datatype returns only date like 2009-12-21
I want to convert that date to 20091221
wayne.mcdaniel
wayne.mcdaniel
SSC Veteran
SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)

Group: General Forum Members
Points: 245 Visits: 592
SQL Server is going to return GetDate as a datetime - which includes the date and the time.

Since you are wanting this in a very specific format, you might want to use the datepart functions to pull out the pieces of the date and put them back together. This will do that in the format you want - including zero padding the month and day if they are one digit.

select cast(
cast(datepart(year,getdate()) as varchar) + -- get the year
right('0' + cast (datepart(month,getdate()) as varchar),2) + --get the month and zero pad
right('0' + cast (datepart(day,getdate()) as varchar),2) --get the day and zero pad
as numeric(10,0)) -- convert back to a numeric(10,0)


Lowell
Lowell
One Orange Chip
One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)

Group: General Forum Members
Points: 29609 Visits: 39986
that is exactly why you'd want to not store a date as a number: 20091221 you can kind of read as a date, but what if the value is 20091250 20099999??

you are using a number and assuming that certain pairs of digits should not exceed #allowed days/#allowed months. it is bad practice. always store dates as dates.

at some point you'll be back saying how do i convert 20091221 into a datetime.

anyway, yo can get the value you want as part of a convert statement:

SELECT CONVERT(VARCHAR(35),@date,112)
--results = '20091221'



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!

GSquared
GSquared
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24865 Visits: 9730
Have you tried converting to varchar with style 112?

select convert(varchar(100), getdate(), 112);



You could wrap that in a further convert/cast to numeric, if you want to.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
SQLRNNR
SQLRNNR
SSC-Dedicated
SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)

Group: General Forum Members
Points: 33940 Visits: 18560
Or look at the other thread on that rahulsony111 started
http://www.sqlservercentral.com/Forums/Topic837454-148-1.aspx#bm837479



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)

Group: General Forum Members
Points: 91392 Visits: 41151
I believe it's already been said on one of these threads but there are a thousand reasons why storing an ISO date as a numeric is absolutely the wrong thing to do. I'd try to compel the designers of such a travesty to change their mind.

--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