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
611 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now