Solved

Construct Date in SQL

Posted on 2006-11-19
4
1,010 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 500 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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSIS GUID Variable 2 36
Rebooting Witness SQL Server 2 25
Find unused columns in a table 12 74
Sql server Error message 3 16
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

733 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