SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Problem with Dates !!!!!!!!!!!!


Problem with Dates !!!!!!!!!!!!

Author
Message
Shadab Shah
Shadab Shah
SSC Eights!
SSC Eights! (947 reputation)SSC Eights! (947 reputation)SSC Eights! (947 reputation)SSC Eights! (947 reputation)SSC Eights! (947 reputation)SSC Eights! (947 reputation)SSC Eights! (947 reputation)SSC Eights! (947 reputation)

Group: General Forum Members
Points: 947 Visits: 798
Consider the below query


CREATE TABLE #TEMP (Id int,VALUE NVARCHAR(50))
INSERT INTO #TEMP VALUES(1,'')
INSERT INTO #TEMP VALUES(2,'2013/08')

DECLARE @FirstDate date,
@EndDate date,

SELECT @FirstDate=VALUE FROM #TEMP WHERE ID=1
SELECT @EndDate=VALUE FROM #TEMP WHERE ID=2


i want my FirstDate and @EndDate variable to store the values as both that of ' ' (blank) and a date.

I tried by kepping the datatype as Varchar but it did not work, i tried with convert but it too did not work.

So is it possible to make my variable such that it stores both ' '(blank) and a date.

Thanks
Shadab Shah
Shadab Shah
SSC Eights!
SSC Eights! (947 reputation)SSC Eights! (947 reputation)SSC Eights! (947 reputation)SSC Eights! (947 reputation)SSC Eights! (947 reputation)SSC Eights! (947 reputation)SSC Eights! (947 reputation)SSC Eights! (947 reputation)

Group: General Forum Members
Points: 947 Visits: 798
The closes i can come to this problem is


SELECT LEFT(CONVERT(DATE,VALUE,111),7) FROM #TEMP WHERE ID=2



still it is not working. Any one who can help !!!!!!!!
LinksUp
LinksUp
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1762 Visits: 4640
Shadab Shah (12/25/2013)
The closes i can come to this problem is


SELECT LEFT(CONVERT(DATE,VALUE,111),7) FROM #TEMP WHERE ID=2




I'm not entirely sure what you are trying to accomplish, but I think this is what you are looking for.


SELECT
case when VALUE = '' then '' else
LEFT(CONVERT(DATE,VALUE + '/01',111),7)
end
FROM #temp



__________________________________________________________________________________________________________
How to Post to get the most: http://www.sqlservercentral.com/articles/Best+Practices/61537/
Shadab Shah
Shadab Shah
SSC Eights!
SSC Eights! (947 reputation)SSC Eights! (947 reputation)SSC Eights! (947 reputation)SSC Eights! (947 reputation)SSC Eights! (947 reputation)SSC Eights! (947 reputation)SSC Eights! (947 reputation)SSC Eights! (947 reputation)

Group: General Forum Members
Points: 947 Visits: 798
I am trying to store the value return by the select in variable. I took that variable as varchar as well as date but nohing seems to work.....
pietlinden
pietlinden
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14343 Visits: 14158
If you wanted to store NULL and a DATE value, then it would be trivial. What's the point storing ''? Is that not the same as Null (unknown)? If not, what does BLANK mean?
LutzM
LutzM
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24253 Visits: 13559
I strongly vote against a NVARCHAR data type to store date values (especially, with a length of 50 characters being allowed).
What's the reason for using NVARCHAR instead of DATETIME?



Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
SQLBlimp
SQLBlimp
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1691 Visits: 510
Hi.

1. The posters who said that a blank date is a bad idea are correct. If you are using the script as a demo to show what you are encountering in the application's data, then convert blanks to NULL with a CASE. Much easier to work with NULL.

2. I'm not sure that a DATE type of field will handle converting partial dates. I think that if the date is not valid, such as a year-month that we are implying from your example, then you must choose then to make the date a NULL (invalid), or if it is a "circa" date, you must complete it with a day.

On the other hand, if it is a JULIAN date, you need to do this (copied from http://www.ehow.com/how_12073756_convert-julian-date-sql.html):


CREATE FUNCTION convert_julian_to_datetime(@julian_date char(5)) RETURNS datetime AS
BEGIN
RETURN (SELECT DATEADD(day, CONVERT(int,RIGHT(@julian_date,3)) - 1, CONVERT(datetime, LEFT(@julian_date,2) + '0101', 112)))END



Thanks
John.
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (219K reputation)SSC Guru (219K reputation)SSC Guru (219K reputation)SSC Guru (219K reputation)SSC Guru (219K reputation)SSC Guru (219K reputation)SSC Guru (219K reputation)SSC Guru (219K reputation)

Group: General Forum Members
Points: 219670 Visits: 42002
Shadab Shah (12/25/2013)
I am trying to store the value return by the select in variable. I took that variable as varchar as well as date but nohing seems to work.....


That is correct. You cannot store a blank in a DATE datatype. The blank will automatically be converted to 1900-01-01. To wit, you're simply using the wrong datatype for this problem.

If you have a reporting requirement, let's see the code for that instead of the simplified example that you original posted. Then, we might be able to help.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
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