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

Convert GETDATE() to YYYYMMDD Expand / Collapse
Author
Message
Posted Monday, December 21, 2009 12:23 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Sunday, September 26, 2010 8:29 AM
Points: 113, Visits: 265
In derived columns i have to convert getdate() to YYYYDDMM.
I dont need time.
I want it in format like 20091221

Please help me
Post #837454
Posted Monday, December 21, 2009 12:29 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 4:56 AM
Points: 20,815, Visits: 32,748
Curious, how many different ways do you plan on asking the same question?

You have asked the same question here and here just worded it a little different.

May I suggest that you pick one thread and concentrate on that one so that you don't get the same answers and have to answer the same questions multiple times?




Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #837459
Posted Monday, December 21, 2009 12:29 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 4:56 AM
Points: 20,815, Visits: 32,748
By the way, I'd store a date as a date, not a numeric value.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #837460
Posted Monday, December 21, 2009 12:33 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 9:52 AM
Points: 17,980, Visits: 15,983
I'm curious what the need is to convert a date to a Number. I would leave the date as one of the datetime datatype in sql 2005.


Now to get the Date Format you seek, you will need to convert to varchar and check the following: http://www.sqlusa.com/bestpractices2005/centurydateformat/




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


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Post #837468
Posted Monday, December 21, 2009 12:35 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 4:56 AM
Points: 20,815, Visits: 32,748
CirquedeSQLeil (12/21/2009)
I'm curious what the need is to convert a date to a Number. I would leave the date as one of the datetime datatype in sql 2005.


Now to get the Date Format you seek, you will need to convert to varchar and check the following: http://www.sqlusa.com/bestpractices2005/centurydateformat/


Actually, on the SQL side dropping the time portion off getdate is this simple:

dateadd(dd, datediff(dd, 0, getdate()), 0)




Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #837469
Posted Monday, December 21, 2009 12:38 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 9:52 AM
Points: 17,980, Visits: 15,983
Lynn Pettis (12/21/2009)
CirquedeSQLeil (12/21/2009)
I'm curious what the need is to convert a date to a Number. I would leave the date as one of the datetime datatype in sql 2005.


Now to get the Date Format you seek, you will need to convert to varchar and check the following: http://www.sqlusa.com/bestpractices2005/centurydateformat/


Actually, on the SQL side dropping the time portion off getdate is this simple:

dateadd(dd, datediff(dd, 0, getdate()), 0)



Noted and added to my toolbox - I had never used that method. Thanks Lynn.




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


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Post #837476
Posted Monday, December 21, 2009 12:40 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 4:56 AM
Points: 20,815, Visits: 32,748
CirquedeSQLeil (12/21/2009)
Lynn Pettis (12/21/2009)
CirquedeSQLeil (12/21/2009)
I'm curious what the need is to convert a date to a Number. I would leave the date as one of the datetime datatype in sql 2005.


Now to get the Date Format you seek, you will need to convert to varchar and check the following: http://www.sqlusa.com/bestpractices2005/centurydateformat/


Actually, on the SQL side dropping the time portion off getdate is this simple:

dateadd(dd, datediff(dd, 0, getdate()), 0)



Noted and added to my toolbox - I had never used that method. Thanks Lynn.


Check my blog for some other date routines. They might come in handy as well.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #837479
Posted Monday, December 21, 2009 12:41 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 9:52 AM
Points: 17,980, Visits: 15,983
Thanks - will do.



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


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Post #837480
Posted Monday, December 21, 2009 12:46 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Sunday, September 26, 2010 8:29 AM
Points: 113, Visits: 265
I am trying this from 5 hrs and unable to get a solution.

In target table i have that column as numeric.
I tried getdate() with datatype databasedate but it is returning 2009-12-21. So that value is not going into target table sand saying it as invalid number.

I want it ti look like 20091221, only then it goes into targat table
Post #837486
Posted Monday, December 21, 2009 12:51 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 4:56 AM
Points: 20,815, Visits: 32,748
rahulsony111 (12/21/2009)
I am trying this from 5 hrs and unable to get a solution.

In target table i have that column as numeric.
I tried getdate() with datatype databasedate but it is returning 2009-12-21. So that value is not going into target table sand saying it as invalid number.

I want it ti look like 20091221, only then it goes into targat table


As mentioned in one of the other threads where you asked the "same" question, and since you seem fixed on storing the date as an integer, in SQL:

cast(convert(varchar(8), getdate(), 112) as int)




Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #837490
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse