Solved

convert stirng to date

Posted on 2009-07-13
3
299 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
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 27

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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

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…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

726 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