?
Solved

How do I store the result of a query into declaration

Posted on 2011-09-15
4
Medium Priority
?
381 Views
Last Modified: 2012-05-12
SQL SERVER (not reporting)

I need to store the results of a query, into a declaration, not actually run it.

For example:
Declare
@mydate datetime

Select
@mydate = select top1  somdatefield from sometable

How do I store the result of the query into @mydate, in date format?

What I have found so far only executes the query in varchar format and doesn't store it.  

Thanks.



0
Comment
Question by:Ursino
[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
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
25112 earned 2000 total points
ID: 36546807
2 examples for you

Declare @mydate datetime =  (select top 1  somdatefield from sometable)
Select @mydate

Declare @mydate datetime =  GETDATE()
select @mydate

(these work in 2008). In 2005 it will be

Declare @mydate datetime
Set @mydate =  (select top 1  somdatefield from sometable)
Select @mydate
0
 
LVL 5

Expert Comment

by:25112
ID: 36546815
as long as somdatefield  is indeed a date field, @mydate will also be datefield only.

try this:

Declare @mydate datetime =  (select top 1  somdatefield from sometable)
Select cast(@mydate as datetime)
select @mydate
0
 
LVL 25

Expert Comment

by:jogos
ID: 36547554
For converting a non-datefield to datetime or other way around : cast or convert
http://msdn.microsoft.com/en-us/library/ms187928.aspx
But the less convertion the better.
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 36547936
Declare @mydate datetime

Select @mydate = (select top1  somdatefield from sometable)

the brackets around the subQuery are required

or

Declare @mydate datetime
 select top1  @mydate =somdatefield from sometable


but in any case the Query has to RUN to obtain the value
and for the top to have any meaning (an obtain consistent results) you must specify an order by clause .
unless you wish to select a random value?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

752 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