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
613 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
  • 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
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 information from SQL Server on Database, Connection and Server properties
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

860 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