| Author |
Topic |
|
sql_newbie121
Yak Posting Veteran
52 Posts |
Posted - 2009-11-12 : 10:34:00
|
| Hi All:Being myself new in SQL Programming, i will ask many questions even if they are very simple or complex. Please bear with me:I want to write a script that does following thing:--If the date is 11/12/2009 or 11/12/2020 or greater than today's date than display 'D'-- If the date is valid date other than 'D' than disply 'W',-- If the date is blank display '' ( blank'.Please help me in this query. Thanks all. |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
sql_newbie121
Yak Posting Veteran
52 Posts |
Posted - 2009-11-12 : 11:39:35
|
| Brett:SQL Server 2005 Standard Edition. The table name is Customer_Details and the column name is datDetails. Hope this helps.Thanks for your help. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2009-11-12 : 11:51:06
|
OK, this is a BIZARRE reqiuirement, but....SELECT datDetails, CASE WHEN datDetails = '11/20/2009' THEN 'W' WHEN DATEDIFF(dd, '11/20/2020',CONVERT(datetime,datDetails)) > 0 THEN 'W' WHEN datDetails = '' THEN '' ELSE 'D' END AS Date_Ind FROM Customer_DetailsWHERE ISDATE(datDetails) = 1 Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
sql_newbie121
Yak Posting Veteran
52 Posts |
Posted - 2009-11-12 : 11:52:27
|
| datDetais is date time.What we have to do is, in the column, if the datDetails is 11/12/2009 or 11/12/2020 or greater than today's date than it should display 'D' in the application.Hope this clearifies you . |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2009-11-12 : 11:58:28
|
Well then. First, this is probably a presentation layer issueSecond, if the column is datetime, then it can't be blank AND must be a valid dateSo that get's rid of most of your "Criteria"So, then we haveSELECT datDetails, CASE WHEN datDetails IN ('11/20/2009','11/20/2020') OR DATEDIFF(dd, GetDate(),datDetails) > 0 THEN 'W' ELSE 'D' END AS Date_Ind FROM Customer_DetailsBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
sql_newbie121
Yak Posting Veteran
52 Posts |
Posted - 2009-11-12 : 12:12:07
|
| Brett:I am going to change datDetails column to varchar(25), if this is the case, how can i achieve this? The second criteria should tell that If the date is valid date other than 'D' than disply 'W'. Here other than D means other than the dates 11/20/2009, 11/20/2020 and the date greater than today's date. As i said am new in T-SQL so can you please elaborate a little bit for the how you did it. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2009-11-12 : 12:28:12
|
| First, did you run the code and did it work as you expected?Second, if you are considering making a valid datetime field into a wild west varchar, I might have to just stop "helping" now. That would be a very BAD idea.Third, isn't the case statement kind of self-explanitory? It really can't get much simpler.What, specifically, do I need to explain to you?Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
sql_newbie121
Yak Posting Veteran
52 Posts |
Posted - 2009-11-12 : 12:41:23
|
| Brett:I did run the code am not getting expected result set.You said that there will not be blank in the date time field so am going to change to varchar, is this a bad practice ( or bad idea). |
 |
|
|
sql_newbie121
Yak Posting Veteran
52 Posts |
Posted - 2009-11-12 : 12:45:25
|
| Brett:It is not giving the blank fields. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
sql_newbie121
Yak Posting Veteran
52 Posts |
Posted - 2009-11-12 : 14:34:05
|
| No i got only D as an output. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2009-11-12 : 14:56:47
|
OK, How aout NOT THIS SELECT datDetails, CASE WHEN CONVERT(varchar(10),datDetails,101) IN ('11/20/2009','11/20/2020') WHEN DATEDIFF(dd, GetDate(),datDetails) > 0 THEN 'D' WHEN datDetails IS NULL THEN 'blank' ELSE 'W' END AS Date_Ind FROM Customer_Details Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2009-11-12 : 15:01:09
|
I think this matches ALL of your criteriaCREATE TABLE #myCustomer_Details (datDetails datetime)GOINSERT INTO #myCustomer_Details (datDetails)SELECT '11/20/2001 12:00:00' UNION ALLSELECT '11/20/2009 13:00:00' UNION ALLSELECT '11/20/2020 14:00:00' UNION ALLSELECT '11/30/2009 15:00:00' UNION ALLSELECT '11/30/5555 16:00:00' UNION ALLSELECT nullGOSELECT datDetails, CASE WHEN CONVERT(varchar(10),datDetails,101) IN ('11/20/2009','11/20/2020') THEN 'D' WHEN DATEDIFF(dd, GetDate(),datDetails) > 0 THEN 'D' WHEN datDetails IS NULL THEN 'blank' ELSE 'W' END AS Date_Ind FROM #myCustomer_DetailsGODROP TABLE #myCustomer_DetailsGOI'll have a Guiness...from KingstonBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
sql_newbie121
Yak Posting Veteran
52 Posts |
Posted - 2009-11-12 : 15:04:39
|
| I get Msg 156, Level 15, State 1, Line 4Incorrect syntax near the keyword 'WHEN'. Am not able to catch the problem here. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
sql_newbie121
Yak Posting Veteran
52 Posts |
Posted - 2009-11-12 : 15:34:48
|
| Brett:Perfect, you are the SCRIPT KING, teach me how to become a SQL Programmer. Thanks a lot and i really appreciate your help. Where should i start for SQL Programming!!!! |
 |
|
|
Next Page
|