Solved

convert stirng to date

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQl server restarts itself 6 38
Stored procedure 4 33
execute a MS SQL script as a schedule SQL job 72 132
Query 14 55
When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

832 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