September 9, 2010 at 9:38 am
Hi,
this sounds simple, but I can't seem to get it to work.!
I have a store procedure that basically generates a date, that needs to be put into a database table field, that has been define as: nvarchar(255)
I want the date to be saved as 'yyyy-mm-dd hh:mm:ss'
My stored procedure generates the converting the date time format
convert(datetime, @LogicalDate, 102)
this display as a query result of: 2010-09-09 00:00:00.000
yet, when it updates the table, it saves it as Sep 9 2010 12:00AM
Anyone got any idea how I get this saved in the format I want?
_____________________________________________________________________________MCITP: Business Intelligence Developer (2005)
September 9, 2010 at 9:45 am
Is @logicaldate defined as a datetime? If so, shouldn't the convert be:
convert(nvarchar(256), @LogicalDate, 102)
September 9, 2010 at 9:46 am
dave-dj (9/9/2010)
Hi,this sounds simple, but I can't seem to get it to work.!
I have a store procedure that basically generates a date, that needs to be put into a database table field, that has been define as: nvarchar(255)
I want the date to be saved as 'yyyy-mm-dd hh:mm:ss'
My stored procedure generates the converting the date time format
convert(datetime, @LogicalDate, 102)
this display as a query result of: 2010-09-09 00:00:00.000
yet, when it updates the table, it saves it as Sep 9 2010 12:00AM
Anyone got any idea how I get this saved in the format I want?
Why are you converting your date to a datetime if you want to store it as a nvarchar? Convert it to nvarchar instead.
Try CONVERT(NVARCHAR(255), @LogicalDate, 120)
Edit: oops, looks like someone was faster than at posting an answer.
For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]
September 9, 2010 at 9:47 am
Assuming @logicaldate is datetime, it should be
SELECT CONVERT(NVARCHAR(255), @logicaldate, 102)
Lol, think I was a little slow :hehe:
September 9, 2010 at 9:50 am
3 of us posted an answer, but I think I'm the only one with the right format. (120 vs 102)
For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]
September 9, 2010 at 9:51 am
-- the stored procedure returns a DATETIME data type:
DECLARE @MyDate nvarchar(255)
SET @MyDate = GETDATE()
SELECT @MyDate
-- 'Sep 9 2010 4:46PM'
-- You need to CONVERT the output to a character type, CHAR(19) is perfect.
SELECT CONVERT(CHAR(19),GETDATE(),120)
-- '2010-09-09 16:45:02'
Heh yep Alvin, it's 120 π
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
September 9, 2010 at 9:53 am
Alvin Ramard (9/9/2010)
3 of us posted an answer, but I think I'm the only one with the right format. (120 vs 102)
True. I didn't really look at the format required, just spotted the error and used the same as the OP used.
So basically, I was slowest and didn't even read the whole question. . . having a good day π
September 9, 2010 at 9:56 am
skcadavre (9/9/2010)
Alvin Ramard (9/9/2010)
3 of us posted an answer, but I think I'm the only one with the right format. (120 vs 102)True. I didn't really look at the format required, just spotted the error and used the same as the OP used.
So basically, I was slowest and didn't even read the whole question. . . having a good day π
At least you tried. That's what matter.
Have a good day too.
For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]
September 9, 2010 at 10:04 am
wow. Thanks for all the quick responses!. That's perfect thank you.
As always the ladies and gents of sqlserver central have been a great help.!
Everyday is a school day...............:-D
_____________________________________________________________________________MCITP: Business Intelligence Developer (2005)
September 9, 2010 at 8:25 pm
dave-dj (9/9/2010)
wow. Thanks for all the quick responses!. That's perfect thank you.As always the ladies and gents of sqlserver central have been a great help.!
Everyday is a school day...............:-D
Let the schooling continue, then. π Storing dates and times as varchar or nvarchar values is one of the worst things you can do in SQL Server or any database for that matter. So, let me ask, why do you need to convert a date to an NVARCHAR?
--Jeff Moden
Change is inevitable... Change for the better is not.
September 9, 2010 at 8:40 pm
Jeff Moden (9/9/2010)
dave-dj (9/9/2010)
wow. Thanks for all the quick responses!. That's perfect thank you.As always the ladies and gents of sqlserver central have been a great help.!
Everyday is a school day...............:-D
Let the schooling continue, then. π Storing dates and times as varchar or nvarchar values is one of the worst things you can do in SQL Server or any database for that matter. So, let me ask, why do you need to convert a date to an NVARCHAR?
Jeff, it's not just NVARCHAR. It's NVARCHAR(255). :w00t:
Based on the way the question was worded, I assumed the OP "inherited" the datatype.
After rereading the original question, I have another question. Why are we answering a question like this for someone with "MCITP: Business Intelligence Developer (2005)" in his signature?
For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]
September 10, 2010 at 2:17 am
Alvin Ramard (9/9/2010)
Jeff Moden (9/9/2010)
dave-dj (9/9/2010)
wow. Thanks for all the quick responses!. That's perfect thank you.As always the ladies and gents of sqlserver central have been a great help.!
Everyday is a school day...............:-D
Let the schooling continue, then. π Storing dates and times as varchar or nvarchar values is one of the worst things you can do in SQL Server or any database for that matter. So, let me ask, why do you need to convert a date to an NVARCHAR?
Jeff, it's not just NVARCHAR. It's NVARCHAR(255). :w00t:
Based on the way the question was worded, I assumed the OP "inherited" the datatype.
After rereading the original question, I have another question. Why are we answering a question like this for someone with "MCITP: Business Intelligence Developer (2005)" in his signature?
CHAR(19) is perfect
I always hope that little hints like this will be picked up, and even if they're not used on the current query then maybe, just maybe, they'll be considered for the next one.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
September 10, 2010 at 4:24 am
Alvin Ramard (9/9/2010)
Jeff Moden (9/9/2010)
dave-dj (9/9/2010)
wow. Thanks for all the quick responses!. That's perfect thank you.As always the ladies and gents of sqlserver central have been a great help.!
Everyday is a school day...............:-D
Let the schooling continue, then. π Storing dates and times as varchar or nvarchar values is one of the worst things you can do in SQL Server or any database for that matter. So, let me ask, why do you need to convert a date to an NVARCHAR?
Jeff, it's not just NVARCHAR. It's NVARCHAR(255). :w00t:
Based on the way the question was worded, I assumed the OP "inherited" the datatype.
Maybe but I'd love to hear from the OP. π
--Jeff Moden
Change is inevitable... Change for the better is not.
September 10, 2010 at 4:25 am
Chris Morris-439714 (9/10/2010)
Alvin Ramard (9/9/2010)
Jeff Moden (9/9/2010)
dave-dj (9/9/2010)
wow. Thanks for all the quick responses!. That's perfect thank you.As always the ladies and gents of sqlserver central have been a great help.!
Everyday is a school day...............:-D
Let the schooling continue, then. π Storing dates and times as varchar or nvarchar values is one of the worst things you can do in SQL Server or any database for that matter. So, let me ask, why do you need to convert a date to an NVARCHAR?
Jeff, it's not just NVARCHAR. It's NVARCHAR(255). :w00t:
Based on the way the question was worded, I assumed the OP "inherited" the datatype.
After rereading the original question, I have another question. Why are we answering a question like this for someone with "MCITP: Business Intelligence Developer (2005)" in his signature?
CHAR(19) is perfect
I always hope that little hints like this will be picked up, and even if they're not used on the current query then maybe, just maybe, they'll be considered for the next one.
Ah, but we still don't know why the OP wants to make what's usually considered to be a mistake. π
--Jeff Moden
Change is inevitable... Change for the better is not.
September 10, 2010 at 4:27 am
Ok everyone... please let the OP answer for himself...
Dave,
It's normally a serious mistake when someone wants to convert a DATETIME to any type of character based output... why do you really need to do this? There may be high performance alternative if we knew.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 15 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic. Login to reply