Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Set Variable based on a SELECT


Set Variable based on a SELECT

Author
Message
rew-370421
rew-370421
SSC Veteran
SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)

Group: General Forum Members
Points: 208 Visits: 1382
I'm still Googling and searching SSC for my answer, but here is my problem.

I need to set a VARIABLE based on the result of a SELECT

SELECT CONVERT(CHAR(9),GETDATE(),1) + SUBSTRING(CONVERT(CHAR(5),GETDATE(),14),1,5) + ' '

which results in...
02/10/10 07:26


This is my code
DECLARE @mydate DATETIME
SET @myDate = CONVERT(CHAR(9),GETDATE(),1) + SUBSTRING(CONVERT(CHAR(5),GETDATE(),14),1,5) + ' '
print @myDate

which results in...
Feb 10 2010 7:28AM

What am I doing wrong?
Hunterwood
Hunterwood
Say Hey Kid
Say Hey Kid (662 reputation)Say Hey Kid (662 reputation)Say Hey Kid (662 reputation)Say Hey Kid (662 reputation)Say Hey Kid (662 reputation)Say Hey Kid (662 reputation)Say Hey Kid (662 reputation)Say Hey Kid (662 reputation)

Group: General Forum Members
Points: 662 Visits: 297
Hi,

since @mydate is of type DATETIME you get an implicit conversion of your neatly formatted string back to datetime.

Declare @mydate as a CHAR(14), or what you find suitable, and everything should work as expected...

/Markus
Steve Cullen
Steve Cullen
SSChasing Mays
SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)

Group: General Forum Members
Points: 603 Visits: 1226
DECLARE @mydate varchar(20)
SELECT @myDate = CONVERT(CHAR(9),GETDATE(),1) + SUBSTRING(CONVERT(CHAR(5),GETDATE(),14),1,5) + ' '
print @mydate

Converting oxygen into carbon dioxide, since 1955.


Steve Cullen
Steve Cullen
SSChasing Mays
SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)

Group: General Forum Members
Points: 603 Visits: 1226
Hmmmm. Must type faster.:-)

Converting oxygen into carbon dioxide, since 1955.


rew-370421
rew-370421
SSC Veteran
SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)

Group: General Forum Members
Points: 208 Visits: 1382
Thanks Markus and Steve! You guyes are great! That works!

But since @mydate is declared as varchar(20), can I still INSERT it into a TABLE's field that is defined as DATETIME? How does that work?
Steve Cullen
Steve Cullen
SSChasing Mays
SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)

Group: General Forum Members
Points: 603 Visits: 1226
You can convert it back to datetime if you need to :

INSERT INTO tableA (columnA)
SELECT CONVERT(datetime,@mydate)

Converting oxygen into carbon dioxide, since 1955.


rew-370421
rew-370421
SSC Veteran
SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)

Group: General Forum Members
Points: 208 Visits: 1382
Thanks, Steve.

I tested INSERTs with both VARCHAR & your DATETIME convertion into my DATETIME field and the results are the same. So I guess it doesn't matter.
Steve Cullen
Steve Cullen
SSChasing Mays
SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)

Group: General Forum Members
Points: 603 Visits: 1226
Well that won't work. In this case I would use two variables based off of the same value such as :

DECLARE @mydate datetime, @mydatestring varchar(20)
SELECT @mydate = getdate()
SELECT @mydatestring = CONVERT(CHAR(9),@mydate,1) + SUBSTRING(CONVERT(CHAR(5),@mydate,14),1,5) + ' '

PRINT @mydatestring

INSERT INTO TableA (columnA)
SELECT @mydate

Converting oxygen into carbon dioxide, since 1955.


Steve Cullen
Steve Cullen
SSChasing Mays
SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)

Group: General Forum Members
Points: 603 Visits: 1226
OK, it does work. Must test better :-)

Converting oxygen into carbon dioxide, since 1955.


rew-370421
rew-370421
SSC Veteran
SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)

Group: General Forum Members
Points: 208 Visits: 1382
1) Then when I do a SELECT on the DATETIME field in the TABLE, I get this:
2010-02-10 08:12:00.000

2) But when I use SSMS and do an OPEN TABLE to view the data, I get this:
2/10/2010 8:12:00 AM

When I do a SELECT on the data I want to "see" option 2 (2/10/2010 8:12:00 AM).

So I assume I have to format my SELECT in order to CONVERT the value into the FORMAT I want to see?
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search