Solved

convert stirng to date

Posted on 2009-07-13
3
252 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 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

20 Experts available now in Live!

Get 1:1 Help Now