March 10, 2011 at 4:06 pm
I have a column with datatype as varchar.
I am writing a select statement and need to show the data in the below format.
MM/DD/YYYY
2011-03-09 15:16:41 should be 03-09-2011 15:16:41
How do I do this.I have been able to get the date part from the varchar filed using this statement.Default date should be 1/1/1899 if no data(NULL) is found.
The statement:
select............
........................
logindate=ISNULL(CONVERT(varchar,cast(Tablename.Logindate as datetime),101),'1/1/1899')
from
..........
SAMPLE DATA:
1899/1/1
1899/1/1
1899/1/1
2011-03-09 15:16:41
2011-02-23 15:07:03
1899/1/1
1899/1/1
1899/1/1
2010-07-26 10:54:25
2009-08-14 10:44:31
2011-02-24 12:55:24
2009-05-04 14:46:29
1899/1/1
2009-07-08 08:57:47
2010-06-08 13:47:13
1899/1/1
1899/1/1
2010-07-08 08:04:09
1899/1/1
2010-09-10 11:20:01
2011-03-10 08:18:14
2011-03-09 08:30:29
2009-09-14 14:12:53
2011-03-09 16:59:00
2011-02-24 13:09:08
1899/1/1
2010-06-18 11:47:04
1899/1/1
2009-08-12 10:22:57
1899/1/1
1899/1/1
1899/1/1
1899/1/1
2011-01-05 11:11:40
2010-12-13 09:10:21
1899/1/1
1899/1/1
2011-02-18 08:55:33
1899/1/1
1899/1/1
2011-03-07 09:30:18
1899/1/1
1899/1/1
2010-05-17 15:31:17
2010-12-08 10:43:48
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
2011-01-11 10:43:43
2011-02-10 13:36:23
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
2009-08-21 10:16:19
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
1899/1/1
2011-03-07 12:16:36
1899/1/1
1899/1/1
2011-03-08 08:48:02
1899/1/1
1899/1/1
2009-08-13 11:12:04
1899/1/1
2011-02-07 11:35:44
1899/1/1
1899/1/1
March 10, 2011 at 4:35 pm
Something like this?
SELECT CONVERT(CHAR(10),Tablename.Logindate,110) + ' ' + CONVERT(CHAR(8),Tablename.Logindate,108)
March 10, 2011 at 4:38 pm
March 10, 2011 at 4:39 pm
Try this:
SELECT ...,
REPLACE(CONVERT(CHAR(10), CONVERT(DATETIME, [column_name]), 101), '/', '-') + ' ' + CONVERT(CHAR(12), CONVERT(DATETIME, [column_name]), 114) AS my_new_date,
...
FROM [some_table];
Edit: missed the replace, thanks bitbucket
There are no special teachers of virtue, because virtue is taught by the whole community.
--Plato
March 15, 2011 at 11:40 am
DOes not work....
March 15, 2011 at 11:43 am
Does not work.....
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting datetime from character string.
March 15, 2011 at 11:51 am
Means you may have a non-date value.
Try this: Select * from My Table where IsDate(MyColumn) = 0
(may not get you everything/anything but it's a good place to start)
March 15, 2011 at 3:11 pm
Here's a thread about identifying bad date values. Maybe it will help
http://www.sqlservercentral.com/Forums/Topic1056892-146-1.aspx
March 15, 2011 at 8:44 pm
sqlserver12345 (3/10/2011)
I have a column with datatype as varchar.I am writing a select statement and need to show the data in the below format.
MM/DD/YYYY
Having dates and times stored in a VARCHAR column is probably one of the biggest design mistakes there is in the world of SQL. If you can, you need to change it to a DATETIME datatype for more reasons that I care to list in a single post. It's just not a good idea to store dates or times and anything other than a proper date or time datatype.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 16, 2011 at 11:08 am
sqlserver12345 (3/15/2011)
Does not work.....Msg 241, Level 16, State 1, Line 1
Conversion failed when converting datetime from character string.
Date conversion is also dictated by the language setting of the login as to whether sql server treats the text date as YMD or YDM
Try adding SET DATEFORMAT YMD before your query
Note: This does not apply to YYYYMMDD formated dates.
Far away is close at hand in the images of elsewhere.
Anon.
March 16, 2011 at 2:00 pm
Jeff Moden (3/15/2011)
Having dates and times stored in a VARCHAR column is probably one of the biggest design mistakes there is in the world of SQL. If you can, you need to change it to a DATETIME datatype for more reasons that I care to list in a single post. It's just not a good idea to store dates or times and anything other than a proper date or time datatype.
This sounds like a great article. I can think of about a zillion reasons myself but I bet you can name about a zillion more that I have never even thought of.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply