954,560 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Character Date String

Guys,

I have a Date string. My objective is how to get the Datename.
I got this Receivedate as string when I used the Bulk insert. i make the
receiptdate as nvarchar because i got an error when i used the bulk insert.

Select 
	ItemNumber,
	PurchasePrice,
	ReceivingPO,
    Cast(ReceivedDate as Datetime) as ReceivedMonth
From #VPTable as vp
Where vp.ReceivingPO is not null

Thanks.

JOv

jovillanuev
Junior Poster in Training
76 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

If your Cast is working, then I don't see a problem.

Select 
	ItemNumber,
	PurchasePrice,
	ReceivingPO,
   DATENAME(d,Cast(ReceivedDate as Datetime) as ReceivedMonth) as 'Day' -- Change d to whatever you need.
From #VPTable as vp
Where vp.ReceivingPO is not null
adam_k
Practically a Posting Shark
803 posts since Jun 2011
Reputation Points: 256
Solved Threads: 149
 

Thanks Adam_K.

Got an ERROR from this script..

Select 
	vp.ItemNumber,
	vp.PurchasePrice,
	vp.ReceivingPO,
    --Cast(vp.ReceivedDate as Datetime) as POMonth,
    DATENAME(Month,Cast(ReceivedDate AS Datetime) AS ReceivedMonth) AS 'POMonth' 
From #VPTable as vp with (nolock)
Msg 156, Level 15, State 1, Line 6
Incorrect syntax near the keyword 'AS'.
Msg 319, Level 15, State 1, Line 7
Incorrect syntax near the keyword 'with'. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon.
jovillanuev
Junior Poster in Training
76 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

Try the following?:

SELECT ItemNumber,	
             PurchasePrice,
             ReceivingPO,
             DATENAME(MONTH,CONVERT(DATETIME, ReceivedDate)) AS ReceivedMonth
FROM #VPTable as vp
WHERE vp.ReceivingPO IS NOT NULL
Eagletalon
Junior Poster
113 posts since Mar 2011
Reputation Points: 47
Solved Threads: 13
 

Eagletalon is right. I should have removed the AS ReceivedMonth part.

adam_k
Practically a Posting Shark
803 posts since Jun 2011
Reputation Points: 256
Solved Threads: 149
 

Hi Adam_K and Eagletalon,


Here is my Data Structure. I got received an error when executing that script
coverting the date string to datetime.

Error:
Msg 241, Level 16, State 1, Line 2
Conversion failed when converting datetime from character string.


Create Table #VPTable
(
  ItemNumber nvarchar(255),
  PurchasePrice Varchar(255),
  ReceivingPO nvarchar(50),
  ReceivedDate nvarchar(24),
)

Thank you..

JV

jovillanuev
Junior Poster in Training
76 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

Hi jovillanuev

Error:
Msg 241, Level 16, State 1, Line 2
Conversion failed when converting datetime from character string.

This error is 1 I've had many times over... it means that the string you are using for a date is not in a valid format for SQL to interpret...

Could be something like: "201121 14:35"...

would it be possible to just select the string to see the output? give us an example of what needs to be converted

Eagletalon
Junior Poster
113 posts since Mar 2011
Reputation Points: 47
Solved Threads: 13
 

Here is the string format for ReceivedDate

This is the fields -- ReceivedDate nvarchar(24)

Output  --   '5/28/2011'
jovillanuev
Junior Poster in Training
76 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

It runs perfectly for me?

QUERY:

SELECT DATENAME(MONTH, CONVERT(DATETIME, '5/28/2011')) AS ReceivedMonth


RESULTS:

May
Eagletalon
Junior Poster
113 posts since Mar 2011
Reputation Points: 47
Solved Threads: 13
 

It runs perfectly for me?

QUERY:

SELECT DATENAME(MONTH, CONVERT(DATETIME, '5/28/2011')) AS ReceivedMonth

RESULTS:

May

That's because your regional settings is US or any other that has MM/DD/YYYY.
If I where to run it I would get the same error (I'm Greek = DD/MM/YYYY).


jovillanuev put

SET DATEFORMAT mdy


before the query and run them together. This will tell SQL how to read the date correctly.

adam_k
Practically a Posting Shark
803 posts since Jun 2011
Reputation Points: 256
Solved Threads: 149
 
That's because your regional settings is US or any other that has MM/DD/YYYY. If I where to run it I would get the same error (I'm Greek = DD/MM/YYYY).

Wow... there I learned something awesome :D... thnx man... oh and btw I'm from South Africa and I believe the setting fall under UK? :S nevertheless if that solves it then bloody well done, wouldn't have thought of it in years

Eagletalon
Junior Poster
113 posts since Mar 2011
Reputation Points: 47
Solved Threads: 13
 

jovillanuev has the dateformat worked for you?

adam_k
Practically a Posting Shark
803 posts since Jun 2011
Reputation Points: 256
Solved Threads: 149
 

Thank you very much guys...

Regards,

Jov

jovillanuev
Junior Poster in Training
76 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You