473,386 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Convert String to DateTime

40
Hai,

Im new to sql server...
I want to convert a field in a table to DateTime.

The column(Column Name-DDateTime , Data type String) has data in this format 01-04-2008 15:12:52
i want to get records in a particular datetime range.Do not no how to proced.

usually i can write a query like this
select DDateTime from table_name where DDateTime between '01/27/2008 01:23:32' and '01/29/2008 01:23:32'

as the column's datatype is string im facing problem..
Apr 3 '08 #1
7 12679
ck9663
2,878 Expert 2GB
use CAST or CONVERT . You have to make sure the arguments that you're using for comparison have the same data type or can be explicitly converted.

-- CK
Apr 3 '08 #2
crs27
40
use CAST or CONVERT . You have to make sure the arguments that you're using for comparison have the same data type or can be explicitly converted.

-- CK
but i did nt no how to use the convert function in the where clause..

The string formate is 'day-month-year hr:min:sec' in the table can u please help me to write the query..

Thanks for the quick reply
Apr 3 '08 #3
ck9663
2,878 Expert 2GB
Here's SELECT and the WHERE. Here's the BETWEEN and here are some samples.

-- CK
Apr 3 '08 #4
crs27
40
Here's SELECT and the WHERE. Here's the BETWEEN and here are some samples.

-- CK
thanks for the help..
Shal get back if still got problem
Apr 3 '08 #5
crs27
40
thanks for the help..
Shal get back if still got problem
it did nt help me much..
as the example their the field was of datetime.But my sinario is different i was to get records between a datetime range and tht particular field is of String datatype.

i wrote a query something like this

SELECT * from vts3 where convert(datetime,dubaidatetime)between '31-03-2008 01:00:00' and '02-04-2008 01:00:00'

its giving me an exception
Error code 242, SQL state S0003: The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

plz help
Apr 3 '08 #6
ck9663
2,878 Expert 2GB
use CAST on your field and the dates on the BETWEEN operator. Check your syntax.

-- CK
Apr 3 '08 #7
gpl
152 100+
Try this

Expand|Select|Wrap|Line Numbers
  1. SELECT * 
  2. from vts3 
  3. where convert(datetime,dubaidatetime, 105) between 
  4. convert(datetime,'31-03-2008 01:00:00', 105) and 
  5. convert(datetime,'02-04-2008 01:00:00', 105)
  6.  
This puts your string dates into datetime format, so that they can be properly compared .... the 105 means the date is in dd-mm-yyyy format, check out Convert in BOL
Graham
Apr 3 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: goochey | last post by:
I'm trying to convert a Julian Date (Format "4365") into an actual calendar date in Visual Basic, can anyone help me out with this.
5
by: simon | last post by:
I have datetime variable: Datetime tsEndTime; Should I use (DateTime): tsEndTime=(DateTime)rdr.GetValue(15) or is better to use: tsEndTime=Convert.ToDateTime(rdr.GetValue(15))
19
by: simon | last post by:
I get from the dateTimePicker the value: string="12/18/2003 11:52:28 AM" Now I need to convert this to dateTime. Any function I use: Convert.ToDateTime(string) or Cdate(string), I get an error...
2
by: ziggislaw | last post by:
hello how can I convert DateTime from "25.12.2005" to "2005-12-25 00:00:00.000" ? Now I have DateTime as string (I get it from <asp:label>). Thanks
14
by: Me | last post by:
Hi all I am getting a really bizzare error on when I convert a string into a datetime: The code is : DateTime dt1 = Convert.ToDateTime("10 Sep 2005"); Console.WriteLine(dt1.Year);
4
by: tshad | last post by:
Is there any difference between convert.ToDateTime and System.DateTime.Parse? I am using them both and they seem the same. Is one better to use than another? Thanks, Tom
5
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public...
3
by: Oren | last post by:
Hi, I use DateTime.Now and Serialize an object into XML field (Sql 2005). the problem is that: select CONVERT(datetime, '2006-10-09T13:54:06.9248089+02:00') ERROR: Conversion failed when...
7
by: groups | last post by:
This is my first foray into writing a generic method and maybe I've bitten off more than I can chew. My intent is to have a generic method that accepts a value name and that value will be...
9
by: mcfly1204 | last post by:
I have read through various posts on converting a string into the datetime format, but cannot resolve my situation. I have a string in the format of 'yyyymmddthhmmss.sssz' that I would like to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.