Solved

How do I cast date format in MS SQL Server like 2010-xx-xx and as char(4) char(2) char(2) using also concatenating?

Posted on 2010-08-27
7
622 Views
Last Modified: 2012-05-10
How do I cast date format in MS SQL Server like 2010-xx-xx and as char(4) char(2) char(2) using also concatenating?

Now, what I get is 2010-5 -16 and 2010-5 -4

Also, after executing command I get order by like this: 2010-5 -16 before 2010-5 -4 (because the order by order digit by digit). So I need to have also zero displayed, like 2010-05-16, 2010-05-04 and not like date format but like varchar format using both cast and concatenating.
0
Comment
Question by:hermesalpha
[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
  • 4
  • 3
7 Comments
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33547797
select convert(char(10), dtcol, 120)
0
 

Author Comment

by:hermesalpha
ID: 33547831
I received this error message when trying to execute the query:

Msg 207, Level 16, State 1, Line 8
Invalid column name 'DTCOL'.
Msg 104, Level 16, State 1, Line 18
ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator.
0
 

Author Comment

by:hermesalpha
ID: 33547836
I have a Union in the first Select part with three columns that are concatenated into one
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33547837
dtcol represents your column that has the datetime.  If it is received_date then you would use

select invoice_id, invoice_number, customer_name, convert(char(10), received_date, 120)
from invoice

or if you want to work on the current datetime, then

select * from payment
where paid=0 and due_date <= convert(char(10), getdate(), 120)
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33547846
So the columns are int for year,month,day?
The following works whether y/m/d are int or char, as long as the they are numeric.

select convert(char(10),convert(datetime,convert(char(8),y*10000 + m*100 + d)),120)
from tbl
0
 

Author Comment

by:hermesalpha
ID: 33547853
Yes, it worked now, thank you! One thing I don't understand: the number 120?

Is it something that represents the format of the date?
0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 33547864
Yes, it is a date format.

http://msdn.microsoft.com/en-us/library/ms187928.aspx

There isn't a format that is YYYY-MM-DD exactly, but by using 120 (yyyy-mm-dd hh:mi:ss(24h)) and forcing it into Char(10), the result is yyyy-mm-dd only.

Regards
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

691 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