?
Solved

Construct Date in SQL

Posted on 2006-11-19
4
Medium Priority
?
1,030 Views
Last Modified: 2010-07-27
Hello all,

I have a Stored Proc that take a date as a parameter.  I would like to create a variable that contains the same date as the passed value but I want to change the time to 11:59pm.

For example if the proc gets the date '10/25/2006 8:00 am', I would like my varaible to say "10/25/2006 11:59 pm'.
If the proc gets a date without time I still need it to work, so '10/26/2006' would be '10/26/2006 11:59 pm'

Thanks
Jym
0
Comment
Question by:jymmealey
[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 Comments
 
LVL 14

Expert Comment

by:mherchl
ID: 17977588
declare @datenew datetime
set @datenew = convert(datetime, convert(varchar,@olddate, 112) + ' 23:59')
0
 
LVL 6

Accepted Solution

by:
Gokulm earned 2000 total points
ID: 17977603
You can construct the date as follows and return

SELECT  CONVERT(char(10), GETDATE(), 101) + ' 11:59 PM'

This will retun a character

SELECT CAST( CONVERT(char(10), GETDATE(), 101) + ' 11:59 PM' AS datetime)

This will return a datetime
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17977607
>For example if the proc gets the date '10/25/2006 8:00 am', I would like my varaible to say "10/25/2006 11:59 pm'.
I guess because you want to get all before 10/26/2006...

where yourfield < dateadd(day, 1, convert(datetime, convert (varchar(10), @parameter, 101), 101))
0
 
LVL 2

Author Comment

by:jymmealey
ID: 17977630
mherchl,

I ran the following and got an error:

SELECT CONVERT(datetime, CONVERT(varchar,'10/26/2006 8:00 am', 112) + ' 23:59')

Error was
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

I assume this is because of the type 112, it is expecting ISO?  

Ran Gokulm's query and it worked like expected.

Thanks
0

Featured Post

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

764 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