473,385 Members | 1,772 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,385 software developers and data experts.

How to use date function in sql server

sk
Hi

I am trying to do a simple select using a date value.

For eg:-
in oracle i would do the following
select count(*) from TEMP_TABLE where to_char(modif_time,'mm/dd/yyyy')='10/04/03'

How do I accomplish the same in sqlserver?

Thanks in Advance
sk
Jul 20 '05 #1
4 61483
You can use the CONVERT function to transform a DATETIME column to a string
in a particular date format. In a WHERE clause though it makes more sense
not to convert the dates - otherwise the conversion will force a table scan
and the conversion will have to be performed for every row.

Instead, specify the range of DATETIME values you require:

SELECT COUNT(*)
FROM temp_table
WHERE modif_time>='20031004' AND modif_time<'20031005'

You can use any of the following styles of formatted string to specify dates
in code:

'20031231'
'2003-12-31T17:59:00'
'2003-12-31T17:59:00.000'

These are the "safe" ISO formats which are guaranteed to work independently
of any regional settings. Other formats such as mm/dd/yyyy are best avoided
because they are dependent on the server's regional settings and are
therefore less portable.

--
David Portas
------------
Please reply only to the newsgroup
--
Jul 20 '05 #2
sai
David,

Tried your suggestion, works great. Thanks a lot.
I also tried using convert function to get counts for
multiple dates and works fine.
But I was trying to sort the rows using date, that
doesn't seem to work.

my query looks like this:-
select convert(varchar,modif_time,101),count(*) from TEMP_TABLE group by
convert(varchar,modif_time,101) order by convert(varchar,modif_time,101)

when I run it, the output looks like :-
01/01/2001
01/01/2002
01/01/2003
01/02/2001
01/02/2002
01/02/2003
01/03/2000
01/03/2001
01/03/2002
01/03/2003

As you can see, the sort order seems to be first 2 chars, then the next
2 chars and so on. I want it to be
01/03/2000
01/01/2001
01/02/2001
01/03/2001
01/01/2002
01/02/2002
01/03/2002
01/01/2003
01/02/2003
01/03/2003

this is the right date format. Is this possible? Any help
will be appreciated.

Thanks
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #3
sai (an*******@devdex.com) writes:
Tried your suggestion, works great. Thanks a lot.
I also tried using convert function to get counts for
multiple dates and works fine.
But I was trying to sort the rows using date, that
doesn't seem to work.

my query looks like this:-
select convert(varchar,modif_time,101),count(*) from TEMP_TABLE group by
convert(varchar,modif_time,101) order by convert(varchar,modif_time,101)

when I run it, the output looks like :-
01/01/2001
01/01/2002
01/01/2003
01/02/2001
01/02/2002
01/02/2003
01/03/2000
01/03/2001
01/03/2002
01/03/2003

As you can see, the sort order seems to be first 2 chars, then the next
2 chars and so on. I want it to be


Of course. You asked to sort on a character string, then SQL Server
will sort on a character string.

If you want to sort by date, there are two options:

1) Use a better date format, and still sort by string. Change 101 to
112 that is YYYYMMDD.

2) Use this somewhat convuluted query:

select convert(varchar, convert(datetime, dt), 101), cnt
from (select dt = convert(varchar, loadtime, 112), cnt = count(*)
from abasysobjects
group by convert(varchar, loadtime, 112)) as x
order by dt

(Column and table names changed to the table I used for the test.)
--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #4
Because your CONVERT function returns a string you need to make sure the
string is in the correct format for sorting. (YYYY-MM-DD):

SELECT CONVERT(CHAR(10),modif_time,120) AS modif_date,
COUNT(*)
FROM TEMP_TABLE
GROUP BY CONVERT(CHAR(10),modif_time,120)
ORDER BY modif_date

You might prefer to output a date rather than a string - leave the
formatting of the date to your client application:

SELECT CAST(CONVERT(CHAR(8),modif_time,112) AS DATETIME) AS modif_date,
COUNT(*)
FROM TEMP_TABLE
GROUP BY CAST(CONVERT(CHAR(8),modif_time,112) AS DATETIME)
ORDER BY modif_date

--
David Portas
------------
Please reply only to the newsgroup
--
Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Funnyweb | last post by:
I have just notices that the date() function is not returning the correct date/time on my "server". I am running apache2 on my winxp pro laptop. My system clock is set to the correct date,...
3
by: Sharad Gupta | last post by:
Friends I need a date simple date function that would append the coming month value in my current month like monthname(month(date)) : gives me current month in string i need next month , a...
2
by: ltamisin | last post by:
Hi Im working on a Year(Date) function right now, the output of that function is this "2005", my question is how can i change the format into this "05" Function: Year(Date) Output: 2005...
8
by: Dennis M. Marks | last post by:
What is the maximum valid date range for the date(yyyy,mm,dd) function. -- Dennis M. Marks http://www.dcs-chico.com/~denmarks/ Replace domain.invalid with dcsi.net -----= Posted via...
5
by: Dennis M. Marks | last post by:
After reading section 15.9.1.1 the ECMAScript Language Specifications I see that the date range for the Date function is +/- 100,000,000 days from 01 Jan 1970. This is called an extrapolated...
6
by: bryan.seaton | last post by:
I have a delete statement that is not doing what I want it to do: Delete from LOG_TABLE where (DATE(LOG_TS)) < (DATE(CURRENT_DATE)- 21 DAYS); It is supposed to delete all records that are 21...
4
by: Christine | last post by:
I am having the strangest problem. I use the Date function in several of my forms and modules in an Access 2000 mdb. Lately, wherever in my code (in this one mdb) I use the Date function, it...
4
by: Patrick McGuire | last post by:
I need to get the current date in vb .NET. In vba I always used the Date() function, and the help in .NET indicates that this function still exists, but I can't seem to find it. The Now()...
1
by: shyam vashista | last post by:
i have problem in validation check for system date with server date problem::: If i change my system date as september 30, 2006 and use validation for filling form as current date as oct30,...
3
by: murch.alexander | last post by:
I made a simple public function to set and return a date value (see below). I have a number of queries that call up the function to get the "As Of Date," which is typically set to today's date....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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.