Solved

convert stirng to date

Posted on 2009-07-13
3
263 Views
Last Modified: 2012-06-27
RIGHT(String, 10)

will result in this: 'DD-MM-YYYY'

how do i convert this to an integer with 'YYYYMMDD' format?
0
Comment
Question by:Amien90
3 Comments
 
LVL 21

Expert Comment

by:Tapan Pattanaik
ID: 24837990
0
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 500 total points
ID: 24838029
try this

Declare @str varchar(20)
set @str = '13-07-2007'

select  Cast ( Right(@str,4) + substring(@str,4,2)+Left(@str,2)  as int)
select  Right(@str,4) + substring(@str,4,2)+Left(@str,2)  
0
 
LVL 26

Expert Comment

by:Zberteoc
ID: 24846290
here is much simpler:

select convert(varchar, convert(datetime, '27-07-2009',105), 112)

The result even varchar can be asigned to integer columns or variables aand even directly used in arithmetic expressions. If you still need integer you can cast it:

select cast(convert(varchar, convert(datetime, '27-07-2009',105), 112) as int)


PS. I know was already answered but I didn't like the solution. :o)
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now